// 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.Response;
import com.azure.core.util.Context;
import com.azure.resourcemanager.customerinsights.fluent.models.AuthorizationPolicyResourceFormatInner;
import com.azure.resourcemanager.customerinsights.models.AuthorizationPolicy;
import com.azure.resourcemanager.customerinsights.models.AuthorizationPolicyResourceFormat;
import com.azure.resourcemanager.customerinsights.models.PermissionTypes;
import java.util.Collections;
import java.util.List;

public final class AuthorizationPolicyResourceFormatImpl
    implements AuthorizationPolicyResourceFormat,
        AuthorizationPolicyResourceFormat.Definition,
        AuthorizationPolicyResourceFormat.Update {
    private AuthorizationPolicyResourceFormatInner innerObject;

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

    public String id() {
        return this.innerModel().id();
    }

    public String name() {
        return this.innerModel().name();
    }

    public String type() {
        return this.innerModel().type();
    }

    public String policyName() {
        return this.innerModel().policyName();
    }

    public List<PermissionTypes> permissions() {
        List<PermissionTypes> inner = this.innerModel().permissions();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public String primaryKey() {
        return this.innerModel().primaryKey();
    }

    public String secondaryKey() {
        return this.innerModel().secondaryKey();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

    public AuthorizationPolicyResourceFormatInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String hubName;

    private String authorizationPolicyName;

    public AuthorizationPolicyResourceFormatImpl withExistingHub(String resourceGroupName, String hubName) {
        this.resourceGroupName = resourceGroupName;
        this.hubName = hubName;
        return this;
    }

    public AuthorizationPolicyResourceFormat create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAuthorizationPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, hubName, authorizationPolicyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public AuthorizationPolicyResourceFormat create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAuthorizationPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, hubName, authorizationPolicyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    AuthorizationPolicyResourceFormatImpl(
        String name, com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerObject = new AuthorizationPolicyResourceFormatInner();
        this.serviceManager = serviceManager;
        this.authorizationPolicyName = name;
    }

    public AuthorizationPolicyResourceFormatImpl update() {
        return this;
    }

    public AuthorizationPolicyResourceFormat apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAuthorizationPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, hubName, authorizationPolicyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public AuthorizationPolicyResourceFormat apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAuthorizationPolicies()
                .createOrUpdateWithResponse(
                    resourceGroupName, hubName, authorizationPolicyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    AuthorizationPolicyResourceFormatImpl(
        AuthorizationPolicyResourceFormatInner innerObject,
        com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.hubName = Utils.getValueFromIdByName(innerObject.id(), "hubs");
        this.authorizationPolicyName = Utils.getValueFromIdByName(innerObject.id(), "authorizationPolicies");
    }

    public AuthorizationPolicyResourceFormat refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAuthorizationPolicies()
                .getWithResponse(resourceGroupName, hubName, authorizationPolicyName, Context.NONE)
                .getValue();
        return this;
    }

    public AuthorizationPolicyResourceFormat refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAuthorizationPolicies()
                .getWithResponse(resourceGroupName, hubName, authorizationPolicyName, context)
                .getValue();
        return this;
    }

    public Response<AuthorizationPolicy> regeneratePrimaryKeyWithResponse(Context context) {
        return serviceManager
            .authorizationPolicies()
            .regeneratePrimaryKeyWithResponse(resourceGroupName, hubName, authorizationPolicyName, context);
    }

    public AuthorizationPolicy regeneratePrimaryKey() {
        return serviceManager
            .authorizationPolicies()
            .regeneratePrimaryKey(resourceGroupName, hubName, authorizationPolicyName);
    }

    public Response<AuthorizationPolicy> regenerateSecondaryKeyWithResponse(Context context) {
        return serviceManager
            .authorizationPolicies()
            .regenerateSecondaryKeyWithResponse(resourceGroupName, hubName, authorizationPolicyName, context);
    }

    public AuthorizationPolicy regenerateSecondaryKey() {
        return serviceManager
            .authorizationPolicies()
            .regenerateSecondaryKey(resourceGroupName, hubName, authorizationPolicyName);
    }

    public AuthorizationPolicyResourceFormatImpl withPermissions(List<PermissionTypes> permissions) {
        this.innerModel().withPermissions(permissions);
        return this;
    }

    public AuthorizationPolicyResourceFormatImpl withPrimaryKey(String primaryKey) {
        this.innerModel().withPrimaryKey(primaryKey);
        return this;
    }

    public AuthorizationPolicyResourceFormatImpl withSecondaryKey(String secondaryKey) {
        this.innerModel().withSecondaryKey(secondaryKey);
        return this;
    }
}
