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

package com.azure.resourcemanager.datadog.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.datadog.fluent.models.DatadogApiKeyInner;
import com.azure.resourcemanager.datadog.fluent.models.DatadogMonitorResourceInner;
import com.azure.resourcemanager.datadog.models.DatadogApiKey;
import com.azure.resourcemanager.datadog.models.DatadogHost;
import com.azure.resourcemanager.datadog.models.DatadogMonitorResource;
import com.azure.resourcemanager.datadog.models.DatadogMonitorResourceUpdateParameters;
import com.azure.resourcemanager.datadog.models.DatadogSetPasswordLink;
import com.azure.resourcemanager.datadog.models.IdentityProperties;
import com.azure.resourcemanager.datadog.models.LinkedResource;
import com.azure.resourcemanager.datadog.models.MonitorProperties;
import com.azure.resourcemanager.datadog.models.MonitorUpdateProperties;
import com.azure.resourcemanager.datadog.models.MonitoredResource;
import com.azure.resourcemanager.datadog.models.ResourceSku;
import java.util.Collections;
import java.util.Map;

public final class DatadogMonitorResourceImpl
    implements DatadogMonitorResource, DatadogMonitorResource.Definition, DatadogMonitorResource.Update {
    private DatadogMonitorResourceInner innerObject;

    private final com.azure.resourcemanager.datadog.MicrosoftDatadogManager 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 MonitorProperties properties() {
        return this.innerModel().properties();
    }

    public IdentityProperties 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 DatadogMonitorResourceInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.datadog.MicrosoftDatadogManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String monitorName;

    private DatadogMonitorResourceUpdateParameters updateBody;

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

    public DatadogMonitorResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .create(resourceGroupName, monitorName, this.innerModel(), Context.NONE);
        return this;
    }

    public DatadogMonitorResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .create(resourceGroupName, monitorName, this.innerModel(), context);
        return this;
    }

    DatadogMonitorResourceImpl(String name, com.azure.resourcemanager.datadog.MicrosoftDatadogManager serviceManager) {
        this.innerObject = new DatadogMonitorResourceInner();
        this.serviceManager = serviceManager;
        this.monitorName = name;
    }

    public DatadogMonitorResourceImpl update() {
        this.updateBody = new DatadogMonitorResourceUpdateParameters();
        return this;
    }

    public DatadogMonitorResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .update(resourceGroupName, monitorName, updateBody, Context.NONE);
        return this;
    }

    public DatadogMonitorResource apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getMonitors().update(resourceGroupName, monitorName, updateBody, context);
        return this;
    }

    DatadogMonitorResourceImpl(
        DatadogMonitorResourceInner innerObject,
        com.azure.resourcemanager.datadog.MicrosoftDatadogManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.monitorName = Utils.getValueFromIdByName(innerObject.id(), "monitors");
    }

    public DatadogMonitorResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .getByResourceGroupWithResponse(resourceGroupName, monitorName, Context.NONE)
                .getValue();
        return this;
    }

    public DatadogMonitorResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .getByResourceGroupWithResponse(resourceGroupName, monitorName, context)
                .getValue();
        return this;
    }

    public PagedIterable<DatadogApiKey> listApiKeys() {
        return serviceManager.monitors().listApiKeys(resourceGroupName, monitorName);
    }

    public PagedIterable<DatadogApiKey> listApiKeys(Context context) {
        return serviceManager.monitors().listApiKeys(resourceGroupName, monitorName, context);
    }

    public Response<DatadogApiKey> getDefaultKeyWithResponse(Context context) {
        return serviceManager.monitors().getDefaultKeyWithResponse(resourceGroupName, monitorName, context);
    }

    public DatadogApiKey getDefaultKey() {
        return serviceManager.monitors().getDefaultKey(resourceGroupName, monitorName);
    }

    public Response<Void> setDefaultKeyWithResponse(DatadogApiKeyInner body, Context context) {
        return serviceManager.monitors().setDefaultKeyWithResponse(resourceGroupName, monitorName, body, context);
    }

    public void setDefaultKey() {
        serviceManager.monitors().setDefaultKey(resourceGroupName, monitorName);
    }

    public PagedIterable<DatadogHost> listHosts() {
        return serviceManager.monitors().listHosts(resourceGroupName, monitorName);
    }

    public PagedIterable<DatadogHost> listHosts(Context context) {
        return serviceManager.monitors().listHosts(resourceGroupName, monitorName, context);
    }

    public PagedIterable<LinkedResource> listLinkedResources() {
        return serviceManager.monitors().listLinkedResources(resourceGroupName, monitorName);
    }

    public PagedIterable<LinkedResource> listLinkedResources(Context context) {
        return serviceManager.monitors().listLinkedResources(resourceGroupName, monitorName, context);
    }

    public PagedIterable<MonitoredResource> listMonitoredResources() {
        return serviceManager.monitors().listMonitoredResources(resourceGroupName, monitorName);
    }

    public PagedIterable<MonitoredResource> listMonitoredResources(Context context) {
        return serviceManager.monitors().listMonitoredResources(resourceGroupName, monitorName, context);
    }

    public Response<DatadogSetPasswordLink> refreshSetPasswordLinkWithResponse(Context context) {
        return serviceManager.monitors().refreshSetPasswordLinkWithResponse(resourceGroupName, monitorName, context);
    }

    public DatadogSetPasswordLink refreshSetPasswordLink() {
        return serviceManager.monitors().refreshSetPasswordLink(resourceGroupName, monitorName);
    }

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

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

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

    public DatadogMonitorResourceImpl withSku(ResourceSku sku) {
        if (isInCreateMode()) {
            this.innerModel().withSku(sku);
            return this;
        } else {
            this.updateBody.withSku(sku);
            return this;
        }
    }

    public DatadogMonitorResourceImpl withProperties(MonitorProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public DatadogMonitorResourceImpl withIdentity(IdentityProperties identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public DatadogMonitorResourceImpl withProperties(MonitorUpdateProperties properties) {
        this.updateBody.withProperties(properties);
        return this;
    }

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