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

package com.azure.resourcemanager.devhub.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devhub.fluent.models.WorkflowInner;
import com.azure.resourcemanager.devhub.models.Acr;
import com.azure.resourcemanager.devhub.models.AuthorizationStatus;
import com.azure.resourcemanager.devhub.models.DeploymentProperties;
import com.azure.resourcemanager.devhub.models.DockerfileGenerationMode;
import com.azure.resourcemanager.devhub.models.GenerationLanguage;
import com.azure.resourcemanager.devhub.models.GenerationManifestType;
import com.azure.resourcemanager.devhub.models.GitHubWorkflowProfileOidcCredentials;
import com.azure.resourcemanager.devhub.models.ManifestGenerationMode;
import com.azure.resourcemanager.devhub.models.PullRequestStatus;
import com.azure.resourcemanager.devhub.models.TagsObject;
import com.azure.resourcemanager.devhub.models.Workflow;
import com.azure.resourcemanager.devhub.models.WorkflowRun;
import java.util.Collections;
import java.util.Map;

public final class WorkflowImpl implements Workflow, Workflow.Definition, Workflow.Update {
    private WorkflowInner innerObject;

    private final com.azure.resourcemanager.devhub.DevHubManager 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 String repositoryOwner() {
        return this.innerModel().repositoryOwner();
    }

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

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

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

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

    public DeploymentProperties deploymentProperties() {
        return this.innerModel().deploymentProperties();
    }

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

    public Acr acr() {
        return this.innerModel().acr();
    }

    public GitHubWorkflowProfileOidcCredentials oidcCredentials() {
        return this.innerModel().oidcCredentials();
    }

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

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

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

    public PullRequestStatus prStatus() {
        return this.innerModel().prStatus();
    }

    public WorkflowRun lastWorkflowRun() {
        return this.innerModel().lastWorkflowRun();
    }

    public AuthorizationStatus authStatus() {
        return this.innerModel().authStatus();
    }

    public GenerationLanguage generationLanguage() {
        return this.innerModel().generationLanguage();
    }

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

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

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

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

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

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

    public DockerfileGenerationMode dockerfileGenerationMode() {
        return this.innerModel().dockerfileGenerationMode();
    }

    public ManifestGenerationMode manifestGenerationMode() {
        return this.innerModel().manifestGenerationMode();
    }

    public GenerationManifestType manifestType() {
        return this.innerModel().manifestType();
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.devhub.DevHubManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String workflowName;

    private TagsObject updateParameters;

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

    public Workflow create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkflows()
                .createOrUpdateWithResponse(resourceGroupName, workflowName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Workflow create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkflows()
                .createOrUpdateWithResponse(resourceGroupName, workflowName, this.innerModel(), context)
                .getValue();
        return this;
    }

    WorkflowImpl(String name, com.azure.resourcemanager.devhub.DevHubManager serviceManager) {
        this.innerObject = new WorkflowInner();
        this.serviceManager = serviceManager;
        this.workflowName = name;
    }

    public WorkflowImpl update() {
        this.updateParameters = new TagsObject();
        return this;
    }

    public Workflow apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkflows()
                .updateTagsWithResponse(resourceGroupName, workflowName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Workflow apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkflows()
                .updateTagsWithResponse(resourceGroupName, workflowName, updateParameters, context)
                .getValue();
        return this;
    }

    WorkflowImpl(WorkflowInner innerObject, com.azure.resourcemanager.devhub.DevHubManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workflowName = Utils.getValueFromIdByName(innerObject.id(), "workflows");
    }

    public Workflow refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkflows()
                .getByResourceGroupWithResponse(resourceGroupName, workflowName, Context.NONE)
                .getValue();
        return this;
    }

