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

package com.azure.resourcemanager.scvmm.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.scvmm.fluent.models.CloudInner;
import com.azure.resourcemanager.scvmm.models.Cloud;
import com.azure.resourcemanager.scvmm.models.CloudCapacity;
import com.azure.resourcemanager.scvmm.models.ExtendedLocation;
import com.azure.resourcemanager.scvmm.models.ResourcePatch;
import com.azure.resourcemanager.scvmm.models.StorageQoSPolicy;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class CloudImpl implements Cloud, Cloud.Definition, Cloud.Update {
    private CloudInner innerObject;

    private final com.azure.resourcemanager.scvmm.ScvmmManager 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 ExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

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

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

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

    public CloudCapacity cloudCapacity() {
        return this.innerModel().cloudCapacity();
    }

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

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

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

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

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

    private com.azure.resourcemanager.scvmm.ScvmmManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String cloudName;

    private ResourcePatch updateBody;

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

    public Cloud create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClouds()
                .createOrUpdate(resourceGroupName, cloudName, this.innerModel(), Context.NONE);
        return this;
    }

    public Cloud create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClouds()
                .createOrUpdate(resourceGroupName, cloudName, this.innerModel(), context);
        return this;
    }

    CloudImpl(String name, com.azure.resourcemanager.scvmm.ScvmmManager serviceManager) {
        this.innerObject = new CloudInner();
        this.serviceManager = serviceManager;
        this.cloudName = name;
    }

    public CloudImpl update() {
        this.updateBody = new ResourcePatch();
        return this;
    }

    public Cloud apply() {
        this.innerObject =
            serviceManager.serviceClient().getClouds().update(resourceGroupName, cloudName, updateBody, Context.NONE);
        return this;
    }

    public Cloud apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getClouds().update(resourceGroupName, cloudName, updateBody, context);
        return this;
    }

    CloudImpl(CloudInner innerObject, com.azure.resourcemanager.scvmm.ScvmmManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.cloudName = Utils.getValueFromIdByName(innerObject.id(), "clouds");
    }

    public Cloud refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClouds()
                .getByResourceGroupWithResponse(resourceGroupName, cloudName, Context.NONE)
                .getValue();
        return this;
    }

    public Cloud refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClouds()
                .getByResourceGroupWithResponse(resourceGroupName, cloudName, context)
                .getValue();
        return this;
    }

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

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

    public CloudImpl withExtendedLocation(ExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

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

    public CloudImpl withInventoryItemId(String inventoryItemId) {
        this.innerModel().withInventoryItemId(inventoryItemId);
        return this;
    }

    public CloudImpl withUuid(String uuid) {
        this.innerModel().withUuid(uuid);
        return this;
    }

    public CloudImpl withVmmServerId(String vmmServerId) {
        this.innerModel().withVmmServerId(vmmServerId);
        return this;
    }

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