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

package com.azure.resourcemanager.recoveryservices.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.recoveryservices.fluent.models.VaultInner;
import com.azure.resourcemanager.recoveryservices.models.IdentityData;
import com.azure.resourcemanager.recoveryservices.models.PatchVault;
import com.azure.resourcemanager.recoveryservices.models.Sku;
import com.azure.resourcemanager.recoveryservices.models.Vault;
import com.azure.resourcemanager.recoveryservices.models.VaultProperties;
import java.util.Collections;
import java.util.Map;

public final class VaultImpl implements Vault, Vault.Definition, Vault.Update {
    private VaultInner innerObject;

    private final com.azure.resourcemanager.recoveryservices.RecoveryServicesManager 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 IdentityData identity() {
        return this.innerModel().identity();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.recoveryservices.RecoveryServicesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String vaultName;

    private PatchVault updateVault;

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

    public Vault create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVaults()
                .createOrUpdate(resourceGroupName, vaultName, this.innerModel(), Context.NONE);
        return this;
    }

    public Vault create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVaults()
                .createOrUpdate(resourceGroupName, vaultName, this.innerModel(), context);
        return this;
    }

    VaultImpl(String name, com.azure.resourcemanager.recoveryservices.RecoveryServicesManager serviceManager) {
        this.innerObject = new VaultInner();
        this.serviceManager = serviceManager;
        this.vaultName = name;
    }

    public VaultImpl update() {
        this.updateVault = new PatchVault();
        return this;
    }

    public Vault apply() {
        this.innerObject =
            serviceManager.serviceClient().getVaults().update(resourceGroupName, vaultName, updateVault, Context.NONE);
        return this;
    }

    public Vault apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getVaults().update(resourceGroupName, vaultName, updateVault, context);
        return this;
    }

    VaultImpl(
        VaultInner innerObject, com.azure.resourcemanager.recoveryservices.RecoveryServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.vaultName = Utils.getValueFromIdByName(innerObject.id(), "vaults");
    }

    public Vault refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVaults()
                .getByResourceGroupWithResponse(resourceGroupName, vaultName, Context.NONE)
                .getValue();
        return this;
    }

    public Vault refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVaults()
                .getByResourceGroupWithResponse(resourceGroupName, vaultName, context)
                .getValue();
        return this;
    }

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

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

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

    public VaultImpl withIdentity(IdentityData identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateVault.withIdentity(identity);
            return this;
        }
    }

    public VaultImpl withProperties(VaultProperties properties) {
        if (isInCreateMode()) {
            this.innerModel().withProperties(properties);
            return this;
        } else {
            this.updateVault.withProperties(properties);
            return this;
        }
    }

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

    public VaultImpl withEtag(String etag) {
        if (isInCreateMode()) {
            this.innerModel().withEtag(etag);
            return this;
        } else {
            this.updateVault.withEtag(etag);
            return this;
        }
    }

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