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

package com.azure.resourcemanager.hanaonazure.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hanaonazure.fluent.models.HanaInstanceInner;
import com.azure.resourcemanager.hanaonazure.models.HanaInstance;
import com.azure.resourcemanager.hanaonazure.models.HanaInstancePowerStateEnum;
import com.azure.resourcemanager.hanaonazure.models.HanaProvisioningStatesEnum;
import com.azure.resourcemanager.hanaonazure.models.HardwareProfile;
import com.azure.resourcemanager.hanaonazure.models.NetworkProfile;
import com.azure.resourcemanager.hanaonazure.models.OSProfile;
import com.azure.resourcemanager.hanaonazure.models.StorageProfile;
import com.azure.resourcemanager.hanaonazure.models.Tags;
import java.util.Collections;
import java.util.Map;

public final class HanaInstanceImpl implements HanaInstance, HanaInstance.Definition, HanaInstance.Update {
    private HanaInstanceInner innerObject;

    private final com.azure.resourcemanager.hanaonazure.HanaManager 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 HardwareProfile hardwareProfile() {
        return this.innerModel().hardwareProfile();
    }

    public StorageProfile storageProfile() {
        return this.innerModel().storageProfile();
    }

    public OSProfile osProfile() {
        return this.innerModel().osProfile();
    }

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

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

    public HanaInstancePowerStateEnum powerState() {
        return this.innerModel().powerState();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.hanaonazure.HanaManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String hanaInstanceName;

    private Tags updateTagsParameter;

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

    public HanaInstance create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHanaInstances()
                .create(resourceGroupName, hanaInstanceName, this.innerModel(), Context.NONE);
        return this;
    }

    public HanaInstance create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHanaInstances()
                .create(resourceGroupName, hanaInstanceName, this.innerModel(), context);
        return this;
    }

    HanaInstanceImpl(String name, com.azure.resourcemanager.hanaonazure.HanaManager serviceManager) {
        this.innerObject = new HanaInstanceInner();
        this.serviceManager = serviceManager;
        this.hanaInstanceName = name;
    }

    public HanaInstanceImpl update() {
        this.updateTagsParameter = new Tags();
        return this;
    }

    public HanaInstance apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHanaInstances()
                .updateWithResponse(resourceGroupName, hanaInstanceName, updateTagsParameter, Context.NONE)
                .getValue();
        return this;
    }

    public HanaInstance apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHanaInstances()
                .updateWithResponse(resourceGroupName, hanaInstanceName, updateTagsParameter, context)
                .getValue();
        return this;
    }

    HanaInstanceImpl(HanaInstanceInner innerObject, com.azure.resourcemanager.hanaonazure.HanaManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.hanaInstanceName = Utils.getValueFromIdByName(innerObject.id(), "hanaInstances");
    }

    public HanaInstance refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHanaInstances()
                .getByResourceGroupWithResponse(resourceGroupName, hanaInstanceName, Context.NONE)
                .getValue();
        return this;
    }

    public HanaInstance refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHanaInstances()
                .getByResourceGroupWithResponse(resourceGroupName, hanaInstanceName, context)
                .getValue();
        return this;
    }

    public void restart() {
        serviceManager.hanaInstances().restart(resourceGroupName, hanaInstanceName);
    }

    public void restart(Context context) {
        serviceManager.hanaInstances().restart(resourceGroupName, hanaInstanceName, context);
    }

    public void start() {
        serviceManager.hanaInstances().start(resourceGroupName, hanaInstanceName);
    }

    public void start(Context context) {
        serviceManager.hanaInstances().start(resourceGroupName, hanaInstanceName, context);
    }

    public void shutdown() {
        serviceManager.hanaInstances().shutdown(resourceGroupName, hanaInstanceName);
    }

    public void shutdown(Context context) {
        serviceManager.hanaInstances().shutdown(resourceGroupName, hanaInstanceName, context);
    }

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

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

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

    public HanaInstanceImpl withHardwareProfile(HardwareProfile hardwareProfile) {
        this.innerModel().withHardwareProfile(hardwareProfile);
        return this;
    }

    public HanaInstanceImpl withStorageProfile(StorageProfile storageProfile) {
        this.innerModel().withStorageProfile(storageProfile);
        return this;
    }

    public HanaInstanceImpl withOsProfile(OSProfile osProfile) {
        this.innerModel().withOsProfile(osProfile);
        return this;
    }

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

    public HanaInstanceImpl withPartnerNodeId(String partnerNodeId) {
        this.innerModel().withPartnerNodeId(partnerNodeId);
        return this;
    }

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