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

package com.azure.resourcemanager.newrelicobservability.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.newrelicobservability.fluent.MonitorsClient;
import com.azure.resourcemanager.newrelicobservability.fluent.models.AppServiceInfoInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.MetricRulesInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.MetricsStatusResponseInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.MonitoredResourceInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.NewRelicMonitorResourceInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.VMExtensionPayloadInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.VMInfoInner;
import com.azure.resourcemanager.newrelicobservability.models.AppServiceInfo;
import com.azure.resourcemanager.newrelicobservability.models.AppServicesGetRequest;
import com.azure.resourcemanager.newrelicobservability.models.HostsGetRequest;
import com.azure.resourcemanager.newrelicobservability.models.MetricRules;
import com.azure.resourcemanager.newrelicobservability.models.MetricsRequest;
import com.azure.resourcemanager.newrelicobservability.models.MetricsStatusRequest;
import com.azure.resourcemanager.newrelicobservability.models.MetricsStatusResponse;
import com.azure.resourcemanager.newrelicobservability.models.MonitoredResource;
import com.azure.resourcemanager.newrelicobservability.models.Monitors;
import com.azure.resourcemanager.newrelicobservability.models.MonitorsSwitchBillingResponse;
import com.azure.resourcemanager.newrelicobservability.models.NewRelicMonitorResource;
import com.azure.resourcemanager.newrelicobservability.models.SwitchBillingRequest;
import com.azure.resourcemanager.newrelicobservability.models.VMExtensionPayload;
import com.azure.resourcemanager.newrelicobservability.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.newrelicobservability.NewRelicObservabilityManager serviceManager;

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

    public PagedIterable<NewRelicMonitorResource> list() {
        PagedIterable<NewRelicMonitorResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new NewRelicMonitorResourceImpl(inner1, this.manager()));
    }

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

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

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

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

    public NewRelicMonitorResource getByResourceGroup(String resourceGroupName, String monitorName) {
        NewRelicMonitorResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, monitorName);
        if (inner != null) {
            return new NewRelicMonitorResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<MetricRules> getMetricRulesWithResponse(
        String resourceGroupName, String monitorName, MetricsRequest request, Context context) {
        Response<MetricRulesInner> inner =
            this.serviceClient().getMetricRulesWithResponse(resourceGroupName, monitorName, request, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new MetricRulesImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public MetricRules getMetricRules(String resourceGroupName, String monitorName, MetricsRequest request) {
        MetricRulesInner inner = this.serviceClient().getMetricRules(resourceGroupName, monitorName, request);
        if (inner != null) {
            return new MetricRulesImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<MetricsStatusResponse> getMetricStatusWithResponse(
        String resourceGroupName, String monitorName, MetricsStatusRequest request, Context context) {
        Response<MetricsStatusResponseInner> inner =
            this.serviceClient().getMetricStatusWithResponse(resourceGroupName, monitorName, request, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new MetricsStatusResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public MetricsStatusResponse getMetricStatus(
        String resourceGroupName, String monitorName, MetricsStatusRequest request) {
        MetricsStatusResponseInner inner =
            this.serviceClient().getMetricStatus(resourceGroupName, monitorName, request);
        if (inner != null) {
            return new MetricsStatusResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<NewRelicMonitorResource> switchBillingWithResponse(
        String resourceGroupName, String monitorName, SwitchBillingRequest request, Context context) {
        MonitorsSwitchBillingResponse inner =
            this.serviceClient().switchBillingWithResponse(resourceGroupName, monitorName, request, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new NewRelicMonitorResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public NewRelicMonitorResource switchBilling(
        String resourceGroupName, String monitorName, SwitchBillingRequest request) {
        NewRelicMonitorResourceInner inner =
            this.serviceClient().switchBilling(resourceGroupName, monitorName, request);
        if (inner != null) {
            return new NewRelicMonitorResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    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 Response<VMExtensionPayload> vmHostPayloadWithResponse(
        String resourceGroupName, String monitorName, Context context) {
        Response<VMExtensionPayloadInner> inner =
            this.serviceClient().vmHostPayloadWithResponse(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 VMExtensionPayload vmHostPayload(String resourceGroupName, String monitorName) {
        VMExtensionPayloadInner inner = this.serviceClient().vmHostPayload(resourceGroupName, monitorName);
        if (inner != null) {
            return new VMExtensionPayloadImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public NewRelicMonitorResource 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<NewRelicMonitorResource> 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);
    }

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

    private com.azure.resourcemanager.newrelicobservability.NewRelicObservabilityManager manager() {
        return this.serviceManager;
    }

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