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

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

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

    public Response<CurrentQuotaLimitBase> getWithResponse(
        String subscriptionId, String providerId, String location, String resourceName, Context context) {
        QuotasGetResponse inner =
            this.serviceClient().getWithResponse(subscriptionId, providerId, location, resourceName, 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 subscriptionId, String providerId, String location, String resourceName) {
        CurrentQuotaLimitBaseInner inner = this.serviceClient().get(subscriptionId, providerId, location, resourceName);
        if (inner != null) {
            return new CurrentQuotaLimitBaseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public CurrentQuotaLimitBase getById(String id) {
        String subscriptionId = Utils.getValueFromIdByName(id, "subscriptions");
        if (subscriptionId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'subscriptions'.", id)));
        }
        String providerId = Utils.getValueFromIdByName(id, "resourceProviders");
        if (providerId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'resourceProviders'.", id)));
        }
        String location = Utils.getValueFromIdByName(id, "locations");
        if (location == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'locations'.", id)));
        }
        String resourceName = Utils.getValueFromIdByName(id, "serviceLimits");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'serviceLimits'.", id)));
        }
        return this.getWithResponse(subscriptionId, providerId, location, resourceName, Context.NONE).getValue();
    }

    public Response<CurrentQuotaLimitBase> getByIdWithResponse(String id, Context context) {
        String subscriptionId = Utils.getValueFromIdByName(id, "subscriptions");
        if (subscriptionId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'subscriptions'.", id)));
        }
        String providerId = Utils.getValueFromIdByName(id, "resourceProviders");
        if (providerId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'resourceProviders'.", id)));
        }
        String location = Utils.getValueFromIdByName(id, "locations");
        if (location == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'locations'.", id)));
        }
        String resourceName = Utils.getValueFromIdByName(id, "serviceLimits");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'serviceLimits'.", id)));
        }
        return this.getWithResponse(subscriptionId, providerId, location, resourceName, context);
    }

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

    private com.azure.resourcemanager.reservations.ReservationsManager manager() {
        return this.serviceManager;
    }

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