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

package com.azure.resourcemanager.avs.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.avs.fluent.AuthorizationsClient;
import com.azure.resourcemanager.avs.fluent.models.ExpressRouteAuthorizationInner;
import com.azure.resourcemanager.avs.models.Authorizations;
import com.azure.resourcemanager.avs.models.ExpressRouteAuthorization;

public final class AuthorizationsImpl implements Authorizations {
    private static final ClientLogger LOGGER = new ClientLogger(AuthorizationsImpl.class);

    private final AuthorizationsClient innerClient;

    private final com.azure.resourcemanager.avs.AvsManager serviceManager;

    public AuthorizationsImpl(
        AuthorizationsClient innerClient, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<ExpressRouteAuthorization> list(String resourceGroupName, String privateCloudName) {
        PagedIterable<ExpressRouteAuthorizationInner> inner =
            this.serviceClient().list(resourceGroupName, privateCloudName);
        return Utils.mapPage(inner, inner1 -> new ExpressRouteAuthorizationImpl(inner1, this.manager()));
    }

    public PagedIterable<ExpressRouteAuthorization> list(
        String resourceGroupName, String privateCloudName, Context context) {
        PagedIterable<ExpressRouteAuthorizationInner> inner =
            this.serviceClient().list(resourceGroupName, privateCloudName, context);
        return Utils.mapPage(inner, inner1 -> new ExpressRouteAuthorizationImpl(inner1, this.manager()));
    }

    public Response<ExpressRouteAuthorization> getWithResponse(
        String resourceGroupName, String privateCloudName, String authorizationName, Context context) {
        Response<ExpressRouteAuthorizationInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, privateCloudName, authorizationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ExpressRouteAuthorizationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ExpressRouteAuthorization get(String resourceGroupName, String privateCloudName, String authorizationName) {
        ExpressRouteAuthorizationInner inner =
            this.serviceClient().get(resourceGroupName, privateCloudName, authorizationName);
        if (inner != null) {
            return new ExpressRouteAuthorizationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String privateCloudName, String authorizationName) {
        this.serviceClient().delete(resourceGroupName, privateCloudName, authorizationName);
    }

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

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

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

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

    private com.azure.resourcemanager.avs.AvsManager manager() {
        return this.serviceManager;
    }

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