// 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.ClusterManagersClient;
import com.azure.resourcemanager.networkcloud.fluent.models.ClusterManagerInner;
import com.azure.resourcemanager.networkcloud.models.ClusterManager;
import com.azure.resourcemanager.networkcloud.models.ClusterManagers;

public final class ClusterManagersImpl implements ClusterManagers {
    private static final ClientLogger LOGGER = new ClientLogger(ClusterManagersImpl.class);

    private final ClusterManagersClient innerClient;

    private final com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager;

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

    public PagedIterable<ClusterManager> list() {
        PagedIterable<ClusterManagerInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ClusterManagerImpl(inner1, this.manager()));
    }

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

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

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

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

    public ClusterManager getByResourceGroup(String resourceGroupName, String clusterManagerName) {
        ClusterManagerInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, clusterManagerName);
        if (inner != null) {
            return new ClusterManagerImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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