// 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.ProjectInner;
import com.azure.resourcemanager.devcenter.models.Project;
import com.azure.resourcemanager.devcenter.models.ProjectUpdate;
import com.azure.resourcemanager.devcenter.models.ProvisioningState;
import java.util.Collections;
import java.util.Map;

public final class ProjectImpl implements Project, Project.Definition, Project.Update {
    private ProjectInner 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 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 String devCenterUri() {
        return this.innerModel().devCenterUri();
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String projectName;

    private ProjectUpdate updateBody;

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

    public Project create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .createOrUpdate(resourceGroupName, projectName, this.innerModel(), Context.NONE);
        return this;
    }

    public Project create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .createOrUpdate(resourceGroupName, projectName, this.innerModel(), context);
        return this;
    }

    ProjectImpl(String name, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerObject = new ProjectInner();
        this.serviceManager = serviceManager;
        this.projectName = name;
    }

    public ProjectImpl update() {
        this.updateBody = new ProjectUpdate();
        return this;
    }

    public Project apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .update(resourceGroupName, projectName, updateBody, Context.NONE);
        return this;
    }

    public Project apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getProjects().update(resourceGroupName, projectName, updateBody, context);
        return this;
    }

    ProjectImpl(ProjectInner 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");
    }

    public Project refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .getByResourceGroupWithResponse(resourceGroupName, projectName, Context.NONE)
                .getValue();
        return this;
    }

    public Project refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .getByResourceGroupWithResponse(resourceGroupName, projectName, context)
                .getValue();
        return this;
    }

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

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

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

    public ProjectImpl withDevCenterId(String devCenterId) {
        if (isInCreateMode()) {
            this.innerModel().withDevCenterId(devCenterId);
            return this;
        } else {
            this.updateBody.withDevCenterId(devCenterId);
            return this;
        }
    }

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

    public ProjectImpl withMaxDevBoxesPerUser(Integer maxDevBoxesPerUser) {
        if (isInCreateMode()) {
            this.innerModel().withMaxDevBoxesPerUser(maxDevBoxesPerUser);
            return this;
        } else {
            this.updateBody.withMaxDevBoxesPerUser(maxDevBoxesPerUser);
            return this;
        }
    }

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