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

package com.azure.resourcemanager.hardwaresecuritymodules.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hardwaresecuritymodules.fluent.models.DedicatedHsmInner;
import com.azure.resourcemanager.hardwaresecuritymodules.models.DedicatedHsm;
import com.azure.resourcemanager.hardwaresecuritymodules.models.DedicatedHsmPatchParameters;
import com.azure.resourcemanager.hardwaresecuritymodules.models.JsonWebKeyType;
import com.azure.resourcemanager.hardwaresecuritymodules.models.NetworkProfile;
import com.azure.resourcemanager.hardwaresecuritymodules.models.Sku;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class DedicatedHsmImpl implements DedicatedHsm, DedicatedHsm.Definition, DedicatedHsm.Update {
    private DedicatedHsmInner innerObject;

    private final com.azure.resourcemanager.hardwaresecuritymodules.HardwareSecurityModulesManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

    public NetworkProfile networkProfile() {
        return this.innerModel().networkProfile();
    }

    public NetworkProfile managementNetworkProfile() {
        return this.innerModel().managementNetworkProfile();
    }

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

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

    public JsonWebKeyType provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

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

    private com.azure.resourcemanager.hardwaresecuritymodules.HardwareSecurityModulesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String name;

    private DedicatedHsmPatchParameters updateParameters;

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

    public DedicatedHsm create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedHsms()
                .createOrUpdate(resourceGroupName, name, this.innerModel(), Context.NONE);
        return this;
    }

    public DedicatedHsm create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedHsms()
                .createOrUpdate(resourceGroupName, name, this.innerModel(), context);
        return this;
    }

    DedicatedHsmImpl(
        String name, com.azure.resourcemanager.hardwaresecuritymodules.HardwareSecurityModulesManager serviceManager) {
        this.innerObject = new DedicatedHsmInner();
        this.serviceManager = serviceManager;
        this.name = name;
    }

    public DedicatedHsmImpl update() {
        this.updateParameters = new DedicatedHsmPatchParameters();
        return this;
    }

    public DedicatedHsm apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedHsms()
                .update(resourceGroupName, name, updateParameters, Context.NONE);
        return this;
    }

    public DedicatedHsm apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedHsms()
                .update(resourceGroupName, name, updateParameters, context);
        return this;
    }

    DedicatedHsmImpl(
        DedicatedHsmInner innerObject,
        com.azure.resourcemanager.hardwaresecuritymodules.HardwareSecurityModulesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.name = Utils.getValueFromIdByName(innerObject.id(), "dedicatedHSMs");
    }

    public DedicatedHsm refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedHsms()
                .getByResourceGroupWithResponse(resourceGroupName, name, Context.NONE)
                .getValue();
        return this;
    }

    public DedicatedHsm refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedHsms()
                .getByResourceGroupWithResponse(resourceGroupName, name, context)
                .getValue();
        return this;
    }

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

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

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

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

    public DedicatedHsmImpl withZones(List<String> zones) {
        this.innerModel().withZones(zones);
        return this;
    }

    public DedicatedHsmImpl withNetworkProfile(NetworkProfile networkProfile) {
        this.innerModel().withNetworkProfile(networkProfile);
        return this;
    }

    public DedicatedHsmImpl withManagementNetworkProfile(NetworkProfile managementNetworkProfile) {
        this.innerModel().withManagementNetworkProfile(managementNetworkProfile);
        return this;
    }

    public DedicatedHsmImpl withStampId(String stampId) {
        this.innerModel().withStampId(stampId);
        return this;
    }

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