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

package com.azure.resourcemanager.automation.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.automation.fluent.CredentialsClient;
import com.azure.resourcemanager.automation.fluent.models.CredentialInner;
import com.azure.resourcemanager.automation.models.Credential;
import com.azure.resourcemanager.automation.models.Credentials;

public final class CredentialsImpl implements Credentials {
    private static final ClientLogger LOGGER = new ClientLogger(CredentialsImpl.class);

    private final CredentialsClient innerClient;

    private final com.azure.resourcemanager.automation.AutomationManager serviceManager;

    public CredentialsImpl(
        CredentialsClient innerClient, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public void delete(String resourceGroupName, String automationAccountName, String credentialName) {
        this.serviceClient().delete(resourceGroupName, automationAccountName, credentialName);
    }

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

    public Credential get(String resourceGroupName, String automationAccountName, String credentialName) {
        CredentialInner inner = this.serviceClient().get(resourceGroupName, automationAccountName, credentialName);
        if (inner != null) {
            return new CredentialImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Credential> getWithResponse(
        String resourceGroupName, String automationAccountName, String credentialName, Context context) {
        Response<CredentialInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, automationAccountName, credentialName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CredentialImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<Credential> listByAutomationAccount(String resourceGroupName, String automationAccountName) {
        PagedIterable<CredentialInner> inner =
            this.serviceClient().listByAutomationAccount(resourceGroupName, automationAccountName);
        return Utils.mapPage(inner, inner1 -> new CredentialImpl(inner1, this.manager()));
    }

    public PagedIterable<Credential> listByAutomationAccount(
        String resourceGroupName, String automationAccountName, Context context) {
        PagedIterable<CredentialInner> inner =
            this.serviceClient().listByAutomationAccount(resourceGroupName, automationAccountName, context);
        return Utils.mapPage(inner, inner1 -> new CredentialImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.automation.AutomationManager manager() {
        return this.serviceManager;
    }

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