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

package com.azure.resourcemanager.datamigration.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.datamigration.fluent.models.ProjectInner;
import com.azure.resourcemanager.datamigration.models.ConnectionInfo;
import com.azure.resourcemanager.datamigration.models.DatabaseInfo;
import com.azure.resourcemanager.datamigration.models.Project;
import com.azure.resourcemanager.datamigration.models.ProjectProvisioningState;
import com.azure.resourcemanager.datamigration.models.ProjectSourcePlatform;
import com.azure.resourcemanager.datamigration.models.ProjectTargetPlatform;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

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

    private final com.azure.resourcemanager.datamigration.DataMigrationManager 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 ProjectSourcePlatform sourcePlatform() {
        return this.innerModel().sourcePlatform();
    }

    public ProjectTargetPlatform targetPlatform() {
        return this.innerModel().targetPlatform();
    }

    public OffsetDateTime creationTime() {
        return this.innerModel().creationTime();
    }

    public ConnectionInfo sourceConnectionInfo() {
        return this.innerModel().sourceConnectionInfo();
    }

    public ConnectionInfo targetConnectionInfo() {
        return this.innerModel().targetConnectionInfo();
    }

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

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

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

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

    public String resourceGroupName() {
        return groupName;
    }

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

    private com.azure.resourcemanager.datamigration.DataMigrationManager manager() {
        return this.serviceManager;
    }

    private String groupName;

    private String serviceName;

    private String projectName;

    public ProjectImpl withExistingService(String groupName, String serviceName) {
        this.groupName = groupName;
        this.serviceName = serviceName;
        return this;
    }

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

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

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

    public ProjectImpl update() {
        return this;
    }

    public Project apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .updateWithResponse(groupName, serviceName, projectName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Project apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .updateWithResponse(groupName, serviceName, projectName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ProjectImpl(ProjectInner innerObject, com.azure.resourcemanager.datamigration.DataMigrationManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.groupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.serviceName = Utils.getValueFromIdByName(innerObject.id(), "services");
        this.projectName = Utils.getValueFromIdByName(innerObject.id(), "projects");
    }

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

    public Project refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProjects()
                .getWithResponse(groupName, serviceName, 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) {
        this.innerModel().withTags(tags);
        return this;
    }

    public ProjectImpl withSourcePlatform(ProjectSourcePlatform sourcePlatform) {
        this.innerModel().withSourcePlatform(sourcePlatform);
        return this;
    }

    public ProjectImpl withTargetPlatform(ProjectTargetPlatform targetPlatform) {
        this.innerModel().withTargetPlatform(targetPlatform);
        return this;
    }

    public ProjectImpl withSourceConnectionInfo(ConnectionInfo sourceConnectionInfo) {
        this.innerModel().withSourceConnectionInfo(sourceConnectionInfo);
        return this;
    }

    public ProjectImpl withTargetConnectionInfo(ConnectionInfo targetConnectionInfo) {
        this.innerModel().withTargetConnectionInfo(targetConnectionInfo);
        return this;
    }

    public ProjectImpl withDatabasesInfo(List<DatabaseInfo> databasesInfo) {
        this.innerModel().withDatabasesInfo(databasesInfo);
        return this;
    }
}
