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

package com.azure.resourcemanager.batchai.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.util.Context;
import com.azure.resourcemanager.batchai.fluent.models.ClusterInner;
import com.azure.resourcemanager.batchai.models.AllocationState;
import com.azure.resourcemanager.batchai.models.BatchAIError;
import com.azure.resourcemanager.batchai.models.Cluster;
import com.azure.resourcemanager.batchai.models.ClusterCreateParameters;
import com.azure.resourcemanager.batchai.models.ClusterUpdateParameters;
import com.azure.resourcemanager.batchai.models.NodeSetup;
import com.azure.resourcemanager.batchai.models.NodeStateCounts;
import com.azure.resourcemanager.batchai.models.ProvisioningState;
import com.azure.resourcemanager.batchai.models.RemoteLoginInformation;
import com.azure.resourcemanager.batchai.models.ResourceId;
import com.azure.resourcemanager.batchai.models.ScaleSettings;
import com.azure.resourcemanager.batchai.models.UserAccountSettings;
import com.azure.resourcemanager.batchai.models.VirtualMachineConfiguration;
import com.azure.resourcemanager.batchai.models.VmPriority;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;

public final class ClusterImpl implements Cluster, Cluster.Definition, Cluster.Update {
    private ClusterInner innerObject;

    private final com.azure.resourcemanager.batchai.BatchAIManager serviceManager;

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

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

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

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

    public VmPriority vmPriority() {
        return this.innerModel().vmPriority();
    }

    public ScaleSettings scaleSettings() {
        return this.innerModel().scaleSettings();
    }

    public VirtualMachineConfiguration virtualMachineConfiguration() {
        return this.innerModel().virtualMachineConfiguration();
    }

    public NodeSetup nodeSetup() {
        return this.innerModel().nodeSetup();
    }

    public UserAccountSettings userAccountSettings() {
        return this.innerModel().userAccountSettings();
    }

    public ResourceId subnet() {
        return this.innerModel().subnet();
    }

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

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

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

    public AllocationState allocationState() {
        return this.innerModel().allocationState();
    }

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

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

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

    public NodeStateCounts nodeStateCounts() {
        return this.innerModel().nodeStateCounts();
    }

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

    private com.azure.resourcemanager.batchai.BatchAIManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String workspaceName;

    private String clusterName;

    private ClusterCreateParameters createParameters;

    private ClusterUpdateParameters updateParameters;

    public ClusterImpl withExistingWorkspace(String resourceGroupName, String workspaceName) {
        this.resourceGroupName = resourceGroupName;
        this.workspaceName = workspaceName;
        return this;
    }

    public Cluster create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .create(resourceGroupName, workspaceName, clusterName, createParameters, Context.NONE);
        return this;
    }

    public Cluster create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .create(resourceGroupName, workspaceName, clusterName, createParameters, context);
        return this;
    }

    ClusterImpl(String name, com.azure.resourcemanager.batchai.BatchAIManager serviceManager) {
        this.innerObject = new ClusterInner();
        this.serviceManager = serviceManager;
        this.clusterName = name;
        this.createParameters = new ClusterCreateParameters();
    }

    public ClusterImpl update() {
        this.updateParameters = new ClusterUpdateParameters();
        return this;
    }

    public Cluster apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .updateWithResponse(resourceGroupName, workspaceName, clusterName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Cluster apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .updateWithResponse(resourceGroupName, workspaceName, clusterName, updateParameters, context)
                .getValue();
        return this;
    }

    ClusterImpl(ClusterInner innerObject, com.azure.resourcemanager.batchai.BatchAIManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "workspaces");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
    }

    public Cluster refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .getWithResponse(resourceGroupName, workspaceName, clusterName, Context.NONE)
                .getValue();
        return this;
    }

    public Cluster refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .getWithResponse(resourceGroupName, workspaceName, clusterName, context)
                .getValue();
        return this;
    }

    public PagedIterable<RemoteLoginInformation> listRemoteLoginInformation() {
        return serviceManager.clusters().listRemoteLoginInformation(resourceGroupName, workspaceName, clusterName);
    }

    public PagedIterable<RemoteLoginInformation> listRemoteLoginInformation(Context context) {
        return serviceManager
            .clusters()
            .listRemoteLoginInformation(resourceGroupName, workspaceName, clusterName, context);
    }

    public ClusterImpl withVmSize(String vmSize) {
        this.createParameters.withVmSize(vmSize);
        return this;
    }

    public ClusterImpl withVmPriority(VmPriority vmPriority) {
        this.createParameters.withVmPriority(vmPriority);
        return this;
    }

    public ClusterImpl withScaleSettings(ScaleSettings scaleSettings) {
        if (isInCreateMode()) {
            this.createParameters.withScaleSettings(scaleSettings);
            return this;
        } else {
            this.updateParameters.withScaleSettings(scaleSettings);
            return this;
        }
    }

    public ClusterImpl withVirtualMachineConfiguration(VirtualMachineConfiguration virtualMachineConfiguration) {
        this.createParameters.withVirtualMachineConfiguration(virtualMachineConfiguration);
        return this;
    }

    public ClusterImpl withNodeSetup(NodeSetup nodeSetup) {
        this.createParameters.withNodeSetup(nodeSetup);
        return this;
    }

    public ClusterImpl withUserAccountSettings(UserAccountSettings userAccountSettings) {
        this.createParameters.withUserAccountSettings(userAccountSettings);
        return this;
    }

    public ClusterImpl withSubnet(ResourceId subnet) {
        this.createParameters.withSubnet(subnet);
        return this;
    }

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