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

package com.azure.resourcemanager.vmwarecloudsimple.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.vmwarecloudsimple.fluent.models.DedicatedCloudNodeInner;
import com.azure.resourcemanager.vmwarecloudsimple.models.DedicatedCloudNode;
import com.azure.resourcemanager.vmwarecloudsimple.models.NodeStatus;
import com.azure.resourcemanager.vmwarecloudsimple.models.PatchPayload;
import com.azure.resourcemanager.vmwarecloudsimple.models.Sku;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;

public final class DedicatedCloudNodeImpl
    implements DedicatedCloudNode, DedicatedCloudNode.Definition, DedicatedCloudNode.Update {
    private DedicatedCloudNodeInner innerObject;

    private final com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager 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 String availabilityZoneId() {
        return this.innerModel().availabilityZoneId();
    }

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

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

    public OffsetDateTime created() {
        return this.innerModel().created();
    }

    public int nodesCount() {
        return this.innerModel().nodesCount();
    }

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

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

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

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

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

    public UUID purchaseId() {
        return this.innerModel().purchaseId();
    }

    public NodeStatus status() {
        return this.innerModel().status();
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String dedicatedCloudNodeName;

    private String createReferer;

    private PatchPayload updateDedicatedCloudNodeRequest;

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

    public DedicatedCloudNode create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudNodes()
                .createOrUpdate(
                    resourceGroupName, createReferer, dedicatedCloudNodeName, this.innerModel(), Context.NONE);
        return this;
    }

    public DedicatedCloudNode create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudNodes()
                .createOrUpdate(resourceGroupName, createReferer, dedicatedCloudNodeName, this.innerModel(), context);
        return this;
    }

    DedicatedCloudNodeImpl(
        String name, com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager) {
        this.innerObject = new DedicatedCloudNodeInner();
        this.serviceManager = serviceManager;
        this.dedicatedCloudNodeName = name;
        this.createReferer = null;
    }

    public DedicatedCloudNodeImpl update() {
        this.updateDedicatedCloudNodeRequest = new PatchPayload();
        return this;
    }

    public DedicatedCloudNode apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudNodes()
                .updateWithResponse(
                    resourceGroupName, dedicatedCloudNodeName, updateDedicatedCloudNodeRequest, Context.NONE)
                .getValue();
        return this;
    }

    public DedicatedCloudNode apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudNodes()
                .updateWithResponse(resourceGroupName, dedicatedCloudNodeName, updateDedicatedCloudNodeRequest, context)
                .getValue();
        return this;
    }

    DedicatedCloudNodeImpl(
        DedicatedCloudNodeInner innerObject,
        com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.dedicatedCloudNodeName = Utils.getValueFromIdByName(innerObject.id(), "dedicatedCloudNodes");
    }

    public DedicatedCloudNode refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudNodes()
                .getByResourceGroupWithResponse(resourceGroupName, dedicatedCloudNodeName, Context.NONE)
                .getValue();
        return this;
    }

    public DedicatedCloudNode refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudNodes()
                .getByResourceGroupWithResponse(resourceGroupName, dedicatedCloudNodeName, context)
                .getValue();
        return this;
    }

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

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

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

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

    public DedicatedCloudNodeImpl withAvailabilityZoneId(String availabilityZoneId) {
        this.innerModel().withAvailabilityZoneId(availabilityZoneId);
        return this;
    }

    public DedicatedCloudNodeImpl withNodesCount(int nodesCount) {
        this.innerModel().withNodesCount(nodesCount);
        return this;
    }

    public DedicatedCloudNodeImpl withPlacementGroupId(String placementGroupId) {
        this.innerModel().withPlacementGroupId(placementGroupId);
        return this;
    }

    public DedicatedCloudNodeImpl withPurchaseId(UUID purchaseId) {
        this.innerModel().withPurchaseId(purchaseId);
        return this;
    }

    public DedicatedCloudNodeImpl withIdPropertiesId(String idPropertiesId) {
        this.innerModel().withIdPropertiesId(idPropertiesId);
        return this;
    }

    public DedicatedCloudNodeImpl withNamePropertiesName(String namePropertiesName) {
        this.innerModel().withNamePropertiesName(namePropertiesName);
        return this;
    }

    public DedicatedCloudNodeImpl withReferer(String referer) {
        this.createReferer = referer;
        return this;
    }

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