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

package com.azure.resourcemanager.azurestackhci.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.azurestackhci.fluent.ArcSettingsClient;
import com.azure.resourcemanager.azurestackhci.fluent.models.ArcIdentityResponseInner;
import com.azure.resourcemanager.azurestackhci.fluent.models.ArcSettingInner;
import com.azure.resourcemanager.azurestackhci.fluent.models.PasswordCredentialInner;
import com.azure.resourcemanager.azurestackhci.models.ArcIdentityResponse;
import com.azure.resourcemanager.azurestackhci.models.ArcSetting;
import com.azure.resourcemanager.azurestackhci.models.ArcSettings;
import com.azure.resourcemanager.azurestackhci.models.PasswordCredential;

public final class ArcSettingsImpl implements ArcSettings {
    private static final ClientLogger LOGGER = new ClientLogger(ArcSettingsImpl.class);

    private final ArcSettingsClient innerClient;

    private final com.azure.resourcemanager.azurestackhci.AzureStackHciManager serviceManager;

    public ArcSettingsImpl(
        ArcSettingsClient innerClient, com.azure.resourcemanager.azurestackhci.AzureStackHciManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

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

    public ArcSetting get(String resourceGroupName, String clusterName, String arcSettingName) {
        ArcSettingInner inner = this.serviceClient().get(resourceGroupName, clusterName, arcSettingName);
        if (inner != null) {
            return new ArcSettingImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public PasswordCredential generatePassword(String resourceGroupName, String clusterName, String arcSettingName) {
        PasswordCredentialInner inner =
            this.serviceClient().generatePassword(resourceGroupName, clusterName, arcSettingName);
        if (inner != null) {
            return new PasswordCredentialImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<PasswordCredential> generatePasswordWithResponse(
        String resourceGroupName, String clusterName, String arcSettingName, Context context) {
        Response<PasswordCredentialInner> inner =
            this.serviceClient().generatePasswordWithResponse(resourceGroupName, clusterName, arcSettingName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PasswordCredentialImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ArcIdentityResponse createIdentity(String resourceGroupName, String clusterName, String arcSettingName) {
        ArcIdentityResponseInner inner =
            this.serviceClient().createIdentity(resourceGroupName, clusterName, arcSettingName);
        if (inner != null) {
            return new ArcIdentityResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public ArcIdentityResponse createIdentity(
        String resourceGroupName, String clusterName, String arcSettingName, Context context) {
        ArcIdentityResponseInner inner =
            this.serviceClient().createIdentity(resourceGroupName, clusterName, arcSettingName, context);
        if (inner != null) {
            return new ArcIdentityResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.azurestackhci.AzureStackHciManager manager() {
        return this.serviceManager;
    }

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