    public Workflow refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkflows()
                .getByResourceGroupWithResponse(resourceGroupName, workflowName, context)
                .getValue();
        return this;
    }

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

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

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

    public WorkflowImpl withRepositoryOwner(String repositoryOwner) {
        this.innerModel().withRepositoryOwner(repositoryOwner);
        return this;
    }

    public WorkflowImpl withRepositoryName(String repositoryName) {
        this.innerModel().withRepositoryName(repositoryName);
        return this;
    }

    public WorkflowImpl withBranchName(String branchName) {
        this.innerModel().withBranchName(branchName);
        return this;
    }

    public WorkflowImpl withDockerfile(String dockerfile) {
        this.innerModel().withDockerfile(dockerfile);
        return this;
    }

    public WorkflowImpl withDockerBuildContext(String dockerBuildContext) {
        this.innerModel().withDockerBuildContext(dockerBuildContext);
        return this;
    }

    public WorkflowImpl withDeploymentProperties(DeploymentProperties deploymentProperties) {
        this.innerModel().withDeploymentProperties(deploymentProperties);
        return this;
    }

    public WorkflowImpl withNamespace(String namespace) {
        this.innerModel().withNamespace(namespace);
        return this;
    }

    public WorkflowImpl withAcr(Acr acr) {
        this.innerModel().withAcr(acr);
        return this;
    }

    public WorkflowImpl withOidcCredentials(GitHubWorkflowProfileOidcCredentials oidcCredentials) {
        this.innerModel().withOidcCredentials(oidcCredentials);
        return this;
    }

    public WorkflowImpl withAksResourceId(String aksResourceId) {
        this.innerModel().withAksResourceId(aksResourceId);
        return this;
    }

    public WorkflowImpl withLastWorkflowRun(WorkflowRun lastWorkflowRun) {
        this.innerModel().withLastWorkflowRun(lastWorkflowRun);
        return this;
    }

    public WorkflowImpl withGenerationLanguage(GenerationLanguage generationLanguage) {
        this.innerModel().withGenerationLanguage(generationLanguage);
        return this;
    }

    public WorkflowImpl withLanguageVersion(String languageVersion) {
        this.innerModel().withLanguageVersion(languageVersion);
        return this;
    }

    public WorkflowImpl withBuilderVersion(String builderVersion) {
        this.innerModel().withBuilderVersion(builderVersion);
        return this;
    }

    public WorkflowImpl withPort(String port) {
        this.innerModel().withPort(port);
        return this;
    }

    public WorkflowImpl withAppName(String appName) {
        this.innerModel().withAppName(appName);
        return this;
    }

    public WorkflowImpl withDockerfileOutputDirectory(String dockerfileOutputDirectory) {
        this.innerModel().withDockerfileOutputDirectory(dockerfileOutputDirectory);
        return this;
    }

    public WorkflowImpl withManifestOutputDirectory(String manifestOutputDirectory) {
        this.innerModel().withManifestOutputDirectory(manifestOutputDirectory);
        return this;
    }

    public WorkflowImpl withDockerfileGenerationMode(DockerfileGenerationMode dockerfileGenerationMode) {
        this.innerModel().withDockerfileGenerationMode(dockerfileGenerationMode);
        return this;
    }

    public WorkflowImpl withManifestGenerationMode(ManifestGenerationMode manifestGenerationMode) {
        this.innerModel().withManifestGenerationMode(manifestGenerationMode);
        return this;
    }

    public WorkflowImpl withManifestType(GenerationManifestType manifestType) {
        this.innerModel().withManifestType(manifestType);
        return this;
    }

    public WorkflowImpl withImageName(String imageName) {
        this.innerModel().withImageName(imageName);
        return this;
    }

    public WorkflowImpl withNamespaceArtifactGenerationPropertiesNamespace(
        String namespaceArtifactGenerationPropertiesNamespace) {
        this
            .innerModel()
            .withNamespaceArtifactGenerationPropertiesNamespace(namespaceArtifactGenerationPropertiesNamespace);
        return this;
    }

    public WorkflowImpl withImageTag(String imageTag) {
        this.innerModel().withImageTag(imageTag);
        return this;
    }

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