// 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.AccountsClient;
import com.azure.resourcemanager.datalakestore.fluent.models.DataLakeStoreAccountBasicInner;
import com.azure.resourcemanager.datalakestore.fluent.models.DataLakeStoreAccountInner;
import com.azure.resourcemanager.datalakestore.fluent.models.NameAvailabilityInformationInner;
import com.azure.resourcemanager.datalakestore.models.Accounts;
import com.azure.resourcemanager.datalakestore.models.CheckNameAvailabilityParameters;
import com.azure.resourcemanager.datalakestore.models.DataLakeStoreAccount;
import com.azure.resourcemanager.datalakestore.models.DataLakeStoreAccountBasic;
import com.azure.resourcemanager.datalakestore.models.NameAvailabilityInformation;

public final class AccountsImpl implements Accounts {
    private static final ClientLogger LOGGER = new ClientLogger(AccountsImpl.class);

    private final AccountsClient innerClient;

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

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

    public PagedIterable<DataLakeStoreAccountBasic> list() {
        PagedIterable<DataLakeStoreAccountBasicInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new DataLakeStoreAccountBasicImpl(inner1, this.manager()));
    }

    public PagedIterable<DataLakeStoreAccountBasic> list(
        String filter, Integer top, Integer skip, String select, String orderby, Boolean count, Context context) {
        PagedIterable<DataLakeStoreAccountBasicInner> inner =
            this.serviceClient().list(filter, top, skip, select, orderby, count, context);
        return Utils.mapPage(inner, inner1 -> new DataLakeStoreAccountBasicImpl(inner1, this.manager()));
    }

    public PagedIterable<DataLakeStoreAccountBasic> listByResourceGroup(String resourceGroupName) {
        PagedIterable<DataLakeStoreAccountBasicInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new DataLakeStoreAccountBasicImpl(inner1, this.manager()));
    }

    public PagedIterable<DataLakeStoreAccountBasic> listByResourceGroup(
        String resourceGroupName,
        String filter,
        Integer top,
        Integer skip,
        String select,
        String orderby,
        Boolean count,
        Context context) {
        PagedIterable<DataLakeStoreAccountBasicInner> inner =
            this
                .serviceClient()
                .listByResourceGroup(resourceGroupName, filter, top, skip, select, orderby, count, context);
        return Utils.mapPage(inner, inner1 -> new DataLakeStoreAccountBasicImpl(inner1, this.manager()));
    }

    public Response<DataLakeStoreAccount> getByResourceGroupWithResponse(
        String resourceGroupName, String accountName, Context context) {
        Response<DataLakeStoreAccountInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, accountName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DataLakeStoreAccountImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public DataLakeStoreAccount getByResourceGroup(String resourceGroupName, String accountName) {
        DataLakeStoreAccountInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, accountName);
        if (inner != null) {
            return new DataLakeStoreAccountImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

    public Response<NameAvailabilityInformation> checkNameAvailabilityWithResponse(
        String location, CheckNameAvailabilityParameters parameters, Context context) {
        Response<NameAvailabilityInformationInner> inner =
            this.serviceClient().checkNameAvailabilityWithResponse(location, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new NameAvailabilityInformationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public NameAvailabilityInformation checkNameAvailability(
        String location, CheckNameAvailabilityParameters parameters) {
        NameAvailabilityInformationInner inner = this.serviceClient().checkNameAvailability(location, parameters);
        if (inner != null) {
            return new NameAvailabilityInformationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public DataLakeStoreAccount 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, accountName, Context.NONE).getValue();
    }

    public Response<DataLakeStoreAccount> 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, accountName, 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)));
        }
        this.delete(resourceGroupName, accountName, 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 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)));
        }
        this.delete(resourceGroupName, accountName, context);
    }

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

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

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