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

package com.azure.resourcemanager.maps.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.maps.fluent.models.MapsAccountInner;
import com.azure.resourcemanager.maps.fluent.models.MapsAccountProperties;
import com.azure.resourcemanager.maps.models.Kind;
import com.azure.resourcemanager.maps.models.MapsAccount;
import com.azure.resourcemanager.maps.models.MapsAccountKeys;
import com.azure.resourcemanager.maps.models.MapsAccountUpdateParameters;
import com.azure.resourcemanager.maps.models.MapsKeySpecification;
import com.azure.resourcemanager.maps.models.Sku;
import java.util.Collections;
import java.util.Map;

public final class MapsAccountImpl implements MapsAccount, MapsAccount.Definition, MapsAccount.Update {
    private MapsAccountInner innerObject;

    private final com.azure.resourcemanager.maps.AzureMapsManager 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 Sku sku() {
        return this.innerModel().sku();
    }

    public Kind kind() {
        return this.innerModel().kind();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

    public MapsAccountProperties properties() {
        return this.innerModel().properties();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.maps.AzureMapsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private MapsAccountUpdateParameters updateMapsAccountUpdateParameters;

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

    public MapsAccount create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .createOrUpdateWithResponse(resourceGroupName, accountName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public MapsAccount create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .createOrUpdateWithResponse(resourceGroupName, accountName, this.innerModel(), context)
                .getValue();
        return this;
    }

    MapsAccountImpl(String name, com.azure.resourcemanager.maps.AzureMapsManager serviceManager) {
        this.innerObject = new MapsAccountInner();
        this.serviceManager = serviceManager;
        this.accountName = name;
    }

    public MapsAccountImpl update() {
        this.updateMapsAccountUpdateParameters = new MapsAccountUpdateParameters();
        return this;
    }

    public MapsAccount apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .updateWithResponse(resourceGroupName, accountName, updateMapsAccountUpdateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public MapsAccount apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .updateWithResponse(resourceGroupName, accountName, updateMapsAccountUpdateParameters, context)
                .getValue();
        return this;
    }

    MapsAccountImpl(MapsAccountInner innerObject, com.azure.resourcemanager.maps.AzureMapsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "accounts");
    }

    public MapsAccount refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .getByResourceGroupWithResponse(resourceGroupName, accountName, Context.NONE)
                .getValue();
        return this;
    }

    public MapsAccount refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .getByResourceGroupWithResponse(resourceGroupName, accountName, context)
                .getValue();
        return this;
    }

    public Response<MapsAccountKeys> listKeysWithResponse(Context context) {
        return serviceManager.accounts().listKeysWithResponse(resourceGroupName, accountName, context);
    }

    public MapsAccountKeys listKeys() {
        return serviceManager.accounts().listKeys(resourceGroupName, accountName);
    }

    public Response<MapsAccountKeys> regenerateKeysWithResponse(
        MapsKeySpecification keySpecification, Context context) {
        return serviceManager
            .accounts()
            .regenerateKeysWithResponse(resourceGroupName, accountName, keySpecification, context);
    }

    public MapsAccountKeys regenerateKeys(MapsKeySpecification keySpecification) {
        return serviceManager.accounts().regenerateKeys(resourceGroupName, accountName, keySpecification);
    }

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

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

    public MapsAccountImpl withSku(Sku sku) {
        if (isInCreateMode()) {
            this.innerModel().withSku(sku);
            return this;
        } else {
            this.updateMapsAccountUpdateParameters.withSku(sku);
            return this;
        }
    }

    public MapsAccountImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updateMapsAccountUpdateParameters.withTags(tags);
            return this;
        }
    }

    public MapsAccountImpl withKind(Kind kind) {
        if (isInCreateMode()) {
            this.innerModel().withKind(kind);
            return this;
        } else {
            this.updateMapsAccountUpdateParameters.withKind(kind);
            return this;
        }
    }

    public MapsAccountImpl withProperties(MapsAccountProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public MapsAccountImpl withDisableLocalAuth(Boolean disableLocalAuth) {
        this.updateMapsAccountUpdateParameters.withDisableLocalAuth(disableLocalAuth);
        return this;
    }

    private boolean isInCreateMode() {
        return this.innerModel().id() == null;
    }
}
