// 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.util.Context;
import com.azure.resourcemanager.automation.fluent.models.DscNodeConfigurationInner;
import com.azure.resourcemanager.automation.models.ContentSource;
import com.azure.resourcemanager.automation.models.DscConfigurationAssociationProperty;
import com.azure.resourcemanager.automation.models.DscNodeConfiguration;
import com.azure.resourcemanager.automation.models.DscNodeConfigurationCreateOrUpdateParameters;
import java.time.OffsetDateTime;
import java.util.Map;

public final class DscNodeConfigurationImpl
    implements DscNodeConfiguration, DscNodeConfiguration.Definition, DscNodeConfiguration.Update {
    private DscNodeConfigurationInner 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 OffsetDateTime lastModifiedTime() {
        return this.innerModel().lastModifiedTime();
    }

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

    public DscConfigurationAssociationProperty configuration() {
        return this.innerModel().configuration();
    }

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

    public Long nodeCount() {
        return this.innerModel().nodeCount();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String automationAccountName;

    private String nodeConfigurationName;

    private DscNodeConfigurationCreateOrUpdateParameters createParameters;

    private DscNodeConfigurationCreateOrUpdateParameters updateParameters;

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

    public DscNodeConfiguration create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscNodeConfigurations()
                .createOrUpdate(
                    resourceGroupName, automationAccountName, nodeConfigurationName, createParameters, Context.NONE);
        return this;
    }

    public DscNodeConfiguration create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscNodeConfigurations()
                .createOrUpdate(
                    resourceGroupName, automationAccountName, nodeConfigurationName, createParameters, context);
        return this;
    }

    DscNodeConfigurationImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new DscNodeConfigurationInner();
        this.serviceManager = serviceManager;
        this.nodeConfigurationName = name;
        this.createParameters = new DscNodeConfigurationCreateOrUpdateParameters();
    }

    public DscNodeConfigurationImpl update() {
        this.updateParameters = new DscNodeConfigurationCreateOrUpdateParameters();
        return this;
    }

    public DscNodeConfiguration apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscNodeConfigurations()
                .createOrUpdate(
                    resourceGroupName, automationAccountName, nodeConfigurationName, updateParameters, Context.NONE);
        return this;
    }

    public DscNodeConfiguration apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscNodeConfigurations()
                .createOrUpdate(
                    resourceGroupName, automationAccountName, nodeConfigurationName, updateParameters, context);
        return this;
    }

    DscNodeConfigurationImpl(
        DscNodeConfigurationInner 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.nodeConfigurationName = Utils.getValueFromIdByName(innerObject.id(), "nodeConfigurations");
    }

    public DscNodeConfiguration refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscNodeConfigurations()
                .getWithResponse(resourceGroupName, automationAccountName, nodeConfigurationName, Context.NONE)
                .getValue();
        return this;
    }

    public DscNodeConfiguration refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDscNodeConfigurations()
                .getWithResponse(resourceGroupName, automationAccountName, nodeConfigurationName, context)
                .getValue();
        return this;
    }

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

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

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

    public DscNodeConfigurationImpl withConfiguration(DscConfigurationAssociationProperty configuration) {
        if (isInCreateMode()) {
            this.createParameters.withConfiguration(configuration);
            return this;
        } else {
            this.updateParameters.withConfiguration(configuration);
            return this;
        }
    }

    public DscNodeConfigurationImpl withIncrementNodeConfigurationBuild(Boolean incrementNodeConfigurationBuild) {
        if (isInCreateMode()) {
            this.createParameters.withIncrementNodeConfigurationBuild(incrementNodeConfigurationBuild);
            return this;
        } else {
            this.updateParameters.withIncrementNodeConfigurationBuild(incrementNodeConfigurationBuild);
            return this;
        }
    }

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