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

package com.azure.resourcemanager.hybridcontainerservice.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.ProvisionedClustersResponseInner;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClusterIdentity;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClusters;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersAllProperties;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersExtendedLocation;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersPatch;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersResponse;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersResponseExtendedLocation;
import com.azure.resourcemanager.hybridcontainerservice.models.ProvisionedClustersResponseProperties;
import java.util.Collections;
import java.util.Map;

public final class ProvisionedClustersResponseImpl
    implements ProvisionedClustersResponse, ProvisionedClustersResponse.Definition, ProvisionedClustersResponse.Update {
    private ProvisionedClustersResponseInner innerObject;

    private final com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager 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 ProvisionedClusterIdentity identity() {
        return this.innerModel().identity();
    }

    public ProvisionedClustersResponseProperties properties() {
        return this.innerModel().properties();
    }

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

    public ProvisionedClustersResponseExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private ProvisionedClusters createProvisionedClusters;

    private ProvisionedClustersPatch updateProvisionedClusters;

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

    public ProvisionedClustersResponse create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProvisionedClustersOperations()
                .createOrUpdate(resourceGroupName, resourceName, createProvisionedClusters, Context.NONE);
        return this;
    }

    public ProvisionedClustersResponse create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProvisionedClustersOperations()
                .createOrUpdate(resourceGroupName, resourceName, createProvisionedClusters, context);
        return this;
    }

    ProvisionedClustersResponseImpl(
        String name, com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerObject = new ProvisionedClustersResponseInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
        this.createProvisionedClusters = new ProvisionedClusters();
    }

    public ProvisionedClustersResponseImpl update() {
        this.updateProvisionedClusters = new ProvisionedClustersPatch();
        return this;
    }

    public ProvisionedClustersResponse apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProvisionedClustersOperations()
                .update(resourceGroupName, resourceName, updateProvisionedClusters, Context.NONE);
        return this;
    }

    public ProvisionedClustersResponse apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProvisionedClustersOperations()
                .update(resourceGroupName, resourceName, updateProvisionedClusters, context);
        return this;
    }

    ProvisionedClustersResponseImpl(
        ProvisionedClustersResponseInner innerObject,
        com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "provisionedClusters");
    }

    public ProvisionedClustersResponse refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProvisionedClustersOperations()
                .getByResourceGroupWithResponse(resourceGroupName, resourceName, Context.NONE)
                .getValue();
        return this;
    }

    public ProvisionedClustersResponse refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getProvisionedClustersOperations()
                .getByResourceGroupWithResponse(resourceGroupName, resourceName, context)
                .getValue();
        return this;
    }

    public void upgradeNodeImageVersionForEntireCluster() {
        serviceManager
            .provisionedClustersOperations()
            .upgradeNodeImageVersionForEntireCluster(resourceGroupName, resourceName);
    }

    public void upgradeNodeImageVersionForEntireCluster(Context context) {
        serviceManager
            .provisionedClustersOperations()
            .upgradeNodeImageVersionForEntireCluster(resourceGroupName, resourceName, context);
    }

    public ProvisionedClustersResponseImpl withRegion(Region location) {
        this.createProvisionedClusters.withLocation(location.toString());
        return this;
    }

    public ProvisionedClustersResponseImpl withRegion(String location) {
        this.createProvisionedClusters.withLocation(location);
        return this;
    }

    public ProvisionedClustersResponseImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.createProvisionedClusters.withTags(tags);
            return this;
        } else {
            this.updateProvisionedClusters.withTags(tags);
            return this;
        }
    }

    public ProvisionedClustersResponseImpl withIdentity(ProvisionedClusterIdentity identity) {
        this.createProvisionedClusters.withIdentity(identity);
        return this;
    }

    public ProvisionedClustersResponseImpl withProperties(ProvisionedClustersAllProperties properties) {
        this.createProvisionedClusters.withProperties(properties);
        return this;
    }

    public ProvisionedClustersResponseImpl withExtendedLocation(ProvisionedClustersExtendedLocation extendedLocation) {
        this.createProvisionedClusters.withExtendedLocation(extendedLocation);
        return this;
    }

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