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

package com.azure.resourcemanager.customerinsights.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.customerinsights.fluent.AuthorizationPoliciesClient;
import com.azure.resourcemanager.customerinsights.fluent.models.AuthorizationPolicyInner;
import com.azure.resourcemanager.customerinsights.fluent.models.AuthorizationPolicyResourceFormatInner;
import com.azure.resourcemanager.customerinsights.models.AuthorizationPolicies;
import com.azure.resourcemanager.customerinsights.models.AuthorizationPolicy;
import com.azure.resourcemanager.customerinsights.models.AuthorizationPolicyResourceFormat;

public final class AuthorizationPoliciesImpl implements AuthorizationPolicies {
    private static final ClientLogger LOGGER = new ClientLogger(AuthorizationPoliciesImpl.class);

    private final AuthorizationPoliciesClient innerClient;

    private final com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager;

    public AuthorizationPoliciesImpl(
        AuthorizationPoliciesClient innerClient,
        com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<AuthorizationPolicyResourceFormat> getWithResponse(
        String resourceGroupName, String hubName, String authorizationPolicyName, Context context) {
        Response<AuthorizationPolicyResourceFormatInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, hubName, authorizationPolicyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AuthorizationPolicyResourceFormatImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AuthorizationPolicyResourceFormat get(
        String resourceGroupName, String hubName, String authorizationPolicyName) {
        AuthorizationPolicyResourceFormatInner inner =
            this.serviceClient().get(resourceGroupName, hubName, authorizationPolicyName);
        if (inner != null) {
            return new AuthorizationPolicyResourceFormatImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<AuthorizationPolicyResourceFormat> listByHub(String resourceGroupName, String hubName) {
        PagedIterable<AuthorizationPolicyResourceFormatInner> inner =
            this.serviceClient().listByHub(resourceGroupName, hubName);
        return Utils.mapPage(inner, inner1 -> new AuthorizationPolicyResourceFormatImpl(inner1, this.manager()));
    }

    public PagedIterable<AuthorizationPolicyResourceFormat> listByHub(
        String resourceGroupName, String hubName, Context context) {
        PagedIterable<AuthorizationPolicyResourceFormatInner> inner =
            this.serviceClient().listByHub(resourceGroupName, hubName, context);
        return Utils.mapPage(inner, inner1 -> new AuthorizationPolicyResourceFormatImpl(inner1, this.manager()));
    }

    public Response<AuthorizationPolicy> regeneratePrimaryKeyWithResponse(
        String resourceGroupName, String hubName, String authorizationPolicyName, Context context) {
        Response<AuthorizationPolicyInner> inner =
            this
                .serviceClient()
                .regeneratePrimaryKeyWithResponse(resourceGroupName, hubName, authorizationPolicyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AuthorizationPolicyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AuthorizationPolicy regeneratePrimaryKey(
        String resourceGroupName, String hubName, String authorizationPolicyName) {
        AuthorizationPolicyInner inner =
            this.serviceClient().regeneratePrimaryKey(resourceGroupName, hubName, authorizationPolicyName);
        if (inner != null) {
            return new AuthorizationPolicyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AuthorizationPolicy> regenerateSecondaryKeyWithResponse(
        String resourceGroupName, String hubName, String authorizationPolicyName, Context context) {
        Response<AuthorizationPolicyInner> inner =
            this
                .serviceClient()
                .regenerateSecondaryKeyWithResponse(resourceGroupName, hubName, authorizationPolicyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AuthorizationPolicyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AuthorizationPolicy regenerateSecondaryKey(
        String resourceGroupName, String hubName, String authorizationPolicyName) {
        AuthorizationPolicyInner inner =
            this.serviceClient().regenerateSecondaryKey(resourceGroupName, hubName, authorizationPolicyName);
        if (inner != null) {
            return new AuthorizationPolicyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.customerinsights.CustomerInsightsManager manager() {
        return this.serviceManager;
    }

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