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

package com.azure.resourcemanager.automation.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.automation.fluent.models.RunbookCreateOrUpdateParametersInner;
import com.azure.resourcemanager.automation.fluent.models.RunbookDraftInner;
import com.azure.resourcemanager.automation.fluent.models.RunbookInner;
import com.azure.resourcemanager.automation.models.ContentLink;
import com.azure.resourcemanager.automation.models.Runbook;
import com.azure.resourcemanager.automation.models.RunbookDraft;
import com.azure.resourcemanager.automation.models.RunbookParameter;
import com.azure.resourcemanager.automation.models.RunbookProvisioningState;
import com.azure.resourcemanager.automation.models.RunbookState;
import com.azure.resourcemanager.automation.models.RunbookTypeEnum;
import com.azure.resourcemanager.automation.models.RunbookUpdateParameters;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class RunbookImpl implements Runbook, Runbook.Definition, Runbook.Update {
    private RunbookInner innerObject;

    private final com.azure.resourcemanager.automation.AutomationManager 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 String etag() {
        return this.innerModel().etag();
    }

    public RunbookTypeEnum runbookType() {
        return this.innerModel().runbookType();
    }

    public ContentLink publishContentLink() {
        return this.innerModel().publishContentLink();
    }

    public RunbookState state() {
        return this.innerModel().state();
    }

    public Boolean logVerbose() {
        return this.innerModel().logVerbose();
    }

    public Boolean logProgress() {
        return this.innerModel().logProgress();
    }

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

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

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

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

    public RunbookDraft draft() {
        RunbookDraftInner inner = this.innerModel().draft();
        if (inner != null) {
            return new RunbookDraftImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.automation.AutomationManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String automationAccountName;

    private String runbookName;

    private RunbookCreateOrUpdateParametersInner createParameters;

    private RunbookUpdateParameters updateParameters;

    public RunbookImpl withExistingAutomationAccount(String resourceGroupName, String automationAccountName) {
        this.resourceGroupName = resourceGroupName;
        this.automationAccountName = automationAccountName;
        return this;
    }

    public Runbook create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRunbooks()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, runbookName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Runbook create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRunbooks()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, runbookName, createParameters, context)
                .getValue();
        return this;
    }

    RunbookImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new RunbookInner();
        this.serviceManager = serviceManager;
        this.runbookName = name;
        this.createParameters = new RunbookCreateOrUpdateParametersInner();
    }

    public RunbookImpl update() {
        this.updateParameters = new RunbookUpdateParameters();
        return this;
    }

    public Runbook apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRunbooks()
                .updateWithResponse(
                    resourceGroupName, automationAccountName, runbookName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Runbook apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRunbooks()
                .updateWithResponse(resourceGroupName, automationAccountName, runbookName, updateParameters, context)
                .getValue();
        return this;
    }

    RunbookImpl(RunbookInner innerObject, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.automationAccountName = Utils.getValueFromIdByName(innerObject.id(), "automationAccounts");
        this.runbookName = Utils.getValueFromIdByName(innerObject.id(), "runbooks");
    }

    public Runbook refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRunbooks()
                .getWithResponse(resourceGroupName, automationAccountName, runbookName, Context.NONE)
                .getValue();
        return this;
    }

    public Runbook refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRunbooks()
                .getWithResponse(resourceGroupName, automationAccountName, runbookName, context)
                .getValue();
        return this;
    }

    public void publish() {
        serviceManager.runbooks().publish(resourceGroupName, automationAccountName, runbookName);
    }

    public void publish(Context context) {
        serviceManager.runbooks().publish(resourceGroupName, automationAccountName, runbookName, context);
    }

    public RunbookImpl withRunbookType(RunbookTypeEnum runbookType) {
        this.createParameters.withRunbookType(runbookType);
        return this;
    }

    public RunbookImpl withRegion(Region location) {
        this.createParameters.withLocation(location.toString());
        return this;
    }

    public RunbookImpl withRegion(String location) {
        this.createParameters.withLocation(location);
        return this;
    }

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

    public RunbookImpl withName(String name) {
        if (isInCreateMode()) {
            this.createParameters.withName(name);
            return this;
        } else {
            this.updateParameters.withName(name);
            return this;
        }
    }

    public RunbookImpl withLogVerbose(Boolean logVerbose) {
        if (isInCreateMode()) {
            this.createParameters.withLogVerbose(logVerbose);
            return this;
        } else {
            this.updateParameters.withLogVerbose(logVerbose);
            return this;
        }
    }

    public RunbookImpl withLogProgress(Boolean logProgress) {
        if (isInCreateMode()) {
            this.createParameters.withLogProgress(logProgress);
            return this;
        } else {
            this.updateParameters.withLogProgress(logProgress);
            return this;
        }
    }

    public RunbookImpl withDraft(RunbookDraftInner draft) {
        this.createParameters.withDraft(draft);
        return this;
    }

    public RunbookImpl withPublishContentLink(ContentLink publishContentLink) {
        this.createParameters.withPublishContentLink(publishContentLink);
        return this;
    }

    public RunbookImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.createParameters.withDescription(description);
            return this;
        } else {
            this.updateParameters.withDescription(description);
            return this;
        }
    }

    public RunbookImpl withLogActivityTrace(Integer logActivityTrace) {
        if (isInCreateMode()) {
            this.createParameters.withLogActivityTrace(logActivityTrace);
            return this;
        } else {
            this.updateParameters.withLogActivityTrace(logActivityTrace);
            return this;
        }
    }

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