// 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.VirtualMachineTemplateInner;
import com.azure.resourcemanager.connectedvmware.models.ExtendedLocation;
import com.azure.resourcemanager.connectedvmware.models.FirmwareType;
import com.azure.resourcemanager.connectedvmware.models.NetworkInterface;
import com.azure.resourcemanager.connectedvmware.models.OsType;
import com.azure.resourcemanager.connectedvmware.models.ResourcePatch;
import com.azure.resourcemanager.connectedvmware.models.ResourceStatus;
import com.azure.resourcemanager.connectedvmware.models.VirtualDisk;
import com.azure.resourcemanager.connectedvmware.models.VirtualMachineTemplate;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class VirtualMachineTemplateImpl
    implements VirtualMachineTemplate, VirtualMachineTemplate.Definition, VirtualMachineTemplate.Update {
    private VirtualMachineTemplateInner 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 String uuid() {
        return this.innerModel().uuid();
    }

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

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

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

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

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

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

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

    public OsType osType() {
        return this.innerModel().osType();
    }

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

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

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

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

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

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

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

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

    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 Region region() {
        return Region.fromName(this.regionName());
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String virtualMachineTemplateName;

    private ResourcePatch updateBody;

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

    public VirtualMachineTemplate create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachineTemplates()
                .create(resourceGroupName, virtualMachineTemplateName, this.innerModel(), Context.NONE);
        return this;
    }

    public VirtualMachineTemplate create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachineTemplates()
                .create(resourceGroupName, virtualMachineTemplateName, this.innerModel(), context);
        return this;
    }

    VirtualMachineTemplateImpl(
        String name, com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerObject = new VirtualMachineTemplateInner();
        this.serviceManager = serviceManager;
        this.virtualMachineTemplateName = name;
    }

    public VirtualMachineTemplateImpl update() {
        this.updateBody = new ResourcePatch();
        return this;
    }

    public VirtualMachineTemplate apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachineTemplates()
                .updateWithResponse(resourceGroupName, virtualMachineTemplateName, updateBody, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualMachineTemplate apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachineTemplates()
                .updateWithResponse(resourceGroupName, virtualMachineTemplateName, updateBody, context)
                .getValue();
        return this;
    }

    VirtualMachineTemplateImpl(
        VirtualMachineTemplateInner innerObject,
        com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.virtualMachineTemplateName = Utils.getValueFromIdByName(innerObject.id(), "virtualMachineTemplates");
    }

    public VirtualMachineTemplate refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachineTemplates()
                .getByResourceGroupWithResponse(resourceGroupName, virtualMachineTemplateName, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualMachineTemplate refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualMachineTemplates()
                .getByResourceGroupWithResponse(resourceGroupName, virtualMachineTemplateName, context)
                .getValue();
        return this;
    }

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

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

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

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

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

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

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

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

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