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

package com.azure.resourcemanager.hybridcontainerservice.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.hybridcontainerservice.fluent.ProvisionedClustersOperationsClient;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.ProvisionedClusterUpgradeProfileInner;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.ProvisionedClustersResponseInner;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClusterUpgradeProfile;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersOperations;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersResponse;

public final class ProvisionedClustersOperationsImpl implements ProvisionedClustersOperations {
    private static final ClientLogger LOGGER = new ClientLogger(ProvisionedClustersOperationsImpl.class);

    private final ProvisionedClustersOperationsClient innerClient;

    private final com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager;

    public ProvisionedClustersOperationsImpl(
        ProvisionedClustersOperationsClient innerClient,
        com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public ProvisionedClustersResponse getByResourceGroup(String resourceGroupName, String resourceName) {
        ProvisionedClustersResponseInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, resourceName);
        if (inner != null) {
            return new ProvisionedClustersResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public PagedIterable<ProvisionedClustersResponse> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<ProvisionedClustersResponseInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new ProvisionedClustersResponseImpl(inner1, this.manager()));
    }

    public PagedIterable<ProvisionedClustersResponse> list() {
        PagedIterable<ProvisionedClustersResponseInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ProvisionedClustersResponseImpl(inner1, this.manager()));
    }

    public PagedIterable<ProvisionedClustersResponse> list(Context context) {
        PagedIterable<ProvisionedClustersResponseInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new ProvisionedClustersResponseImpl(inner1, this.manager()));
    }

    public Response<ProvisionedClusterUpgradeProfile> getUpgradeProfileWithResponse(
        String resourceGroupName, String resourceName, Context context) {
        Response<ProvisionedClusterUpgradeProfileInner> inner =
            this.serviceClient().getUpgradeProfileWithResponse(resourceGroupName, resourceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ProvisionedClusterUpgradeProfileImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ProvisionedClusterUpgradeProfile getUpgradeProfile(String resourceGroupName, String resourceName) {
        ProvisionedClusterUpgradeProfileInner inner =
            this.serviceClient().getUpgradeProfile(resourceGroupName, resourceName);
        if (inner != null) {
            return new ProvisionedClusterUpgradeProfileImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void upgradeNodeImageVersionForEntireCluster(String resourceGroupName, String resourceName) {
        this.serviceClient().upgradeNodeImageVersionForEntireCluster(resourceGroupName, resourceName);
    }

    public void upgradeNodeImageVersionForEntireCluster(
        String resourceGroupName, String resourceName, Context context) {
        this.serviceClient().upgradeNodeImageVersionForEntireCluster(resourceGroupName, resourceName, context);
    }

    public ProvisionedClustersResponse 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 resourceName = Utils.getValueFromIdByName(id, "provisionedClusters");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisionedClusters'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, resourceName, Context.NONE).getValue();
    }

    public Response<ProvisionedClustersResponse> 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 resourceName = Utils.getValueFromIdByName(id, "provisionedClusters");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisionedClusters'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, resourceName, 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 resourceName = Utils.getValueFromIdByName(id, "provisionedClusters");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisionedClusters'.", id)));
        }
        this.deleteByResourceGroupWithResponse(resourceGroupName, resourceName, 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 resourceName = Utils.getValueFromIdByName(id, "provisionedClusters");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisionedClusters'.", id)));
        }
        return this.deleteByResourceGroupWithResponse(resourceGroupName, resourceName, context);
    }

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

    private com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager manager() {
        return this.serviceManager;
    }

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