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

package com.azure.resourcemanager.labservices.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.labservices.fluent.models.LabInner;
import com.azure.resourcemanager.labservices.models.AutoShutdownProfile;
import com.azure.resourcemanager.labservices.models.ConnectionProfile;
import com.azure.resourcemanager.labservices.models.Lab;
import com.azure.resourcemanager.labservices.models.LabNetworkProfile;
import com.azure.resourcemanager.labservices.models.LabState;
import com.azure.resourcemanager.labservices.models.LabUpdate;
import com.azure.resourcemanager.labservices.models.ProvisioningState;
import com.azure.resourcemanager.labservices.models.RosterProfile;
import com.azure.resourcemanager.labservices.models.SecurityProfile;
import com.azure.resourcemanager.labservices.models.VirtualMachineProfile;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class LabImpl implements Lab, Lab.Definition, Lab.Update {
    private LabInner innerObject;

    private final com.azure.resourcemanager.labservices.LabServicesManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

    public LabState state() {
        return this.innerModel().state();
    }

    public AutoShutdownProfile autoShutdownProfile() {
        return this.innerModel().autoShutdownProfile();
    }

    public ConnectionProfile connectionProfile() {
        return this.innerModel().connectionProfile();
    }

    public VirtualMachineProfile virtualMachineProfile() {
        return this.innerModel().virtualMachineProfile();
    }

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

    public RosterProfile rosterProfile() {
        return this.innerModel().rosterProfile();
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.labservices.LabServicesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String labName;

    private LabUpdate updateBody;

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

    public Lab create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabs()
                .createOrUpdate(resourceGroupName, labName, this.innerModel(), Context.NONE);
        return this;
    }

    public Lab create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabs()
                .createOrUpdate(resourceGroupName, labName, this.innerModel(), context);
        return this;
    }

    LabImpl(String name, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = new LabInner();
        this.serviceManager = serviceManager;
        this.labName = name;
    }

    public LabImpl update() {
        this.updateBody = new LabUpdate();
        return this;
    }

    public Lab apply() {
        this.innerObject =
            serviceManager.serviceClient().getLabs().update(resourceGroupName, labName, updateBody, Context.NONE);
        return this;
    }

    public Lab apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getLabs().update(resourceGroupName, labName, updateBody, context);
        return this;
    }

    LabImpl(LabInner innerObject, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.labName = Utils.getValueFromIdByName(innerObject.id(), "labs");
    }

    public Lab refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabs()
                .getByResourceGroupWithResponse(resourceGroupName, labName, Context.NONE)
                .getValue();
        return this;
    }

    public Lab refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabs()
                .getByResourceGroupWithResponse(resourceGroupName, labName, context)
                .getValue();
        return this;
    }

    public void publish() {
        serviceManager.labs().publish(resourceGroupName, labName);
    }

    public void publish(Context context) {
        serviceManager.labs().publish(resourceGroupName, labName, context);
    }

    public void syncGroup() {
        serviceManager.labs().syncGroup(resourceGroupName, labName);
    }

    public void syncGroup(Context context) {
        serviceManager.labs().syncGroup(resourceGroupName, labName, context);
    }

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

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

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

    public LabImpl withNetworkProfile(LabNetworkProfile networkProfile) {
        this.innerModel().withNetworkProfile(networkProfile);
        return this;
    }

    public LabImpl withAutoShutdownProfile(AutoShutdownProfile autoShutdownProfile) {
        if (isInCreateMode()) {
            this.innerModel().withAutoShutdownProfile(autoShutdownProfile);
            return this;
        } else {
            this.updateBody.withAutoShutdownProfile(autoShutdownProfile);
            return this;
        }
    }

    public LabImpl withConnectionProfile(ConnectionProfile connectionProfile) {
        if (isInCreateMode()) {
            this.innerModel().withConnectionProfile(connectionProfile);
            return this;
        } else {
            this.updateBody.withConnectionProfile(connectionProfile);
            return this;
        }
    }

    public LabImpl withVirtualMachineProfile(VirtualMachineProfile virtualMachineProfile) {
        if (isInCreateMode()) {
            this.innerModel().withVirtualMachineProfile(virtualMachineProfile);
            return this;
        } else {
            this.updateBody.withVirtualMachineProfile(virtualMachineProfile);
            return this;
        }
    }

    public LabImpl withSecurityProfile(SecurityProfile securityProfile) {
        if (isInCreateMode()) {
            this.innerModel().withSecurityProfile(securityProfile);
            return this;
        } else {
            this.updateBody.withSecurityProfile(securityProfile);
            return this;
        }
    }

    public LabImpl withRosterProfile(RosterProfile rosterProfile) {
        if (isInCreateMode()) {
            this.innerModel().withRosterProfile(rosterProfile);
            return this;
        } else {
            this.updateBody.withRosterProfile(rosterProfile);
            return this;
        }
    }

    public LabImpl withLabPlanId(String labPlanId) {
        if (isInCreateMode()) {
            this.innerModel().withLabPlanId(labPlanId);
            return this;
        } else {
            this.updateBody.withLabPlanId(labPlanId);
            return this;
        }
    }

    public LabImpl withTitle(String title) {
        if (isInCreateMode()) {
            this.innerModel().withTitle(title);
            return this;
        } else {
            this.updateBody.withTitle(title);
            return this;
        }
    }

    public LabImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.innerModel().withDescription(description);
            return this;
        } else {
            this.updateBody.withDescription(description);
            return this;
        }
    }

    public LabImpl withTags(List<String> tags) {
        this.updateBody.withTags(tags);
        return this;
    }

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