// 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.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.AgentPoolsClient;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.AgentPoolInner;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.AgentPoolListResultInner;
import com.azure.resourcemanager.hybridcontainerservice.models.AgentPool;
import com.azure.resourcemanager.hybridcontainerservice.models.AgentPoolListResult;
import com.azure.resourcemanager.hybridcontainerservice.models.AgentPools;

public final class AgentPoolsImpl implements AgentPools {
    private static final ClientLogger LOGGER = new ClientLogger(AgentPoolsImpl.class);

    private final AgentPoolsClient innerClient;

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

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

    public Response<AgentPool> getWithResponse(
        String resourceGroupName, String resourceName, String agentPoolName, Context context) {
        Response<AgentPoolInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, resourceName, agentPoolName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AgentPoolImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AgentPool get(String resourceGroupName, String resourceName, String agentPoolName) {
        AgentPoolInner inner = this.serviceClient().get(resourceGroupName, resourceName, agentPoolName);
        if (inner != null) {
            return new AgentPoolImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public AgentPoolListResult listByProvisionedCluster(String resourceGroupName, String resourceName) {
        AgentPoolListResultInner inner = this.serviceClient().listByProvisionedCluster(resourceGroupName, resourceName);
        if (inner != null) {
            return new AgentPoolListResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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