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

package com.azure.resourcemanager.devtestlabs.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devtestlabs.fluent.models.DtlEnvironmentInner;
import com.azure.resourcemanager.devtestlabs.models.DtlEnvironment;
import com.azure.resourcemanager.devtestlabs.models.DtlEnvironmentFragment;
import com.azure.resourcemanager.devtestlabs.models.EnvironmentDeploymentProperties;
import java.util.Collections;
import java.util.Map;

public final class DtlEnvironmentImpl implements DtlEnvironment, DtlEnvironment.Definition, DtlEnvironment.Update {
    private DtlEnvironmentInner innerObject;

    private final com.azure.resourcemanager.devtestlabs.DevTestLabsManager 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 EnvironmentDeploymentProperties deploymentProperties() {
        return this.innerModel().deploymentProperties();
    }

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

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.devtestlabs.DevTestLabsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String labName;

    private String username;

    private String name;

    private DtlEnvironmentFragment updateDtlEnvironment;

    public DtlEnvironmentImpl withExistingUser(String resourceGroupName, String labName, String username) {
        this.resourceGroupName = resourceGroupName;
        this.labName = labName;
        this.username = username;
        return this;
    }

    public DtlEnvironment create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getEnvironments()
                .createOrUpdate(resourceGroupName, labName, username, name, this.innerModel(), Context.NONE);
        return this;
    }

    public DtlEnvironment create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getEnvironments()
                .createOrUpdate(resourceGroupName, labName, username, name, this.innerModel(), context);
        return this;
    }

    DtlEnvironmentImpl(String name, com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager) {
        this.innerObject = new DtlEnvironmentInner();
        this.serviceManager = serviceManager;
        this.name = name;
    }

    public DtlEnvironmentImpl update() {
        this.updateDtlEnvironment = new DtlEnvironmentFragment();
        return this;
    }

    public DtlEnvironment apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getEnvironments()
                .updateWithResponse(resourceGroupName, labName, username, name, updateDtlEnvironment, Context.NONE)
                .getValue();
        return this;
    }

    public DtlEnvironment apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getEnvironments()
                .updateWithResponse(resourceGroupName, labName, username, name, updateDtlEnvironment, context)
                .getValue();
        return this;
    }

    DtlEnvironmentImpl(
        DtlEnvironmentInner innerObject, com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.labName = Utils.getValueFromIdByName(innerObject.id(), "labs");
        this.username = Utils.getValueFromIdByName(innerObject.id(), "users");
        this.name = Utils.getValueFromIdByName(innerObject.id(), "environments");
    }

    public DtlEnvironment refresh() {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getEnvironments()
                .getWithResponse(resourceGroupName, labName, username, name, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public DtlEnvironment refresh(Context context) {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getEnvironments()
                .getWithResponse(resourceGroupName, labName, username, name, localExpand, context)
                .getValue();
        return this;
    }

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

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

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

    public DtlEnvironmentImpl withDeploymentProperties(EnvironmentDeploymentProperties deploymentProperties) {
        this.innerModel().withDeploymentProperties(deploymentProperties);
        return this;
    }

    public DtlEnvironmentImpl withArmTemplateDisplayName(String armTemplateDisplayName) {
        this.innerModel().withArmTemplateDisplayName(armTemplateDisplayName);
        return this;
    }

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