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

package com.azure.resourcemanager.avs.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import com.azure.resourcemanager.avs.fluent.models.ClusterInner;
import com.azure.resourcemanager.avs.models.Cluster;
import com.azure.resourcemanager.avs.models.ClusterProvisioningState;
import com.azure.resourcemanager.avs.models.ClusterUpdate;
import com.azure.resourcemanager.avs.models.ClusterZoneList;
import com.azure.resourcemanager.avs.models.Sku;
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.avs.AvsManager serviceManager;

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

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

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

    public Sku sku() {
        return this.innerModel().sku();
    }

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.avs.AvsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String privateCloudName;

    private String clusterName;

    private ClusterUpdate updateClusterUpdate;

    public ClusterImpl withExistingPrivateCloud(String resourceGroupName, String privateCloudName) {
        this.resourceGroupName = resourceGroupName;
        this.privateCloudName = privateCloudName;
        return this;
    }

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

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

    ClusterImpl(String name, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerObject = new ClusterInner();
        this.serviceManager = serviceManager;
        this.clusterName = name;
    }

    public ClusterImpl update() {
        this.updateClusterUpdate = new ClusterUpdate();
        return this;
    }

    public Cluster apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .update(resourceGroupName, privateCloudName, clusterName, updateClusterUpdate, Context.NONE);
        return this;
    }

    public Cluster apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getClusters()
                .update(resourceGroupName, privateCloudName, clusterName, updateClusterUpdate, context);
        return this;
    }

    ClusterImpl(ClusterInner innerObject, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.privateCloudName = Utils.getValueFromIdByName(innerObject.id(), "privateClouds");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
    }

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

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

    public Response<ClusterZoneList> listZonesWithResponse(Context context) {
        return serviceManager
            .clusters()
            .listZonesWithResponse(resourceGroupName, privateCloudName, clusterName, context);
    }

    public ClusterZoneList listZones() {
        return serviceManager.clusters().listZones(resourceGroupName, privateCloudName, clusterName);
    }

    public ClusterImpl withSku(Sku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public ClusterImpl withClusterSize(Integer clusterSize) {
        if (isInCreateMode()) {
            this.innerModel().withClusterSize(clusterSize);
            return this;
        } else {
            this.updateClusterUpdate.withClusterSize(clusterSize);
            return this;
        }
    }

    public ClusterImpl withHosts(List<String> hosts) {
        if (isInCreateMode()) {
            this.innerModel().withHosts(hosts);
            return this;
        } else {
            this.updateClusterUpdate.withHosts(hosts);
            return this;
        }
    }

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