// 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.ConnectionInner;
import com.azure.resourcemanager.automation.models.Connection;
import com.azure.resourcemanager.automation.models.ConnectionCreateOrUpdateParameters;
import com.azure.resourcemanager.automation.models.ConnectionTypeAssociationProperty;
import com.azure.resourcemanager.automation.models.ConnectionUpdateParameters;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class ConnectionImpl implements Connection, Connection.Definition, Connection.Update {
    private ConnectionInner 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 ConnectionTypeAssociationProperty connectionType() {
        return this.innerModel().connectionType();
    }

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String automationAccountName;

    private String connectionName;

    private ConnectionCreateOrUpdateParameters createParameters;

    private ConnectionUpdateParameters updateParameters;

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

    public Connection create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnections()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, connectionName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Connection create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnections()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, connectionName, createParameters, context)
                .getValue();
        return this;
    }

    ConnectionImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new ConnectionInner();
        this.serviceManager = serviceManager;
        this.connectionName = name;
        this.createParameters = new ConnectionCreateOrUpdateParameters();
    }

    public ConnectionImpl update() {
        this.updateParameters = new ConnectionUpdateParameters();
        return this;
    }

    public Connection apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnections()
                .updateWithResponse(
                    resourceGroupName, automationAccountName, connectionName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Connection apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnections()
                .updateWithResponse(resourceGroupName, automationAccountName, connectionName, updateParameters, context)
                .getValue();
        return this;
    }

    ConnectionImpl(ConnectionInner 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.connectionName = Utils.getValueFromIdByName(innerObject.id(), "connections");
    }

    public Connection refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnections()
                .getWithResponse(resourceGroupName, automationAccountName, connectionName, Context.NONE)
                .getValue();
        return this;
    }

    public Connection refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnections()
                .getWithResponse(resourceGroupName, automationAccountName, connectionName, context)
                .getValue();
        return this;
    }

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

    public ConnectionImpl withConnectionType(ConnectionTypeAssociationProperty connectionType) {
        this.createParameters.withConnectionType(connectionType);
        return this;
    }

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

    public ConnectionImpl withFieldDefinitionValues(Map<String, String> fieldDefinitionValues) {
        if (isInCreateMode()) {
            this.createParameters.withFieldDefinitionValues(fieldDefinitionValues);
            return this;
        } else {
            this.updateParameters.withFieldDefinitionValues(fieldDefinitionValues);
            return this;
        }
    }

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