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

package com.azure.resourcemanager.datalakestore.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.datalakestore.fluent.TrustedIdProvidersClient;
import com.azure.resourcemanager.datalakestore.fluent.models.TrustedIdProviderInner;
import com.azure.resourcemanager.datalakestore.models.TrustedIdProvider;
import com.azure.resourcemanager.datalakestore.models.TrustedIdProviders;

public final class TrustedIdProvidersImpl implements TrustedIdProviders {
    private static final ClientLogger LOGGER = new ClientLogger(TrustedIdProvidersImpl.class);

    private final TrustedIdProvidersClient innerClient;

    private final com.azure.resourcemanager.datalakestore.DataLakeStoreManager serviceManager;

    public TrustedIdProvidersImpl(
        TrustedIdProvidersClient innerClient,
        com.azure.resourcemanager.datalakestore.DataLakeStoreManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<TrustedIdProvider> listByAccount(String resourceGroupName, String accountName) {
        PagedIterable<TrustedIdProviderInner> inner =
            this.serviceClient().listByAccount(resourceGroupName, accountName);
        return Utils.mapPage(inner, inner1 -> new TrustedIdProviderImpl(inner1, this.manager()));
    }

    public PagedIterable<TrustedIdProvider> listByAccount(
        String resourceGroupName, String accountName, Context context) {
        PagedIterable<TrustedIdProviderInner> inner =
            this.serviceClient().listByAccount(resourceGroupName, accountName, context);
        return Utils.mapPage(inner, inner1 -> new TrustedIdProviderImpl(inner1, this.manager()));
    }

    public Response<TrustedIdProvider> getWithResponse(
        String resourceGroupName, String accountName, String trustedIdProviderName, Context context) {
        Response<TrustedIdProviderInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, accountName, trustedIdProviderName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new TrustedIdProviderImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public TrustedIdProvider get(String resourceGroupName, String accountName, String trustedIdProviderName) {
        TrustedIdProviderInner inner = this.serviceClient().get(resourceGroupName, accountName, trustedIdProviderName);
        if (inner != null) {
            return new TrustedIdProviderImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String resourceGroupName, String accountName, String trustedIdProviderName) {
        this.serviceClient().delete(resourceGroupName, accountName, trustedIdProviderName);
    }

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

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

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

    private com.azure.resourcemanager.datalakestore.DataLakeStoreManager manager() {
        return this.serviceManager;
    }

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