// 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.DscConfigurationInner;
import com.azure.resourcemanager.automation.models.ContentSource;
import com.azure.resourcemanager.automation.models.DscConfiguration;
import com.azure.resourcemanager.automation.models.DscConfigurationCreateOrUpdateParameters;
import com.azure.resourcemanager.automation.models.DscConfigurationParameter;
import com.azure.resourcemanager.automation.models.DscConfigurationProvisioningState;
import com.azure.resourcemanager.automation.models.DscConfigurationState;
import com.azure.resourcemanager.automation.models.DscConfigurationUpdateParameters;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class DscConfigurationImpl
    implements DscConfiguration, DscConfiguration.Definition, DscConfiguration.Update {
    private DscConfigurationInner 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 DscConfigurationProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

    public ContentSource source() {
        return this.innerModel().source();
    }

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

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

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

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

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

    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 DscConfigurationInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String automationAccountName;

    private String configurationName;

    private DscConfigurationCreateOrUpdateParameters createParameters;

    private DscConfigurationUpdateParameters updateParameters;

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

    public DscConfiguration create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscConfigurations()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, configurationName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public DscConfiguration create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscConfigurations()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, configurationName, createParameters, context)
                .getValue();
        return this;
    }

    DscConfigurationImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new DscConfigurationInner();
        this.serviceManager = serviceManager;
        this.configurationName = name;
        this.createParameters = new DscConfigurationCreateOrUpdateParameters();
    }

    public DscConfigurationImpl update() {
        this.updateParameters = new DscConfigurationUpdateParameters();
        return this;
    }

    public DscConfiguration apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscConfigurations()
                .updateWithResponse(
                    resourceGroupName, automationAccountName, configurationName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public DscConfiguration apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscConfigurations()
                .updateWithResponse(
                    resourceGroupName, automationAccountName, configurationName, updateParameters, context)
                .getValue();
        return this;
    }

    DscConfigurationImpl(
        DscConfigurationInner 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.configurationName = Utils.getValueFromIdByName(innerObject.id(), "configurations");
    }

    public DscConfiguration refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscConfigurations()
                .getWithResponse(resourceGroupName, automationAccountName, configurationName, Context.NONE)
                .getValue();
        return this;
    }

    public DscConfiguration refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscConfigurations()
                .getWithResponse(resourceGroupName, automationAccountName, configurationName, context)
                .getValue();
        return this;
    }

    public DscConfigurationImpl withSource(ContentSource source) {
        if (isInCreateMode()) {
            this.createParameters.withSource(source);
            return this;
        } else {
            this.updateParameters.withSource(source);
            return this;
        }
    }

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

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

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

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

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

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

    public DscConfigurationImpl withParameters(Map<String, DscConfigurationParameter> parameters) {
        if (isInCreateMode()) {
            this.createParameters.withParameters(parameters);
            return this;
        } else {
            this.updateParameters.withParameters(parameters);
            return this;
        }
    }

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

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