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

package azure.resourcemanager.multiservicesharedmodels.combined.implementation;

import azure.resourcemanager.multiservicesharedmodels.combined.fluent.models.VirtualMachineInner;
import azure.resourcemanager.multiservicesharedmodels.combined.models.VirtualMachine;
import azure.resourcemanager.multiservicesharedmodels.combined.models.VirtualMachineProperties;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import java.util.Collections;
import java.util.Map;

public final class VirtualMachineImpl implements VirtualMachine, VirtualMachine.Definition, VirtualMachine.Update {
    private VirtualMachineInner innerObject;

    private final azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager 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 VirtualMachineProperties properties() {
        return this.innerModel().properties();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String vmName;

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

    public VirtualMachine create() {
        this.innerObject = serviceManager.serviceClient()
            .getVirtualMachines()
            .createOrUpdate(resourceGroupName, vmName, this.innerModel(), Context.NONE);
        return this;
    }

    public VirtualMachine create(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getVirtualMachines()
            .createOrUpdate(resourceGroupName, vmName, this.innerModel(), context);
        return this;
    }

    VirtualMachineImpl(String name,
        azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager serviceManager) {
        this.innerObject = new VirtualMachineInner();
        this.serviceManager = serviceManager;
        this.vmName = name;
    }

    public VirtualMachineImpl update() {
        return this;
    }

    public VirtualMachine apply() {
        this.innerObject = serviceManager.serviceClient()
            .getVirtualMachines()
            .createOrUpdate(resourceGroupName, vmName, this.innerModel(), Context.NONE);
        return this;
    }

    public VirtualMachine apply(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getVirtualMachines()
            .createOrUpdate(resourceGroupName, vmName, this.innerModel(), context);
        return this;
    }

    VirtualMachineImpl(VirtualMachineInner innerObject,
        azure.resourcemanager.multiservicesharedmodels.combined.CombinedManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.vmName = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "virtualMachinesShared");
    }

    public VirtualMachine refresh() {
        this.innerObject = serviceManager.serviceClient()
            .getVirtualMachines()
            .getByResourceGroupWithResponse(resourceGroupName, vmName, Context.NONE)
            .getValue();
        return this;
    }

    public VirtualMachine refresh(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getVirtualMachines()
            .getByResourceGroupWithResponse(resourceGroupName, vmName, context)
            .getValue();
        return this;
    }

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

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

    public VirtualMachineImpl withTags(Map<String, String> tags) {
        this.innerModel().withTags(tags);
        return this;
    }

    public VirtualMachineImpl withProperties(VirtualMachineProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }
}
