// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.desktopvirtualization.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.desktopvirtualization.fluent.ScalingPlansClient;
import com.azure.resourcemanager.desktopvirtualization.fluent.models.ScalingPlanInner;
import com.azure.resourcemanager.desktopvirtualization.models.ScalingPlan;
import com.azure.resourcemanager.desktopvirtualization.models.ScalingPlans;

public final class ScalingPlansImpl implements ScalingPlans {
    private static final ClientLogger LOGGER = new ClientLogger(ScalingPlansImpl.class);

    private final ScalingPlansClient innerClient;

    private final com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager;

    public ScalingPlansImpl(
        ScalingPlansClient innerClient,
        com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ScalingPlan> getByResourceGroupWithResponse(
        String resourceGroupName, String scalingPlanName, Context context) {
        Response<ScalingPlanInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, scalingPlanName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ScalingPlanImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ScalingPlan getByResourceGroup(String resourceGroupName, String scalingPlanName) {
        ScalingPlanInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, scalingPlanName);
        if (inner != null) {
            return new ScalingPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteByResourceGroupWithResponse(
        String resourceGroupName, String scalingPlanName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, scalingPlanName, context);
    }

    public void deleteByResourceGroup(String resourceGroupName, String scalingPlanName) {
        this.serviceClient().delete(resourceGroupName, scalingPlanName);
    }

    public PagedIterable<ScalingPlan> listByResourceGroup(String resourceGroupName) {
        PagedIterable<ScalingPlanInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new ScalingPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<ScalingPlan> listByResourceGroup(
        String resourceGroupName, Integer pageSize, Boolean isDescending, Integer initialSkip, Context context) {
        PagedIterable<ScalingPlanInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new ScalingPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<ScalingPlan> list() {
        PagedIterable<ScalingPlanInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ScalingPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<ScalingPlan> list(
        Integer pageSize, Boolean isDescending, Integer initialSkip, Context context) {
        PagedIterable<ScalingPlanInner> inner = this.serviceClient().list(pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new ScalingPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<ScalingPlan> listByHostPool(String resourceGroupName, String hostPoolName) {
        PagedIterable<ScalingPlanInner> inner = this.serviceClient().listByHostPool(resourceGroupName, hostPoolName);
        return Utils.mapPage(inner, inner1 -> new ScalingPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<ScalingPlan> listByHostPool(
        String resourceGroupName,
        String hostPoolName,
        Integer pageSize,
        Boolean isDescending,
        Integer initialSkip,
        Context context) {
        PagedIterable<ScalingPlanInner> inner =
            this
                .serviceClient()
                .listByHostPool(resourceGroupName, hostPoolName, pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new ScalingPlanImpl(inner1, this.manager()));
    }

    public ScalingPlan getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String scalingPlanName = Utils.getValueFromIdByName(id, "scalingPlans");
        if (scalingPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scalingPlans'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, scalingPlanName, Context.NONE).getValue();
    }

    public Response<ScalingPlan> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String scalingPlanName = Utils.getValueFromIdByName(id, "scalingPlans");
        if (scalingPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scalingPlans'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, scalingPlanName, context);
    }

    public void deleteById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String scalingPlanName = Utils.getValueFromIdByName(id, "scalingPlans");
        if (scalingPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scalingPlans'.", id)));
        }
        this.deleteByResourceGroupWithResponse(resourceGroupName, scalingPlanName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String scalingPlanName = Utils.getValueFromIdByName(id, "scalingPlans");
        if (scalingPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scalingPlans'.", id)));
        }
        return this.deleteByResourceGroupWithResponse(resourceGroupName, scalingPlanName, context);
    }

    private ScalingPlansClient serviceClient() {
        return this.innerClient;
    }

    private com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager manager() {
        return this.serviceManager;
    }

    public ScalingPlanImpl define(String name) {
        return new ScalingPlanImpl(name, this.manager());
    }
}
