// 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.ResourcePoolInner;
import com.azure.resourcemanager.vmwarecloudsimple.fluent.models.VirtualMachineInner;
import com.azure.resourcemanager.vmwarecloudsimple.fluent.models.VirtualNicInner;
import com.azure.resourcemanager.vmwarecloudsimple.models.GuestOSCustomization;
import com.azure.resourcemanager.vmwarecloudsimple.models.GuestOSType;
import com.azure.resourcemanager.vmwarecloudsimple.models.PatchPayload;
import com.azure.resourcemanager.vmwarecloudsimple.models.ResourcePool;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualDisk;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualDiskController;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualMachine;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualMachineStatus;
import com.azure.resourcemanager.vmwarecloudsimple.models.VirtualNic;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class VirtualMachineImpl implements VirtualMachine, VirtualMachine.Definition, VirtualMachine.Update {
    private VirtualMachineInner 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 int amountOfRam() {
        return this.innerModel().amountOfRam();
    }

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

    public GuestOSCustomization customization() {
        return this.innerModel().customization();
    }

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

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

    public Boolean exposeToGuestVM() {
        return this.innerModel().exposeToGuestVM();
    }

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

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

    public GuestOSType guestOSType() {
        return this.innerModel().guestOSType();
    }

    public List<VirtualNic> nics() {
        List<VirtualNicInner> inner = this.innerModel().nics();
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new VirtualNicImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

    public int numberOfCores() {
        return this.innerModel().numberOfCores();
    }

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

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

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

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

    public ResourcePool resourcePool() {
        ResourcePoolInner inner = this.innerModel().resourcePool();
        if (inner != null) {
            return new ResourcePoolImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public VirtualMachineStatus status() {
        return this.innerModel().status();
    }

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

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

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

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

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

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

    private String resourceGroupName;

    private String virtualMachineName;

    private String createReferer;

    private PatchPayload updateVirtualMachineRequest;

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

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

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

    VirtualMachineImpl(
        String name, com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager serviceManager) {
        this.innerObject = new VirtualMachineInner();
        this.serviceManager = serviceManager;
        this.virtualMachineName = name;
        this.createReferer = null;
    }

    public VirtualMachineImpl update() {
        this.updateVirtualMachineRequest = new PatchPayload();
        return this;
    }

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

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

    VirtualMachineImpl(
        VirtualMachineInner innerObject,
        com.azure.resourcemanager.vmwarecloudsimple.VMwareCloudSimpleManager 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 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.updateVirtualMachineRequest.withTags(tags);
            return this;
        }
    }

    public VirtualMachineImpl withAmountOfRam(int amountOfRam) {
        this.innerModel().withAmountOfRam(amountOfRam);
        return this;
    }

    public VirtualMachineImpl withCustomization(GuestOSCustomization customization) {
        this.innerModel().withCustomization(customization);
        return this;
    }

    public VirtualMachineImpl withDisks(List<VirtualDisk> disks) {
        this.innerModel().withDisks(disks);
        return this;
    }

    public VirtualMachineImpl withExposeToGuestVM(Boolean exposeToGuestVM) {
        this.innerModel().withExposeToGuestVM(exposeToGuestVM);
        return this;
    }

    public VirtualMachineImpl withNics(List<VirtualNicInner> nics) {
        this.innerModel().withNics(nics);
        return this;
    }

    public VirtualMachineImpl withNumberOfCores(int numberOfCores) {
        this.innerModel().withNumberOfCores(numberOfCores);
        return this;
    }

    public VirtualMachineImpl withPassword(String password) {
        this.innerModel().withPassword(password);
        return this;
    }

    public VirtualMachineImpl withPrivateCloudId(String privateCloudId) {
        this.innerModel().withPrivateCloudId(privateCloudId);
        return this;
    }

    public VirtualMachineImpl withResourcePool(ResourcePoolInner resourcePool) {
        this.innerModel().withResourcePool(resourcePool);
        return this;
    }

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

    public VirtualMachineImpl withUsername(String username) {
        this.innerModel().withUsername(username);
        return this;
    }

    public VirtualMachineImpl withVSphereNetworks(List<String> vSphereNetworks) {
        this.innerModel().withVSphereNetworks(vSphereNetworks);
        return this;
    }

    public VirtualMachineImpl withReferer(String referer) {
        this.createReferer = referer;
        return this;
    }

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