// 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.BmcKeySetsClient;
import com.azure.resourcemanager.networkcloud.fluent.models.BmcKeySetInner;
import com.azure.resourcemanager.networkcloud.models.BmcKeySet;
import com.azure.resourcemanager.networkcloud.models.BmcKeySets;

public final class BmcKeySetsImpl implements BmcKeySets {
    private static final ClientLogger LOGGER = new ClientLogger(BmcKeySetsImpl.class);

    private final BmcKeySetsClient innerClient;

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

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

    public PagedIterable<BmcKeySet> listByCluster(String resourceGroupName, String clusterName) {
        PagedIterable<BmcKeySetInner> inner = this.serviceClient().listByCluster(resourceGroupName, clusterName);
        return Utils.mapPage(inner, inner1 -> new BmcKeySetImpl(inner1, this.manager()));
    }

    public PagedIterable<BmcKeySet> listByCluster(String resourceGroupName, String clusterName, Context context) {
        PagedIterable<BmcKeySetInner> inner =
            this.serviceClient().listByCluster(resourceGroupName, clusterName, context);
        return Utils.mapPage(inner, inner1 -> new BmcKeySetImpl(inner1, this.manager()));
    }

    public Response<BmcKeySet> getWithResponse(
        String resourceGroupName, String clusterName, String bmcKeySetName, Context context) {
        Response<BmcKeySetInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, clusterName, bmcKeySetName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new BmcKeySetImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public BmcKeySet get(String resourceGroupName, String clusterName, String bmcKeySetName) {
        BmcKeySetInner inner = this.serviceClient().get(resourceGroupName, clusterName, bmcKeySetName);
        if (inner != null) {
            return new BmcKeySetImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String clusterName, String bmcKeySetName) {
        this.serviceClient().delete(resourceGroupName, clusterName, bmcKeySetName);
    }

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

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

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

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

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

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