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

package com.azure.resourcemanager.storagemover.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.storagemover.fluent.models.ProjectInner;
import com.azure.resourcemanager.storagemover.models.Project;
import com.azure.resourcemanager.storagemover.models.ProjectUpdateParameters;
import com.azure.resourcemanager.storagemover.models.ProvisioningState;

public final class ProjectImpl implements Project, Project.Definition, Project.Update {
    private ProjectInner innerObject;

    private final com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager;

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.storagemover.StorageMoverManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String storageMoverName;

    private String projectName;

    private ProjectUpdateParameters updateProject;

    public ProjectImpl withExistingStorageMover(String resourceGroupName, String storageMoverName) {
        this.resourceGroupName = resourceGroupName;
        this.storageMoverName = storageMoverName;
        return this;
    }

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

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

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

    public ProjectImpl update() {
        this.updateProject = new ProjectUpdateParameters();
        return this;
    }

    public Project apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .updateWithResponse(resourceGroupName, storageMoverName, projectName, updateProject, Context.NONE)
                .getValue();
        return this;
    }

    public Project apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .updateWithResponse(resourceGroupName, storageMoverName, projectName, updateProject, context)
                .getValue();
        return this;
    }

    ProjectImpl(ProjectInner innerObject, com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.storageMoverName = Utils.getValueFromIdByName(innerObject.id(), "storageMovers");
        this.projectName = Utils.getValueFromIdByName(innerObject.id(), "projects");
    }

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

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

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

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