// 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.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.dynatrace.fluent.MonitorsClient;
import com.azure.resourcemanager.dynatrace.fluent.models.AccountInfoSecureInner;
import com.azure.resourcemanager.dynatrace.fluent.models.AppServiceInfoInner;
import com.azure.resourcemanager.dynatrace.fluent.models.LinkableEnvironmentResponseInner;
import com.azure.resourcemanager.dynatrace.fluent.models.MonitorResourceInner;
import com.azure.resourcemanager.dynatrace.fluent.models.MonitoredResourceInner;
import com.azure.resourcemanager.dynatrace.fluent.models.SsoDetailsResponseInner;
import com.azure.resourcemanager.dynatrace.fluent.models.VMExtensionPayloadInner;
import com.azure.resourcemanager.dynatrace.fluent.models.VMInfoInner;
import com.azure.resourcemanager.dynatrace.models.AccountInfoSecure;
import com.azure.resourcemanager.dynatrace.models.AppServiceInfo;
import com.azure.resourcemanager.dynatrace.models.LinkableEnvironmentRequest;
import com.azure.resourcemanager.dynatrace.models.LinkableEnvironmentResponse;
import com.azure.resourcemanager.dynatrace.models.MonitorResource;
import com.azure.resourcemanager.dynatrace.models.MonitoredResource;
import com.azure.resourcemanager.dynatrace.models.Monitors;
import com.azure.resourcemanager.dynatrace.models.SsoDetailsRequest;
import com.azure.resourcemanager.dynatrace.models.SsoDetailsResponse;
import com.azure.resourcemanager.dynatrace.models.VMExtensionPayload;
import com.azure.resourcemanager.dynatrace.models.VMInfo;

public final class MonitorsImpl implements Monitors {
    private static final ClientLogger LOGGER = new ClientLogger(MonitorsImpl.class);

    private final MonitorsClient innerClient;

    private final com.azure.resourcemanager.dynatrace.DynatraceManager serviceManager;

