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

package com.azure.resourcemanager.cognitiveservices.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.cognitiveservices.fluent.AccountsClient;
import com.azure.resourcemanager.cognitiveservices.fluent.models.AccountInner;
import com.azure.resourcemanager.cognitiveservices.fluent.models.AccountModelInner;
import com.azure.resourcemanager.cognitiveservices.fluent.models.AccountSkuListResultInner;
import com.azure.resourcemanager.cognitiveservices.fluent.models.ApiKeysInner;
import com.azure.resourcemanager.cognitiveservices.fluent.models.UsageListResultInner;
import com.azure.resourcemanager.cognitiveservices.models.Account;
import com.azure.resourcemanager.cognitiveservices.models.AccountModel;
import com.azure.resourcemanager.cognitiveservices.models.AccountSkuListResult;
import com.azure.resourcemanager.cognitiveservices.models.Accounts;
import com.azure.resourcemanager.cognitiveservices.models.ApiKeys;
import com.azure.resourcemanager.cognitiveservices.models.RegenerateKeyParameters;
import com.azure.resourcemanager.cognitiveservices.models.UsageListResult;

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

    private final AccountsClient innerClient;

    private final com.azure.resourcemanager.cognitiveservices.CognitiveServicesManager serviceManager;

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

    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<Account> getByResourceGroupWithResponse(
        String resourceGroupName, String accountName, Context context) {
        Response<AccountInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, accountName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AccountImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Account getByResourceGroup(String resourceGroupName, String accountName) {
        AccountInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, accountName);
        if (inner != null) {
            return new AccountImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public PagedIterable<Account> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<AccountInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new AccountImpl(inner1, this.manager()));
    }

    public PagedIterable<Account> list() {
        PagedIterable<AccountInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new AccountImpl(inner1, this.manager()));
    }

    public PagedIterable<Account> list(Context context) {
        PagedIterable<AccountInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new AccountImpl(inner1, this.manager()));
    }

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

    public ApiKeys listKeys(String resourceGroupName, String accountName) {
        ApiKeysInner inner = this.serviceClient().listKeys(resourceGroupName, accountName);
        if (inner != null) {
            return new ApiKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<ApiKeys> regenerateKeyWithResponse(
        String resourceGroupName, String accountName, RegenerateKeyParameters parameters, Context context) {
        Response<ApiKeysInner> inner =
            this.serviceClient().regenerateKeyWithResponse(resourceGroupName, accountName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApiKeysImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ApiKeys regenerateKey(String resourceGroupName, String accountName, RegenerateKeyParameters parameters) {
        ApiKeysInner inner = this.serviceClient().regenerateKey(resourceGroupName, accountName, parameters);
        if (inner != null) {
            return new ApiKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public AccountSkuListResult listSkus(String resourceGroupName, String accountName) {
        AccountSkuListResultInner inner = this.serviceClient().listSkus(resourceGroupName, accountName);
        if (inner != null) {
            return new AccountSkuListResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<UsageListResult> listUsagesWithResponse(
        String resourceGroupName, String accountName, String filter, Context context) {
        Response<UsageListResultInner> inner =
            this.serviceClient().listUsagesWithResponse(resourceGroupName, accountName, filter, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new UsageListResultImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public UsageListResult listUsages(String resourceGroupName, String accountName) {
        UsageListResultInner inner = this.serviceClient().listUsages(resourceGroupName, accountName);
        if (inner != null) {
            return new UsageListResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<AccountModel> listModels(String resourceGroupName, String accountName) {
        PagedIterable<AccountModelInner> inner = this.serviceClient().listModels(resourceGroupName, accountName);
        return Utils.mapPage(inner, inner1 -> new AccountModelImpl(inner1, this.manager()));
    }

    public PagedIterable<AccountModel> listModels(String resourceGroupName, String accountName, Context context) {
        PagedIterable<AccountModelInner> inner =
            this.serviceClient().listModels(resourceGroupName, accountName, context);
        return Utils.mapPage(inner, inner1 -> new AccountModelImpl(inner1, this.manager()));
    }

    public Account 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<Account> 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.cognitiveservices.CognitiveServicesManager manager() {
        return this.serviceManager;
    }

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