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

package com.azure.resourcemanager.azurestackhci.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.azurestackhci.fluent.models.ClusterInner;
import com.azure.resourcemanager.azurestackhci.models.Cluster;
import com.azure.resourcemanager.azurestackhci.models.ClusterDesiredProperties;
import com.azure.resourcemanager.azurestackhci.models.ClusterIdentityResponse;
import com.azure.resourcemanager.azurestackhci.models.ClusterPatch;
import com.azure.resourcemanager.azurestackhci.models.ClusterReportedProperties;
import com.azure.resourcemanager.azurestackhci.models.ProvisioningState;
import com.azure.resourcemanager.azurestackhci.models.Status;
import com.azure.resourcemanager.azurestackhci.models.UploadCertificateRequest;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

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

    private final com.azure.resourcemanager.azurestackhci.AzureStackHciManager serviceManager;

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

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

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

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

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

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

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

    public Status status() {
        return this.innerModel().status();
    }

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

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

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

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

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

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

    public ClusterDesiredProperties desiredProperties() {
        return this.innerModel().desiredProperties();
    }

    public ClusterReportedProperties reportedProperties() {
        return this.innerModel().reportedProperties();
    }

    public Float trialDaysRemaining() {
        return this.innerModel().trialDaysRemaining();
    }

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

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

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

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

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

    public Region region() {
        return Region.fromName(this.regionName());
    }

    public String regionName() {
        return this.location();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.azurestackhci.AzureStackHciManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String clusterName;

    private ClusterPatch updateCluster;

    public ClusterImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

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

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

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

    public ClusterImpl update() {
        this.updateCluster = new ClusterPatch();
        return this;
    }

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

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

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

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

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

    public void uploadCertificate(UploadCertificateRequest uploadCertificateRequest) {
        serviceManager.clusters().uploadCertificate(resourceGroupName, clusterName, uploadCertificateRequest);
    }

    public void uploadCertificate(UploadCertificateRequest uploadCertificateRequest, Context context) {
        serviceManager.clusters().uploadCertificate(resourceGroupName, clusterName, uploadCertificateRequest, context);
    }

    public ClusterIdentityResponse createIdentity() {
        return serviceManager.clusters().createIdentity(resourceGroupName, clusterName);
    }

    public ClusterIdentityResponse createIdentity(Context context) {
        return serviceManager.clusters().createIdentity(resourceGroupName, clusterName, context);
    }

    public ClusterImpl withRegion(Region location) {
        this.innerModel().withLocation(location.toString());
        return this;
    }

    public ClusterImpl withRegion(String location) {
        this.innerModel().withLocation(location);
        return this;
    }

    public ClusterImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updateCluster.withTags(tags);
            return this;
        }
    }

    public ClusterImpl withCloudManagementEndpoint(String cloudManagementEndpoint) {
        if (isInCreateMode()) {
            this.innerModel().withCloudManagementEndpoint(cloudManagementEndpoint);
            return this;
        } else {
            this.updateCluster.withCloudManagementEndpoint(cloudManagementEndpoint);
            return this;
        }
    }

    public ClusterImpl withAadClientId(String aadClientId) {
        if (isInCreateMode()) {
            this.innerModel().withAadClientId(aadClientId);
            return this;
        } else {
            this.updateCluster.withAadClientId(aadClientId);
            return this;
        }
    }

    public ClusterImpl withAadTenantId(String aadTenantId) {
        if (isInCreateMode()) {
            this.innerModel().withAadTenantId(aadTenantId);
            return this;
        } else {
            this.updateCluster.withAadTenantId(aadTenantId);
            return this;
        }
    }

    public ClusterImpl withAadApplicationObjectId(String aadApplicationObjectId) {
        this.innerModel().withAadApplicationObjectId(aadApplicationObjectId);
        return this;
    }

    public ClusterImpl withAadServicePrincipalObjectId(String aadServicePrincipalObjectId) {
        this.innerModel().withAadServicePrincipalObjectId(aadServicePrincipalObjectId);
        return this;
    }

    public ClusterImpl withDesiredProperties(ClusterDesiredProperties desiredProperties) {
        if (isInCreateMode()) {
            this.innerModel().withDesiredProperties(desiredProperties);
            return this;
        } else {
            this.updateCluster.withDesiredProperties(desiredProperties);
            return this;
        }
    }

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