    public MonitorsImpl(
        MonitorsClient innerClient, com.azure.resourcemanager.dynatrace.DynatraceManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public AccountInfoSecure getAccountCredentials(String resourceGroupName, String monitorName) {
        AccountInfoSecureInner inner = this.serviceClient().getAccountCredentials(resourceGroupName, monitorName);
        if (inner != null) {
            return new AccountInfoSecureImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AccountInfoSecure> getAccountCredentialsWithResponse(
        String resourceGroupName, String monitorName, Context context) {
        Response<AccountInfoSecureInner> inner =
            this.serviceClient().getAccountCredentialsWithResponse(resourceGroupName, monitorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AccountInfoSecureImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<MonitoredResource> listMonitoredResources(String resourceGroupName, String monitorName) {
        PagedIterable<MonitoredResourceInner> inner =
            this.serviceClient().listMonitoredResources(resourceGroupName, monitorName);
        return Utils.mapPage(inner, inner1 -> new MonitoredResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<MonitoredResource> listMonitoredResources(
        String resourceGroupName, String monitorName, Context context) {
        PagedIterable<MonitoredResourceInner> inner =
            this.serviceClient().listMonitoredResources(resourceGroupName, monitorName, context);
        return Utils.mapPage(inner, inner1 -> new MonitoredResourceImpl(inner1, this.manager()));
    }

    public VMExtensionPayload getVMHostPayload(String resourceGroupName, String monitorName) {
        VMExtensionPayloadInner inner = this.serviceClient().getVMHostPayload(resourceGroupName, monitorName);
        if (inner != null) {
            return new VMExtensionPayloadImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<VMExtensionPayload> getVMHostPayloadWithResponse(
        String resourceGroupName, String monitorName, Context context) {
        Response<VMExtensionPayloadInner> inner =
            this.serviceClient().getVMHostPayloadWithResponse(resourceGroupName, monitorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new VMExtensionPayloadImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public MonitorResource getByResourceGroup(String resourceGroupName, String monitorName) {
        MonitorResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, monitorName);
        if (inner != null) {
            return new MonitorResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<MonitorResource> getByResourceGroupWithResponse(
        String resourceGroupName, String monitorName, Context context) {
        Response<MonitorResourceInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, monitorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new MonitorResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void deleteByResourceGroup(String resourceGroupName, String monitorName) {
        this.serviceClient().delete(resourceGroupName, monitorName);
    }

    public void delete(String resourceGroupName, String monitorName, Context context) {
        this.serviceClient().delete(resourceGroupName, monitorName, context);
    }

    public PagedIterable<MonitorResource> list() {
        PagedIterable<MonitorResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new MonitorResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<MonitorResource> list(Context context) {
        PagedIterable<MonitorResourceInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new MonitorResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<MonitorResource> listByResourceGroup(String resourceGroupName) {
        PagedIterable<MonitorResourceInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new MonitorResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<MonitorResource> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<MonitorResourceInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new MonitorResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<VMInfo> listHosts(String resourceGroupName, String monitorName) {
        PagedIterable<VMInfoInner> inner = this.serviceClient().listHosts(resourceGroupName, monitorName);
        return Utils.mapPage(inner, inner1 -> new VMInfoImpl(inner1, this.manager()));
    }

    public PagedIterable<VMInfo> listHosts(String resourceGroupName, String monitorName, Context context) {
        PagedIterable<VMInfoInner> inner = this.serviceClient().listHosts(resourceGroupName, monitorName, context);
        return Utils.mapPage(inner, inner1 -> new VMInfoImpl(inner1, this.manager()));
    }

    public PagedIterable<AppServiceInfo> listAppServices(String resourceGroupName, String monitorName) {
        PagedIterable<AppServiceInfoInner> inner = this.serviceClient().listAppServices(resourceGroupName, monitorName);
        return Utils.mapPage(inner, inner1 -> new AppServiceInfoImpl(inner1, this.manager()));
    }

    public PagedIterable<AppServiceInfo> listAppServices(
        String resourceGroupName, String monitorName, Context context) {
        PagedIterable<AppServiceInfoInner> inner =
            this.serviceClient().listAppServices(resourceGroupName, monitorName, context);
        return Utils.mapPage(inner, inner1 -> new AppServiceInfoImpl(inner1, this.manager()));
    }

    public SsoDetailsResponse getSsoDetails(String resourceGroupName, String monitorName) {
        SsoDetailsResponseInner inner = this.serviceClient().getSsoDetails(resourceGroupName, monitorName);
        if (inner != null) {
            return new SsoDetailsResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<SsoDetailsResponse> getSsoDetailsWithResponse(
        String resourceGroupName, String monitorName, SsoDetailsRequest request, Context context) {
        Response<SsoDetailsResponseInner> inner =
            this.serviceClient().getSsoDetailsWithResponse(resourceGroupName, monitorName, request, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SsoDetailsResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<LinkableEnvironmentResponse> listLinkableEnvironments(
        String resourceGroupName, String monitorName, LinkableEnvironmentRequest request) {
        PagedIterable<LinkableEnvironmentResponseInner> inner =
            this.serviceClient().listLinkableEnvironments(resourceGroupName, monitorName, request);
        return Utils.mapPage(inner, inner1 -> new LinkableEnvironmentResponseImpl(inner1, this.manager()));
    }

    public PagedIterable<LinkableEnvironmentResponse> listLinkableEnvironments(
        String resourceGroupName, String monitorName, LinkableEnvironmentRequest request, Context context) {
        PagedIterable<LinkableEnvironmentResponseInner> inner =
            this.serviceClient().listLinkableEnvironments(resourceGroupName, monitorName, request, context);
        return Utils.mapPage(inner, inner1 -> new LinkableEnvironmentResponseImpl(inner1, this.manager()));
    }

    public MonitorResource getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String monitorName = Utils.getValueFromIdByName(id, "monitors");
        if (monitorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'monitors'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, monitorName, Context.NONE).getValue();
    }

    public Response<MonitorResource> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String monitorName = Utils.getValueFromIdByName(id, "monitors");
        if (monitorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'monitors'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, monitorName, context);
    }

    public void deleteById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String monitorName = Utils.getValueFromIdByName(id, "monitors");
        if (monitorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'monitors'.", id)));
        }
        this.delete(resourceGroupName, monitorName, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String monitorName = Utils.getValueFromIdByName(id, "monitors");
        if (monitorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'monitors'.", id)));
        }
        this.delete(resourceGroupName, monitorName, context);
    }

    private MonitorsClient serviceClient() {
        return this.innerClient;
    }

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

    public MonitorResourceImpl define(String name) {
        return new MonitorResourceImpl(name, this.manager());
    }
}
