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

package com.azure.resourcemanager.datafactory.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.datafactory.fluent.CredentialOperationsClient;
import com.azure.resourcemanager.datafactory.fluent.models.ManagedIdentityCredentialResourceInner;
import com.azure.resourcemanager.datafactory.models.CredentialOperations;
import com.azure.resourcemanager.datafactory.models.ManagedIdentityCredentialResource;

public final class CredentialOperationsImpl implements CredentialOperations {
    private static final ClientLogger LOGGER = new ClientLogger(CredentialOperationsImpl.class);

    private final CredentialOperationsClient innerClient;

    private final com.azure.resourcemanager.datafactory.DataFactoryManager serviceManager;

    public CredentialOperationsImpl(
        CredentialOperationsClient innerClient,
        com.azure.resourcemanager.datafactory.DataFactoryManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<ManagedIdentityCredentialResource> listByFactory(
        String resourceGroupName, String factoryName) {
        PagedIterable<ManagedIdentityCredentialResourceInner> inner =
            this.serviceClient().listByFactory(resourceGroupName, factoryName);
        return Utils.mapPage(inner, inner1 -> new ManagedIdentityCredentialResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<ManagedIdentityCredentialResource> listByFactory(
        String resourceGroupName, String factoryName, Context context) {
        PagedIterable<ManagedIdentityCredentialResourceInner> inner =
            this.serviceClient().listByFactory(resourceGroupName, factoryName, context);
        return Utils.mapPage(inner, inner1 -> new ManagedIdentityCredentialResourceImpl(inner1, this.manager()));
    }

    public Response<ManagedIdentityCredentialResource> getWithResponse(
        String resourceGroupName, String factoryName, String credentialName, String ifNoneMatch, Context context) {
        Response<ManagedIdentityCredentialResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, factoryName, credentialName, ifNoneMatch, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ManagedIdentityCredentialResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ManagedIdentityCredentialResource get(String resourceGroupName, String factoryName, String credentialName) {
        ManagedIdentityCredentialResourceInner inner =
            this.serviceClient().get(resourceGroupName, factoryName, credentialName);
        if (inner != null) {
            return new ManagedIdentityCredentialResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public Response<ManagedIdentityCredentialResource> getByIdWithResponse(
        String id, String ifNoneMatch, 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 factoryName = Utils.getValueFromIdByName(id, "factories");
        if (factoryName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'factories'.", 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, factoryName, credentialName, ifNoneMatch, 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 factoryName = Utils.getValueFromIdByName(id, "factories");
        if (factoryName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'factories'.", 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, factoryName, 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 factoryName = Utils.getValueFromIdByName(id, "factories");
        if (factoryName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'factories'.", 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, factoryName, credentialName, context);
    }

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

    private com.azure.resourcemanager.datafactory.DataFactoryManager manager() {
        return this.serviceManager;
    }

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