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

package com.azure.resourcemanager.connectedvmware.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.connectedvmware.fluent.models.VirtualMachineInner;
import com.azure.resourcemanager.connectedvmware.models.ExtendedLocation;
import com.azure.resourcemanager.connectedvmware.models.FirmwareType;
import com.azure.resourcemanager.connectedvmware.models.GuestAgentProfile;
import com.azure.resourcemanager.connectedvmware.models.HardwareProfile;
import com.azure.resourcemanager.connectedvmware.models.Identity;
import com.azure.resourcemanager.connectedvmware.models.NetworkProfile;
import com.azure.resourcemanager.connectedvmware.models.NetworkProfileUpdate;
import com.azure.resourcemanager.connectedvmware.models.OsProfile;
import com.azure.resourcemanager.connectedvmware.models.OsProfileUpdate;
import com.azure.resourcemanager.connectedvmware.models.PlacementProfile;
import com.azure.resourcemanager.connectedvmware.models.ResourceStatus;
import com.azure.resourcemanager.connectedvmware.models.SecurityProfile;
import com.azure.resourcemanager.connectedvmware.models.StopVirtualMachineOptions;
import com.azure.resourcemanager.connectedvmware.models.StorageProfile;
import com.azure.resourcemanager.connectedvmware.models.StorageProfileUpdate;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachine;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachineUpdate;
import java.util.Collections;
import java.util.List;
import java.util.Map;

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

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

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

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

    public Identity identity() {
        return this.innerModel().identity();
    }

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

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

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

    public PlacementProfile placementProfile() {
        return this.innerModel().placementProfile();
    }

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

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

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

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

    public GuestAgentProfile guestAgentProfile() {
        return this.innerModel().guestAgentProfile();
    }

    public SecurityProfile securityProfile() {
        return this.innerModel().securityProfile();
    }

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

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

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

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

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

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

    public FirmwareType firmwareType() {
        return this.innerModel().firmwareType();
    }

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

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

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

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

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

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

    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 com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String virtualMachineName;

    private VirtualMachineUpdate updateBody;

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

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

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

    VirtualMachineImpl(String name, com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerObject = new VirtualMachineInner();
        this.serviceManager = serviceManager;
        this.virtualMachineName = name;
    }

    public VirtualMachineImpl update() {
        this.updateBody = new VirtualMachineUpdate();
        return this;
    }

    public VirtualMachine apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachines()
                .update(resourceGroupName, virtualMachineName, updateBody, Context.NONE);
        return this;
    }

    public VirtualMachine apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachines()
                .update(resourceGroupName, virtualMachineName, updateBody, context);
        return this;
    }

    VirtualMachineImpl(
        VirtualMachineInner innerObject,
        com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.virtualMachineName = Utils.getValueFromIdByName(innerObject.id(), "virtualMachines");
    }

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

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

    public void stop(StopVirtualMachineOptions body) {
        serviceManager.virtualMachines().stop(resourceGroupName, virtualMachineName, body);
    }

    public void stop() {
        serviceManager.virtualMachines().stop(resourceGroupName, virtualMachineName);
    }

    public void stop(StopVirtualMachineOptions body, Context context) {
        serviceManager.virtualMachines().stop(resourceGroupName, virtualMachineName, body, context);
    }

    public void start() {
        serviceManager.virtualMachines().start(resourceGroupName, virtualMachineName);
    }

    public void start(Context context) {
        serviceManager.virtualMachines().start(resourceGroupName, virtualMachineName, context);
    }

    public void restart() {
        serviceManager.virtualMachines().restart(resourceGroupName, virtualMachineName);
    }

    public void restart(Context context) {
        serviceManager.virtualMachines().restart(resourceGroupName, virtualMachineName, context);
    }

    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) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updateBody.withTags(tags);
            return this;
        }
    }

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

    public VirtualMachineImpl withKind(String kind) {
        this.innerModel().withKind(kind);
        return this;
    }

    public VirtualMachineImpl withIdentity(Identity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateBody.withIdentity(identity);
            return this;
        }
    }

    public VirtualMachineImpl withResourcePoolId(String resourcePoolId) {
        this.innerModel().withResourcePoolId(resourcePoolId);
        return this;
    }

    public VirtualMachineImpl withTemplateId(String templateId) {
        this.innerModel().withTemplateId(templateId);
        return this;
    }

    public VirtualMachineImpl withVCenterId(String vCenterId) {
        this.innerModel().withVCenterId(vCenterId);
        return this;
    }

    public VirtualMachineImpl withPlacementProfile(PlacementProfile placementProfile) {
        this.innerModel().withPlacementProfile(placementProfile);
        return this;
    }

    public VirtualMachineImpl withOsProfile(OsProfile osProfile) {
        this.innerModel().withOsProfile(osProfile);
        return this;
    }

    public VirtualMachineImpl withHardwareProfile(HardwareProfile hardwareProfile) {
        if (isInCreateMode()) {
            this.innerModel().withHardwareProfile(hardwareProfile);
            return this;
        } else {
            this.updateBody.withHardwareProfile(hardwareProfile);
            return this;
        }
    }

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

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

    public VirtualMachineImpl withGuestAgentProfile(GuestAgentProfile guestAgentProfile) {
        this.innerModel().withGuestAgentProfile(guestAgentProfile);
        return this;
    }

    public VirtualMachineImpl withSecurityProfile(SecurityProfile securityProfile) {
        this.innerModel().withSecurityProfile(securityProfile);
        return this;
    }

    public VirtualMachineImpl withMoRefId(String moRefId) {
        this.innerModel().withMoRefId(moRefId);
        return this;
    }

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

    public VirtualMachineImpl withSmbiosUuid(String smbiosUuid) {
        this.innerModel().withSmbiosUuid(smbiosUuid);
        return this;
    }

    public VirtualMachineImpl withFirmwareType(FirmwareType firmwareType) {
        this.innerModel().withFirmwareType(firmwareType);
        return this;
    }

    public VirtualMachineImpl withOsProfile(OsProfileUpdate osProfile) {
        this.updateBody.withOsProfile(osProfile);
        return this;
    }

    public VirtualMachineImpl withStorageProfile(StorageProfileUpdate storageProfile) {
        this.updateBody.withStorageProfile(storageProfile);
        return this;
    }

    public VirtualMachineImpl withNetworkProfile(NetworkProfileUpdate networkProfile) {
        this.updateBody.withNetworkProfile(networkProfile);
        return this;
    }

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