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

package com.azure.resourcemanager.dynatrace.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.dynatrace.fluent.models.MonitorResourceInner;
import com.azure.resourcemanager.dynatrace.models.AccountInfoSecure;
import com.azure.resourcemanager.dynatrace.models.AppServiceInfo;
import com.azure.resourcemanager.dynatrace.models.DynatraceEnvironmentProperties;
import com.azure.resourcemanager.dynatrace.models.IdentityProperties;
import com.azure.resourcemanager.dynatrace.models.LiftrResourceCategories;
import com.azure.resourcemanager.dynatrace.models.LinkableEnvironmentRequest;
import com.azure.resourcemanager.dynatrace.models.LinkableEnvironmentResponse;
import com.azure.resourcemanager.dynatrace.models.MarketplaceSubscriptionStatus;
import com.azure.resourcemanager.dynatrace.models.MonitorResource;
import com.azure.resourcemanager.dynatrace.models.MonitorResourceUpdate;
import com.azure.resourcemanager.dynatrace.models.MonitoredResource;
import com.azure.resourcemanager.dynatrace.models.MonitoringStatus;
import com.azure.resourcemanager.dynatrace.models.PlanData;
import com.azure.resourcemanager.dynatrace.models.ProvisioningState;
import com.azure.resourcemanager.dynatrace.models.SsoDetailsRequest;
import com.azure.resourcemanager.dynatrace.models.SsoDetailsResponse;
import com.azure.resourcemanager.dynatrace.models.UserInfo;
import com.azure.resourcemanager.dynatrace.models.VMExtensionPayload;
import com.azure.resourcemanager.dynatrace.models.VMInfo;
import java.util.Collections;
import java.util.Map;

public final class MonitorResourceImpl implements MonitorResource, MonitorResource.Definition, MonitorResource.Update {
    private MonitorResourceInner innerObject;

    private final com.azure.resourcemanager.dynatrace.DynatraceManager 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 IdentityProperties identity() {
        return this.innerModel().identity();
    }

    public MonitoringStatus monitoringStatus() {
        return this.innerModel().monitoringStatus();
    }

    public MarketplaceSubscriptionStatus marketplaceSubscriptionStatus() {
        return this.innerModel().marketplaceSubscriptionStatus();
    }

    public DynatraceEnvironmentProperties dynatraceEnvironmentProperties() {
        return this.innerModel().dynatraceEnvironmentProperties();
    }

    public UserInfo userInfo() {
        return this.innerModel().userInfo();
    }

    public PlanData planData() {
        return this.innerModel().planData();
    }

    public LiftrResourceCategories liftrResourceCategory() {
        return this.innerModel().liftrResourceCategory();
    }

    public Integer liftrResourcePreference() {
        return this.innerModel().liftrResourcePreference();
    }

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

    private com.azure.resourcemanager.dynatrace.DynatraceManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String monitorName;

    private MonitorResourceUpdate updateResource;

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

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

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

    MonitorResourceImpl(String name, com.azure.resourcemanager.dynatrace.DynatraceManager serviceManager) {
        this.innerObject = new MonitorResourceInner();
        this.serviceManager = serviceManager;
        this.monitorName = name;
    }

    public MonitorResourceImpl update() {
        this.updateResource = new MonitorResourceUpdate();
        return this;
    }

    public MonitorResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .updateWithResponse(resourceGroupName, monitorName, updateResource, Context.NONE)
                .getValue();
        return this;
    }

    public MonitorResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .updateWithResponse(resourceGroupName, monitorName, updateResource, context)
                .getValue();
        return this;
    }

    MonitorResourceImpl(
        MonitorResourceInner innerObject, com.azure.resourcemanager.dynatrace.DynatraceManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.monitorName = Utils.getValueFromIdByName(innerObject.id(), "monitors");
    }

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

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

    public AccountInfoSecure getAccountCredentials() {
        return serviceManager.monitors().getAccountCredentials(resourceGroupName, monitorName);
    }

    public Response<AccountInfoSecure> getAccountCredentialsWithResponse(Context context) {
        return serviceManager.monitors().getAccountCredentialsWithResponse(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 VMExtensionPayload getVMHostPayload() {
        return serviceManager.monitors().getVMHostPayload(resourceGroupName, monitorName);
    }

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

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

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

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

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

    public SsoDetailsResponse getSsoDetails() {
        return serviceManager.monitors().getSsoDetails(resourceGroupName, monitorName);
    }

    public Response<SsoDetailsResponse> getSsoDetailsWithResponse(SsoDetailsRequest request, Context context) {
        return serviceManager.monitors().getSsoDetailsWithResponse(resourceGroupName, monitorName, request, context);
    }

    public PagedIterable<LinkableEnvironmentResponse> listLinkableEnvironments(LinkableEnvironmentRequest request) {
        return serviceManager.monitors().listLinkableEnvironments(resourceGroupName, monitorName, request);
    }

    public PagedIterable<LinkableEnvironmentResponse> listLinkableEnvironments(
        LinkableEnvironmentRequest request, Context context) {
        return serviceManager.monitors().listLinkableEnvironments(resourceGroupName, monitorName, request, context);
    }

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

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

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

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

    public MonitorResourceImpl withMonitoringStatus(MonitoringStatus monitoringStatus) {
        if (isInCreateMode()) {
            this.innerModel().withMonitoringStatus(monitoringStatus);
            return this;
        } else {
            this.updateResource.withMonitoringStatus(monitoringStatus);
            return this;
        }
    }

    public MonitorResourceImpl withMarketplaceSubscriptionStatus(
        MarketplaceSubscriptionStatus marketplaceSubscriptionStatus) {
        if (isInCreateMode()) {
            this.innerModel().withMarketplaceSubscriptionStatus(marketplaceSubscriptionStatus);
            return this;
        } else {
            this.updateResource.withMarketplaceSubscriptionStatus(marketplaceSubscriptionStatus);
            return this;
        }
    }

    public MonitorResourceImpl withDynatraceEnvironmentProperties(
        DynatraceEnvironmentProperties dynatraceEnvironmentProperties) {
        if (isInCreateMode()) {
            this.innerModel().withDynatraceEnvironmentProperties(dynatraceEnvironmentProperties);
            return this;
        } else {
            this.updateResource.withDynatraceEnvironmentProperties(dynatraceEnvironmentProperties);
            return this;
        }
    }

    public MonitorResourceImpl withUserInfo(UserInfo userInfo) {
        if (isInCreateMode()) {
            this.innerModel().withUserInfo(userInfo);
            return this;
        } else {
            this.updateResource.withUserInfo(userInfo);
            return this;
        }
    }

    public MonitorResourceImpl withPlanData(PlanData planData) {
        if (isInCreateMode()) {
            this.innerModel().withPlanData(planData);
            return this;
        } else {
            this.updateResource.withPlanData(planData);
            return this;
        }
    }

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