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

package com.azure.resourcemanager.dashboard.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.dashboard.fluent.models.ManagedGrafanaInner;
import com.azure.resourcemanager.dashboard.models.ManagedGrafana;
import com.azure.resourcemanager.dashboard.models.ManagedGrafanaProperties;
import com.azure.resourcemanager.dashboard.models.ManagedGrafanaPropertiesUpdateParameters;
import com.azure.resourcemanager.dashboard.models.ManagedGrafanaUpdateParameters;
import com.azure.resourcemanager.dashboard.models.ManagedServiceIdentity;
import com.azure.resourcemanager.dashboard.models.ResourceSku;
import java.util.Collections;
import java.util.Map;

public final class ManagedGrafanaImpl implements ManagedGrafana, ManagedGrafana.Definition, ManagedGrafana.Update {
    private ManagedGrafanaInner innerObject;

    private final com.azure.resourcemanager.dashboard.DashboardManager 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 ResourceSku sku() {
        return this.innerModel().sku();
    }

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

    public ManagedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.dashboard.DashboardManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String workspaceName;

    private ManagedGrafanaUpdateParameters updateRequestBodyParameters;

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

    public ManagedGrafana create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getGrafanas()
                .create(resourceGroupName, workspaceName, this.innerModel(), Context.NONE);
        return this;
    }

    public ManagedGrafana create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getGrafanas()
                .create(resourceGroupName, workspaceName, this.innerModel(), context);
        return this;
    }

    ManagedGrafanaImpl(String name, com.azure.resourcemanager.dashboard.DashboardManager serviceManager) {
        this.innerObject = new ManagedGrafanaInner();
        this.serviceManager = serviceManager;
        this.workspaceName = name;
    }

    public ManagedGrafanaImpl update() {
        this.updateRequestBodyParameters = new ManagedGrafanaUpdateParameters();
        return this;
    }

    public ManagedGrafana apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getGrafanas()
                .updateWithResponse(resourceGroupName, workspaceName, updateRequestBodyParameters, Context.NONE)
                .getValue();
        return this;
    }

    public ManagedGrafana apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getGrafanas()
                .updateWithResponse(resourceGroupName, workspaceName, updateRequestBodyParameters, context)
                .getValue();
        return this;
    }

    ManagedGrafanaImpl(
        ManagedGrafanaInner innerObject, com.azure.resourcemanager.dashboard.DashboardManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "grafana");
    }

    public ManagedGrafana refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getGrafanas()
                .getByResourceGroupWithResponse(resourceGroupName, workspaceName, Context.NONE)
                .getValue();
        return this;
    }

    public ManagedGrafana refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getGrafanas()
                .getByResourceGroupWithResponse(resourceGroupName, workspaceName, context)
                .getValue();
        return this;
    }

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

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

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

    public ManagedGrafanaImpl withSku(ResourceSku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public ManagedGrafanaImpl withProperties(ManagedGrafanaProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public ManagedGrafanaImpl withIdentity(ManagedServiceIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateRequestBodyParameters.withIdentity(identity);
            return this;
        }
    }

    public ManagedGrafanaImpl withProperties(ManagedGrafanaPropertiesUpdateParameters properties) {
        this.updateRequestBodyParameters.withProperties(properties);
        return this;
    }

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