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

package com.azure.resourcemanager.quota.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.quota.fluent.QuotasClient;
import com.azure.resourcemanager.quota.fluent.models.CurrentQuotaLimitBaseInner;
import com.azure.resourcemanager.quota.models.CurrentQuotaLimitBase;
import com.azure.resourcemanager.quota.models.Quotas;
import com.azure.resourcemanager.quota.models.QuotasGetResponse;

public final class QuotasImpl implements Quotas {
    private static final ClientLogger LOGGER = new ClientLogger(QuotasImpl.class);

    private final QuotasClient innerClient;

    private final com.azure.resourcemanager.quota.QuotaManager serviceManager;

    public QuotasImpl(QuotasClient innerClient, com.azure.resourcemanager.quota.QuotaManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<CurrentQuotaLimitBase> getWithResponse(String resourceName, String scope, Context context) {
        QuotasGetResponse inner = this.serviceClient().getWithResponse(resourceName, scope, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CurrentQuotaLimitBaseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CurrentQuotaLimitBase get(String resourceName, String scope) {
        CurrentQuotaLimitBaseInner inner = this.serviceClient().get(resourceName, scope);
        if (inner != null) {
            return new CurrentQuotaLimitBaseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<CurrentQuotaLimitBase> list(String scope) {
        PagedIterable<CurrentQuotaLimitBaseInner> inner = this.serviceClient().list(scope);
        return Utils.mapPage(inner, inner1 -> new CurrentQuotaLimitBaseImpl(inner1, this.manager()));
    }

    public PagedIterable<CurrentQuotaLimitBase> list(String scope, Context context) {
        PagedIterable<CurrentQuotaLimitBaseInner> inner = this.serviceClient().list(scope, context);
        return Utils.mapPage(inner, inner1 -> new CurrentQuotaLimitBaseImpl(inner1, this.manager()));
    }

    public CurrentQuotaLimitBase getById(String id) {
        String resourceName =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}", "resourceName");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'quotas'.", id)));
        }
        String scope =
            Utils
                .getValueFromIdByParameterName(id, "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}", "scope");
        if (scope == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scope'.", id)));
        }
        return this.getWithResponse(resourceName, scope, Context.NONE).getValue();
    }

    public Response<CurrentQuotaLimitBase> getByIdWithResponse(String id, Context context) {
        String resourceName =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}", "resourceName");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'quotas'.", id)));
        }
        String scope =
            Utils
                .getValueFromIdByParameterName(id, "/{scope}/providers/Microsoft.Quota/quotas/{resourceName}", "scope");
        if (scope == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scope'.", id)));
        }
        return this.getWithResponse(resourceName, scope, context);
    }

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

    private com.azure.resourcemanager.quota.QuotaManager manager() {
        return this.serviceManager;
    }

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