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

package com.azure.resourcemanager.networkcloud.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.networkcloud.fluent.AgentPoolsClient;
import com.azure.resourcemanager.networkcloud.fluent.models.AgentPoolInner;
import com.azure.resourcemanager.networkcloud.models.AgentPool;
import com.azure.resourcemanager.networkcloud.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.networkcloud.NetworkCloudManager serviceManager;

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

    public PagedIterable<AgentPool> listByKubernetesCluster(String resourceGroupName, String kubernetesClusterName) {
        PagedIterable<AgentPoolInner> inner =
            this.serviceClient().listByKubernetesCluster(resourceGroupName, kubernetesClusterName);
        return Utils.mapPage(inner, inner1 -> new AgentPoolImpl(inner1, this.manager()));
    }

    public PagedIterable<AgentPool> listByKubernetesCluster(
        String resourceGroupName, String kubernetesClusterName, Context context) {
        PagedIterable<AgentPoolInner> inner =
            this.serviceClient().listByKubernetesCluster(resourceGroupName, kubernetesClusterName, context);
        return Utils.mapPage(inner, inner1 -> new AgentPoolImpl(inner1, this.manager()));
    }

    public Response<AgentPool> getWithResponse(
        String resourceGroupName, String kubernetesClusterName, String agentPoolName, Context context) {
        Response<AgentPoolInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, kubernetesClusterName, 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 kubernetesClusterName, String agentPoolName) {
        AgentPoolInner inner = this.serviceClient().get(resourceGroupName, kubernetesClusterName, agentPoolName);
        if (inner != null) {
            return new AgentPoolImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String resourceGroupName, String kubernetesClusterName, String agentPoolName, Context context) {
        this.serviceClient().delete(resourceGroupName, kubernetesClusterName, agentPoolName, context);
    }

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

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

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

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