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

package com.azure.resourcemanager.logic.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.logic.fluent.models.IntegrationAccountInner;
import com.azure.resourcemanager.logic.models.CallbackUrl;
import com.azure.resourcemanager.logic.models.GetCallbackUrlParameters;
import com.azure.resourcemanager.logic.models.IntegrationAccount;
import com.azure.resourcemanager.logic.models.IntegrationAccountSku;
import com.azure.resourcemanager.logic.models.KeyVaultKey;
import com.azure.resourcemanager.logic.models.ListKeyVaultKeysDefinition;
import com.azure.resourcemanager.logic.models.RegenerateActionParameter;
import com.azure.resourcemanager.logic.models.ResourceReference;
import com.azure.resourcemanager.logic.models.TrackingEventsDefinition;
import com.azure.resourcemanager.logic.models.WorkflowState;
import java.util.Collections;
import java.util.Map;

public final class IntegrationAccountImpl
    implements IntegrationAccount, IntegrationAccount.Definition, IntegrationAccount.Update {
    private IntegrationAccountInner innerObject;

    private final com.azure.resourcemanager.logic.LogicManager serviceManager;

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

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

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

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

    public Map<String, String> tags() {
        Map<String, String> inner = this.innerModel().tags();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public IntegrationAccountSku sku() {
        return this.innerModel().sku();
    }

    public ResourceReference integrationServiceEnvironment() {
        return this.innerModel().integrationServiceEnvironment();
    }

    public WorkflowState state() {
        return this.innerModel().state();
    }

    public Region region() {
        return Region.fromName(this.regionName());
    }

    public String regionName() {
        return this.location();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.logic.LogicManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String integrationAccountName;

    public IntegrationAccountImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public IntegrationAccount create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIntegrationAccounts()
                .createOrUpdateWithResponse(resourceGroupName, integrationAccountName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public IntegrationAccount create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIntegrationAccounts()
                .createOrUpdateWithResponse(resourceGroupName, integrationAccountName, this.innerModel(), context)
                .getValue();
        return this;
    }

    IntegrationAccountImpl(String name, com.azure.resourcemanager.logic.LogicManager serviceManager) {
        this.innerObject = new IntegrationAccountInner();
        this.serviceManager = serviceManager;
        this.integrationAccountName = name;
    }

    public IntegrationAccountImpl update() {
        return this;
    }

    public IntegrationAccount apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIntegrationAccounts()
                .updateWithResponse(resourceGroupName, integrationAccountName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public IntegrationAccount apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIntegrationAccounts()
                .updateWithResponse(resourceGroupName, integrationAccountName, this.innerModel(), context)
                .getValue();
        return this;
    }

    IntegrationAccountImpl(
        IntegrationAccountInner innerObject, com.azure.resourcemanager.logic.LogicManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.integrationAccountName = Utils.getValueFromIdByName(innerObject.id(), "integrationAccounts");
    }

    public IntegrationAccount refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIntegrationAccounts()
                .getByResourceGroupWithResponse(resourceGroupName, integrationAccountName, Context.NONE)
                .getValue();
        return this;
    }

    public IntegrationAccount refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIntegrationAccounts()
                .getByResourceGroupWithResponse(resourceGroupName, integrationAccountName, context)
                .getValue();
        return this;
    }

    public Response<CallbackUrl> listCallbackUrlWithResponse(GetCallbackUrlParameters parameters, Context context) {
        return serviceManager
            .integrationAccounts()
            .listCallbackUrlWithResponse(resourceGroupName, integrationAccountName, parameters, context);
    }

    public CallbackUrl listCallbackUrl(GetCallbackUrlParameters parameters) {
        return serviceManager
            .integrationAccounts()
            .listCallbackUrl(resourceGroupName, integrationAccountName, parameters);
    }

    public PagedIterable<KeyVaultKey> listKeyVaultKeys(ListKeyVaultKeysDefinition listKeyVaultKeys) {
        return serviceManager
            .integrationAccounts()
            .listKeyVaultKeys(resourceGroupName, integrationAccountName, listKeyVaultKeys);
    }

    public PagedIterable<KeyVaultKey> listKeyVaultKeys(ListKeyVaultKeysDefinition listKeyVaultKeys, Context context) {
        return serviceManager
            .integrationAccounts()
            .listKeyVaultKeys(resourceGroupName, integrationAccountName, listKeyVaultKeys, context);
    }

    public Response<Void> logTrackingEventsWithResponse(TrackingEventsDefinition logTrackingEvents, Context context) {
        return serviceManager
            .integrationAccounts()
            .logTrackingEventsWithResponse(resourceGroupName, integrationAccountName, logTrackingEvents, context);
    }

    public void logTrackingEvents(TrackingEventsDefinition logTrackingEvents) {
        serviceManager
            .integrationAccounts()
            .logTrackingEvents(resourceGroupName, integrationAccountName, logTrackingEvents);
    }

    public Response<IntegrationAccount> regenerateAccessKeyWithResponse(
        RegenerateActionParameter regenerateAccessKey, Context context) {
        return serviceManager
            .integrationAccounts()
            .regenerateAccessKeyWithResponse(resourceGroupName, integrationAccountName, regenerateAccessKey, context);
    }

    public IntegrationAccount regenerateAccessKey(RegenerateActionParameter regenerateAccessKey) {
        return serviceManager
            .integrationAccounts()
            .regenerateAccessKey(resourceGroupName, integrationAccountName, regenerateAccessKey);
    }

    public IntegrationAccountImpl withRegion(Region location) {
        this.innerModel().withLocation(location.toString());
        return this;
    }

    public IntegrationAccountImpl withRegion(String location) {
        this.innerModel().withLocation(location);
        return this;
    }

    public IntegrationAccountImpl withTags(Map<String, String> tags) {
        this.innerModel().withTags(tags);
        return this;
    }

    public IntegrationAccountImpl withSku(IntegrationAccountSku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public IntegrationAccountImpl withIntegrationServiceEnvironment(ResourceReference integrationServiceEnvironment) {
        this.innerModel().withIntegrationServiceEnvironment(integrationServiceEnvironment);
        return this;
    }

    public IntegrationAccountImpl withState(WorkflowState state) {
        this.innerModel().withState(state);
        return this;
    }
}
