// 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.LabPlanInner;
import com.azure.resourcemanager.labservices.models.AutoShutdownProfile;
import com.azure.resourcemanager.labservices.models.ConnectionProfile;
import com.azure.resourcemanager.labservices.models.Identity;
import com.azure.resourcemanager.labservices.models.LabPlan;
import com.azure.resourcemanager.labservices.models.LabPlanNetworkProfile;
import com.azure.resourcemanager.labservices.models.LabPlanUpdate;
import com.azure.resourcemanager.labservices.models.ProvisioningState;
import com.azure.resourcemanager.labservices.models.SaveImageBody;
import com.azure.resourcemanager.labservices.models.SupportInfo;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class LabPlanImpl implements LabPlan, LabPlan.Definition, LabPlan.Update {
    private LabPlanInner 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 Identity identity() {
        return this.innerModel().identity();
    }

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

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

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

    public LabPlanNetworkProfile defaultNetworkProfile() {
        return this.innerModel().defaultNetworkProfile();
    }

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

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

    public SupportInfo supportInfo() {
        return this.innerModel().supportInfo();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String labPlanName;

    private LabPlanUpdate updateBody;

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

    public LabPlan create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabPlans()
                .createOrUpdate(resourceGroupName, labPlanName, this.innerModel(), Context.NONE);
        return this;
    }

    public LabPlan create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabPlans()
                .createOrUpdate(resourceGroupName, labPlanName, this.innerModel(), context);
        return this;
    }

    LabPlanImpl(String name, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = new LabPlanInner();
        this.serviceManager = serviceManager;
        this.labPlanName = name;
    }

    public LabPlanImpl update() {
        this.updateBody = new LabPlanUpdate();
        return this;
    }

    public LabPlan apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabPlans()
                .update(resourceGroupName, labPlanName, updateBody, Context.NONE);
        return this;
    }

    public LabPlan apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getLabPlans().update(resourceGroupName, labPlanName, updateBody, context);
        return this;
    }

    LabPlanImpl(LabPlanInner innerObject, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.labPlanName = Utils.getValueFromIdByName(innerObject.id(), "labPlans");
    }

    public LabPlan refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabPlans()
                .getByResourceGroupWithResponse(resourceGroupName, labPlanName, Context.NONE)
                .getValue();
        return this;
    }

    public LabPlan refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getLabPlans()
                .getByResourceGroupWithResponse(resourceGroupName, labPlanName, context)
                .getValue();
        return this;
    }

    public void saveImage(SaveImageBody body) {
        serviceManager.labPlans().saveImage(resourceGroupName, labPlanName, body);
    }

    public void saveImage(SaveImageBody body, Context context) {
        serviceManager.labPlans().saveImage(resourceGroupName, labPlanName, body, context);
    }

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

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

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

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

    public LabPlanImpl withDefaultConnectionProfile(ConnectionProfile defaultConnectionProfile) {
        if (isInCreateMode()) {
            this.innerModel().withDefaultConnectionProfile(defaultConnectionProfile);
            return this;
        } else {
            this.updateBody.withDefaultConnectionProfile(defaultConnectionProfile);
            return this;
        }
    }

    public LabPlanImpl withDefaultAutoShutdownProfile(AutoShutdownProfile defaultAutoShutdownProfile) {
        if (isInCreateMode()) {
            this.innerModel().withDefaultAutoShutdownProfile(defaultAutoShutdownProfile);
            return this;
        } else {
            this.updateBody.withDefaultAutoShutdownProfile(defaultAutoShutdownProfile);
            return this;
        }
    }

    public LabPlanImpl withDefaultNetworkProfile(LabPlanNetworkProfile defaultNetworkProfile) {
        if (isInCreateMode()) {
            this.innerModel().withDefaultNetworkProfile(defaultNetworkProfile);
            return this;
        } else {
            this.updateBody.withDefaultNetworkProfile(defaultNetworkProfile);
            return this;
        }
    }

    public LabPlanImpl withAllowedRegions(List<String> allowedRegions) {
        if (isInCreateMode()) {
            this.innerModel().withAllowedRegions(allowedRegions);
            return this;
        } else {
            this.updateBody.withAllowedRegions(allowedRegions);
            return this;
        }
    }

    public LabPlanImpl withSharedGalleryId(String sharedGalleryId) {
        if (isInCreateMode()) {
            this.innerModel().withSharedGalleryId(sharedGalleryId);
            return this;
        } else {
            this.updateBody.withSharedGalleryId(sharedGalleryId);
            return this;
        }
    }

    public LabPlanImpl withSupportInfo(SupportInfo supportInfo) {
        if (isInCreateMode()) {
            this.innerModel().withSupportInfo(supportInfo);
            return this;
        } else {
            this.updateBody.withSupportInfo(supportInfo);
            return this;
        }
    }

    public LabPlanImpl withLinkedLmsInstance(String linkedLmsInstance) {
        if (isInCreateMode()) {
            this.innerModel().withLinkedLmsInstance(linkedLmsInstance);
            return this;
        } else {
            this.updateBody.withLinkedLmsInstance(linkedLmsInstance);
            return this;
        }
    }

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

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