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

package com.azure.resourcemanager.devcenter.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devcenter.fluent.models.ProjectEnvironmentTypeInner;
import com.azure.resourcemanager.devcenter.models.EnvironmentTypeEnableStatus;
import com.azure.resourcemanager.devcenter.models.ManagedServiceIdentity;
import com.azure.resourcemanager.devcenter.models.ProjectEnvironmentType;
import com.azure.resourcemanager.devcenter.models.ProjectEnvironmentTypeUpdate;
import com.azure.resourcemanager.devcenter.models.ProjectEnvironmentTypeUpdatePropertiesCreatorRoleAssignment;
import com.azure.resourcemanager.devcenter.models.ProvisioningState;
import com.azure.resourcemanager.devcenter.models.UserRoleAssignmentValue;
import java.util.Collections;
import java.util.Map;

public final class ProjectEnvironmentTypeImpl
    implements ProjectEnvironmentType, ProjectEnvironmentType.Definition, ProjectEnvironmentType.Update {
    private ProjectEnvironmentTypeInner innerObject;

    private final com.azure.resourcemanager.devcenter.DevCenterManager serviceManager;

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

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

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

    public Map<String, String> tags() {
        Map<String, String> inner = this.innerModel().tags();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public ManagedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

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

    public ProjectEnvironmentTypeUpdatePropertiesCreatorRoleAssignment creatorRoleAssignment() {
        return this.innerModel().creatorRoleAssignment();
    }

    public Map<String, UserRoleAssignmentValue> userRoleAssignments() {
        Map<String, UserRoleAssignmentValue> inner = this.innerModel().userRoleAssignments();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.devcenter.DevCenterManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String projectName;

    private String environmentTypeName;

    private ProjectEnvironmentTypeUpdate updateBody;

    public ProjectEnvironmentTypeImpl withExistingProject(String resourceGroupName, String projectName) {
        this.resourceGroupName = resourceGroupName;
        this.projectName = projectName;
        return this;
    }

    public ProjectEnvironmentType create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjectEnvironmentTypes()
                .createOrUpdateWithResponse(
                    resourceGroupName, projectName, environmentTypeName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ProjectEnvironmentType create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjectEnvironmentTypes()
                .createOrUpdateWithResponse(
                    resourceGroupName, projectName, environmentTypeName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ProjectEnvironmentTypeImpl(String name, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerObject = new ProjectEnvironmentTypeInner();
        this.serviceManager = serviceManager;
        this.environmentTypeName = name;
    }

    public ProjectEnvironmentTypeImpl update() {
        this.updateBody = new ProjectEnvironmentTypeUpdate();
        return this;
    }

    public ProjectEnvironmentType apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjectEnvironmentTypes()
                .updateWithResponse(resourceGroupName, projectName, environmentTypeName, updateBody, Context.NONE)
                .getValue();
        return this;
    }

    public ProjectEnvironmentType apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjectEnvironmentTypes()
                .updateWithResponse(resourceGroupName, projectName, environmentTypeName, updateBody, context)
                .getValue();
        return this;
    }

    ProjectEnvironmentTypeImpl(
        ProjectEnvironmentTypeInner innerObject, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.projectName = Utils.getValueFromIdByName(innerObject.id(), "projects");
        this.environmentTypeName = Utils.getValueFromIdByName(innerObject.id(), "environmentTypes");
    }

    public ProjectEnvironmentType refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjectEnvironmentTypes()
                .getWithResponse(resourceGroupName, projectName, environmentTypeName, Context.NONE)
                .getValue();
        return this;
    }

    public ProjectEnvironmentType refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjectEnvironmentTypes()
                .getWithResponse(resourceGroupName, projectName, environmentTypeName, context)
                .getValue();
        return this;
    }

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

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

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

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

    public ProjectEnvironmentTypeImpl withDeploymentTargetId(String deploymentTargetId) {
        if (isInCreateMode()) {
            this.innerModel().withDeploymentTargetId(deploymentTargetId);
            return this;
        } else {
            this.updateBody.withDeploymentTargetId(deploymentTargetId);
            return this;
        }
    }

    public ProjectEnvironmentTypeImpl withStatus(EnvironmentTypeEnableStatus status) {
        if (isInCreateMode()) {
            this.innerModel().withStatus(status);
            return this;
        } else {
            this.updateBody.withStatus(status);
            return this;
        }
    }

    public ProjectEnvironmentTypeImpl withCreatorRoleAssignment(
        ProjectEnvironmentTypeUpdatePropertiesCreatorRoleAssignment creatorRoleAssignment) {
        if (isInCreateMode()) {
            this.innerModel().withCreatorRoleAssignment(creatorRoleAssignment);
            return this;
        } else {
            this.updateBody.withCreatorRoleAssignment(creatorRoleAssignment);
            return this;
        }
    }

    public ProjectEnvironmentTypeImpl withUserRoleAssignments(
        Map<String, UserRoleAssignmentValue> userRoleAssignments) {
        if (isInCreateMode()) {
            this.innerModel().withUserRoleAssignments(userRoleAssignments);
            return this;
        } else {
            this.updateBody.withUserRoleAssignments(userRoleAssignments);
            return this;
        }
    }

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