// 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.ConnectionTypeInner;
import com.azure.resourcemanager.automation.models.ConnectionType;
import com.azure.resourcemanager.automation.models.ConnectionTypeCreateOrUpdateParameters;
import com.azure.resourcemanager.automation.models.FieldDefinition;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class ConnectionTypeImpl implements ConnectionType, ConnectionType.Definition, ConnectionType.Update {
    private ConnectionTypeInner 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 Boolean isGlobal() {
        return this.innerModel().isGlobal();
    }

    public Map<String, FieldDefinition> fieldDefinitions() {
        Map<String, FieldDefinition> inner = this.innerModel().fieldDefinitions();
        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 ConnectionTypeInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String automationAccountName;

    private String connectionTypeName;

    private ConnectionTypeCreateOrUpdateParameters createParameters;

    private ConnectionTypeCreateOrUpdateParameters updateParameters;

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

    public ConnectionType create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnectionTypes()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, connectionTypeName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public ConnectionType create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnectionTypes()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, connectionTypeName, createParameters, context)
                .getValue();
        return this;
    }

    ConnectionTypeImpl(String name, com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerObject = new ConnectionTypeInner();
        this.serviceManager = serviceManager;
        this.connectionTypeName = name;
        this.createParameters = new ConnectionTypeCreateOrUpdateParameters();
    }

    public ConnectionTypeImpl update() {
        this.updateParameters = new ConnectionTypeCreateOrUpdateParameters();
        return this;
    }

    public ConnectionType apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnectionTypes()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, connectionTypeName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public ConnectionType apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnectionTypes()
                .createOrUpdateWithResponse(
                    resourceGroupName, automationAccountName, connectionTypeName, updateParameters, context)
                .getValue();
        return this;
    }

    ConnectionTypeImpl(
        ConnectionTypeInner 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.connectionTypeName = Utils.getValueFromIdByName(innerObject.id(), "connectionTypes");
    }

    public ConnectionType refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnectionTypes()
                .getWithResponse(resourceGroupName, automationAccountName, connectionTypeName, Context.NONE)
                .getValue();
        return this;
    }

    public ConnectionType refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConnectionTypes()
                .getWithResponse(resourceGroupName, automationAccountName, connectionTypeName, context)
                .getValue();
        return this;
    }

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

    public ConnectionTypeImpl withFieldDefinitions(Map<String, FieldDefinition> fieldDefinitions) {
        if (isInCreateMode()) {
            this.createParameters.withFieldDefinitions(fieldDefinitions);
            return this;
        } else {
            this.updateParameters.withFieldDefinitions(fieldDefinitions);
            return this;
        }
    }

    public ConnectionTypeImpl withIsGlobal(Boolean isGlobal) {
        if (isInCreateMode()) {
            this.createParameters.withIsGlobal(isGlobal);
            return this;
        } else {
            this.updateParameters.withIsGlobal(isGlobal);
            return this;
        }
    }

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