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

package com.azure.resourcemanager.networkcloud.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.networkcloud.fluent.models.ClusterMetricsConfigurationInner;
import com.azure.resourcemanager.networkcloud.models.ClusterMetricsConfiguration;
import com.azure.resourcemanager.networkcloud.models.ClusterMetricsConfigurationDetailedStatus;
import com.azure.resourcemanager.networkcloud.models.ClusterMetricsConfigurationPatchParameters;
import com.azure.resourcemanager.networkcloud.models.ClusterMetricsConfigurationProvisioningState;
import com.azure.resourcemanager.networkcloud.models.ExtendedLocation;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class ClusterMetricsConfigurationImpl
    implements ClusterMetricsConfiguration, ClusterMetricsConfiguration.Definition, ClusterMetricsConfiguration.Update {
    private ClusterMetricsConfigurationInner innerObject;

    private final com.azure.resourcemanager.networkcloud.NetworkCloudManager 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 ExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

    public long collectionInterval() {
        return this.innerModel().collectionInterval();
    }

    public ClusterMetricsConfigurationDetailedStatus detailedStatus() {
        return this.innerModel().detailedStatus();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String clusterName;

    private String metricsConfigurationName;

    private ClusterMetricsConfigurationPatchParameters updateMetricsConfigurationUpdateParameters;

    public ClusterMetricsConfigurationImpl withExistingCluster(String resourceGroupName, String clusterName) {
        this.resourceGroupName = resourceGroupName;
        this.clusterName = clusterName;
        return this;
    }

    public ClusterMetricsConfiguration create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMetricsConfigurations()
                .createOrUpdate(
                    resourceGroupName, clusterName, metricsConfigurationName, this.innerModel(), Context.NONE);
        return this;
    }

    public ClusterMetricsConfiguration create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMetricsConfigurations()
                .createOrUpdate(resourceGroupName, clusterName, metricsConfigurationName, this.innerModel(), context);
        return this;
    }

    ClusterMetricsConfigurationImpl(
        String name, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = new ClusterMetricsConfigurationInner();
        this.serviceManager = serviceManager;
        this.metricsConfigurationName = name;
    }

    public ClusterMetricsConfigurationImpl update() {
        this.updateMetricsConfigurationUpdateParameters = new ClusterMetricsConfigurationPatchParameters();
        return this;
    }

    public ClusterMetricsConfiguration apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMetricsConfigurations()
                .update(
                    resourceGroupName,
                    clusterName,
                    metricsConfigurationName,
                    updateMetricsConfigurationUpdateParameters,
                    Context.NONE);
        return this;
    }

    public ClusterMetricsConfiguration apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMetricsConfigurations()
                .update(
                    resourceGroupName,
                    clusterName,
                    metricsConfigurationName,
                    updateMetricsConfigurationUpdateParameters,
                    context);
        return this;
    }

    ClusterMetricsConfigurationImpl(
        ClusterMetricsConfigurationInner innerObject,
        com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
        this.metricsConfigurationName = Utils.getValueFromIdByName(innerObject.id(), "metricsConfigurations");
    }

    public ClusterMetricsConfiguration refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMetricsConfigurations()
                .getWithResponse(resourceGroupName, clusterName, metricsConfigurationName, Context.NONE)
                .getValue();
        return this;
    }

    public ClusterMetricsConfiguration refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMetricsConfigurations()
                .getWithResponse(resourceGroupName, clusterName, metricsConfigurationName, context)
                .getValue();
        return this;
    }

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

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

    public ClusterMetricsConfigurationImpl withExtendedLocation(ExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

    public ClusterMetricsConfigurationImpl withCollectionInterval(long collectionInterval) {
        this.innerModel().withCollectionInterval(collectionInterval);
        return this;
    }

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

    public ClusterMetricsConfigurationImpl withEnabledMetrics(List<String> enabledMetrics) {
        if (isInCreateMode()) {
            this.innerModel().withEnabledMetrics(enabledMetrics);
            return this;
        } else {
            this.updateMetricsConfigurationUpdateParameters.withEnabledMetrics(enabledMetrics);
            return this;
        }
    }

    public ClusterMetricsConfigurationImpl withCollectionInterval(Long collectionInterval) {
        this.updateMetricsConfigurationUpdateParameters.withCollectionInterval(collectionInterval);
        return this;
    }

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