// 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.DedicatedCloudServiceInner;
import com.azure.resourcemanager.vmwarecloudsimple.models.DedicatedCloudService;
import com.azure.resourcemanager.vmwarecloudsimple.models.OnboardingStatus;
import com.azure.resourcemanager.vmwarecloudsimple.models.PatchPayload;
import java.util.Collections;
import java.util.Map;

public final class DedicatedCloudServiceImpl
    implements DedicatedCloudService, DedicatedCloudService.Definition, DedicatedCloudService.Update {
    private DedicatedCloudServiceInner 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 String gatewaySubnet() {
        return this.innerModel().gatewaySubnet();
    }

    public OnboardingStatus isAccountOnboarded() {
        return this.innerModel().isAccountOnboarded();
    }

    public Integer nodes() {
        return this.innerModel().nodes();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String dedicatedCloudServiceName;

    private PatchPayload updateDedicatedCloudServiceRequest;

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

    public DedicatedCloudService create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudServices()
                .createOrUpdateWithResponse(
                    resourceGroupName, dedicatedCloudServiceName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public DedicatedCloudService create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudServices()
                .createOrUpdateWithResponse(resourceGroupName, dedicatedCloudServiceName, this.innerModel(), context)
                .getValue();
        return this;
    }

    DedicatedCloudServiceImpl(
        String name, com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager) {
        this.innerObject = new DedicatedCloudServiceInner();
        this.serviceManager = serviceManager;
        this.dedicatedCloudServiceName = name;
    }

    public DedicatedCloudServiceImpl update() {
        this.updateDedicatedCloudServiceRequest = new PatchPayload();
        return this;
    }

    public DedicatedCloudService apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudServices()
                .updateWithResponse(
                    resourceGroupName, dedicatedCloudServiceName, updateDedicatedCloudServiceRequest, Context.NONE)
                .getValue();
        return this;
    }

    public DedicatedCloudService apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudServices()
                .updateWithResponse(
                    resourceGroupName, dedicatedCloudServiceName, updateDedicatedCloudServiceRequest, context)
                .getValue();
        return this;
    }

    DedicatedCloudServiceImpl(
        DedicatedCloudServiceInner innerObject,
        com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.dedicatedCloudServiceName = Utils.getValueFromIdByName(innerObject.id(), "dedicatedCloudServices");
    }

    public DedicatedCloudService refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudServices()
                .getByResourceGroupWithResponse(resourceGroupName, dedicatedCloudServiceName, Context.NONE)
                .getValue();
        return this;
    }

    public DedicatedCloudService refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDedicatedCloudServices()
                .getByResourceGroupWithResponse(resourceGroupName, dedicatedCloudServiceName, context)
                .getValue();
        return this;
    }

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

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

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

    public DedicatedCloudServiceImpl withGatewaySubnet(String gatewaySubnet) {
        this.innerModel().withGatewaySubnet(gatewaySubnet);
        return this;
    }

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