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

package com.azure.resourcemanager.networkcloud.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.networkcloud.fluent.models.VirtualMachineInner;
import com.azure.resourcemanager.networkcloud.models.ExtendedLocation;
import com.azure.resourcemanager.networkcloud.models.ImageRepositoryCredentials;
import com.azure.resourcemanager.networkcloud.models.NetworkAttachment;
import com.azure.resourcemanager.networkcloud.models.SshPublicKey;
import com.azure.resourcemanager.networkcloud.models.StorageProfile;
import com.azure.resourcemanager.networkcloud.models.VirtualMachine;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineBootMethod;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineDetailedStatus;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineDeviceModelType;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineIsolateEmulatorThread;
import com.azure.resourcemanager.networkcloud.models.VirtualMachinePatchParameters;
import com.azure.resourcemanager.networkcloud.models.VirtualMachinePlacementHint;
import com.azure.resourcemanager.networkcloud.models.VirtualMachinePowerOffParameters;
import com.azure.resourcemanager.networkcloud.models.VirtualMachinePowerState;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineProvisioningState;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineVirtioInterfaceType;
import com.azure.resourcemanager.networkcloud.models.VirtualMachineVolumeParameters;
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.networkcloud.NetworkCloudManager 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 adminUsername() {
        return this.innerModel().adminUsername();
    }

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

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

    public VirtualMachineBootMethod bootMethod() {
        return this.innerModel().bootMethod();
    }

    public NetworkAttachment cloudServicesNetworkAttachment() {
        return this.innerModel().cloudServicesNetworkAttachment();
    }

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

    public long cpuCores() {
        return this.innerModel().cpuCores();
    }

    public VirtualMachineDetailedStatus detailedStatus() {
        return this.innerModel().detailedStatus();
    }

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

    public VirtualMachineIsolateEmulatorThread isolateEmulatorThread() {
        return this.innerModel().isolateEmulatorThread();
    }

    public long memorySizeGB() {
        return this.innerModel().memorySizeGB();
    }

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

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

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

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

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

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

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

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

    public VirtualMachineVirtioInterfaceType virtioInterface() {
        return this.innerModel().virtioInterface();
    }

    public VirtualMachineDeviceModelType vmDeviceModel() {
        return this.innerModel().vmDeviceModel();
    }

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

    public ImageRepositoryCredentials vmImageRepositoryCredentials() {
        return this.innerModel().vmImageRepositoryCredentials();
    }

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

    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.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String virtualMachineName;

    private VirtualMachinePatchParameters updateVirtualMachineUpdateParameters;

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

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

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

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

    public VirtualMachineImpl update() {
        this.updateVirtualMachineUpdateParameters = new VirtualMachinePatchParameters();
        return this;
    }

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

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

    VirtualMachineImpl(
        VirtualMachineInner innerObject, com.azure.resourcemanager.networkcloud.NetworkCloudManager 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 attachVolume(VirtualMachineVolumeParameters virtualMachineAttachVolumeParameters) {
        serviceManager
            .virtualMachines()
            .attachVolume(resourceGroupName, virtualMachineName, virtualMachineAttachVolumeParameters);
    }

    public void attachVolume(VirtualMachineVolumeParameters virtualMachineAttachVolumeParameters, Context context) {
        serviceManager
            .virtualMachines()
            .attachVolume(resourceGroupName, virtualMachineName, virtualMachineAttachVolumeParameters, context);
    }

    public void detachVolume(VirtualMachineVolumeParameters virtualMachineDetachVolumeParameters) {
        serviceManager
            .virtualMachines()
            .detachVolume(resourceGroupName, virtualMachineName, virtualMachineDetachVolumeParameters);
    }

    public void detachVolume(VirtualMachineVolumeParameters virtualMachineDetachVolumeParameters, Context context) {
        serviceManager
            .virtualMachines()
            .detachVolume(resourceGroupName, virtualMachineName, virtualMachineDetachVolumeParameters, context);
    }

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

    public void powerOff(VirtualMachinePowerOffParameters virtualMachinePowerOffParameters, Context context) {
        serviceManager
            .virtualMachines()
            .powerOff(resourceGroupName, virtualMachineName, virtualMachinePowerOffParameters, context);
    }

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

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

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

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

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

    public void start(Context context) {
        serviceManager.virtualMachines().start(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 withExtendedLocation(ExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

    public VirtualMachineImpl withAdminUsername(String adminUsername) {
        this.innerModel().withAdminUsername(adminUsername);
        return this;
    }

    public VirtualMachineImpl withCloudServicesNetworkAttachment(NetworkAttachment cloudServicesNetworkAttachment) {
        this.innerModel().withCloudServicesNetworkAttachment(cloudServicesNetworkAttachment);
        return this;
    }

    public VirtualMachineImpl withCpuCores(long cpuCores) {
        this.innerModel().withCpuCores(cpuCores);
        return this;
    }

    public VirtualMachineImpl withMemorySizeGB(long memorySizeGB) {
        this.innerModel().withMemorySizeGB(memorySizeGB);
        return this;
    }

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

    public VirtualMachineImpl withVmImage(String vmImage) {
        this.innerModel().withVmImage(vmImage);
        return this;
    }

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

    public VirtualMachineImpl withBootMethod(VirtualMachineBootMethod bootMethod) {
        this.innerModel().withBootMethod(bootMethod);
        return this;
    }

    public VirtualMachineImpl withIsolateEmulatorThread(VirtualMachineIsolateEmulatorThread isolateEmulatorThread) {
        this.innerModel().withIsolateEmulatorThread(isolateEmulatorThread);
        return this;
    }

    public VirtualMachineImpl withNetworkAttachments(List<NetworkAttachment> networkAttachments) {
        this.innerModel().withNetworkAttachments(networkAttachments);
        return this;
    }

    public VirtualMachineImpl withNetworkData(String networkData) {
        this.innerModel().withNetworkData(networkData);
        return this;
    }

    public VirtualMachineImpl withPlacementHints(List<VirtualMachinePlacementHint> placementHints) {
        this.innerModel().withPlacementHints(placementHints);
        return this;
    }

    public VirtualMachineImpl withSshPublicKeys(List<SshPublicKey> sshPublicKeys) {
        this.innerModel().withSshPublicKeys(sshPublicKeys);
        return this;
    }

    public VirtualMachineImpl withUserData(String userData) {
        this.innerModel().withUserData(userData);
        return this;
    }

    public VirtualMachineImpl withVirtioInterface(VirtualMachineVirtioInterfaceType virtioInterface) {
        this.innerModel().withVirtioInterface(virtioInterface);
        return this;
    }

    public VirtualMachineImpl withVmDeviceModel(VirtualMachineDeviceModelType vmDeviceModel) {
        this.innerModel().withVmDeviceModel(vmDeviceModel);
        return this;
    }

    public VirtualMachineImpl withVmImageRepositoryCredentials(
        ImageRepositoryCredentials vmImageRepositoryCredentials) {
        if (isInCreateMode()) {
            this.innerModel().withVmImageRepositoryCredentials(vmImageRepositoryCredentials);
            return this;
        } else {
            this.updateVirtualMachineUpdateParameters.withVmImageRepositoryCredentials(vmImageRepositoryCredentials);
            return this;
        }
    }

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