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

package com.azure.resourcemanager.alertsmanagement.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.alertsmanagement.fluent.AlertsClient;
import com.azure.resourcemanager.alertsmanagement.fluent.models.AlertInner;
import com.azure.resourcemanager.alertsmanagement.fluent.models.AlertModificationInner;
import com.azure.resourcemanager.alertsmanagement.fluent.models.AlertsMetadataInner;
import com.azure.resourcemanager.alertsmanagement.fluent.models.AlertsSummaryInner;
import com.azure.resourcemanager.alertsmanagement.models.Alert;
import com.azure.resourcemanager.alertsmanagement.models.AlertModification;
import com.azure.resourcemanager.alertsmanagement.models.AlertState;
import com.azure.resourcemanager.alertsmanagement.models.Alerts;
import com.azure.resourcemanager.alertsmanagement.models.AlertsMetadata;
import com.azure.resourcemanager.alertsmanagement.models.AlertsSortByFields;
import com.azure.resourcemanager.alertsmanagement.models.AlertsSummary;
import com.azure.resourcemanager.alertsmanagement.models.AlertsSummaryGroupByFields;
import com.azure.resourcemanager.alertsmanagement.models.Identifier;
import com.azure.resourcemanager.alertsmanagement.models.MonitorCondition;
import com.azure.resourcemanager.alertsmanagement.models.MonitorService;
import com.azure.resourcemanager.alertsmanagement.models.Severity;
import com.azure.resourcemanager.alertsmanagement.models.SortOrder;
import com.azure.resourcemanager.alertsmanagement.models.TimeRange;

public final class AlertsImpl implements Alerts {
    private static final ClientLogger LOGGER = new ClientLogger(AlertsImpl.class);

    private final AlertsClient innerClient;

    private final com.azure.resourcemanager.alertsmanagement.AlertsManagementManager serviceManager;

    public AlertsImpl(
        AlertsClient innerClient, com.azure.resourcemanager.alertsmanagement.AlertsManagementManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public AlertsMetadata metadata(Identifier identifier) {
        AlertsMetadataInner inner = this.serviceClient().metadata(identifier);
        if (inner != null) {
            return new AlertsMetadataImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AlertsMetadata> metadataWithResponse(Identifier identifier, Context context) {
        Response<AlertsMetadataInner> inner = this.serviceClient().metadataWithResponse(identifier, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertsMetadataImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<Alert> list() {
        PagedIterable<AlertInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new AlertImpl(inner1, this.manager()));
    }

    public PagedIterable<Alert> list(
        String targetResource,
        String targetResourceType,
        String targetResourceGroup,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState alertState,
        String alertRule,
        String smartGroupId,
        Boolean includeContext,
        Boolean includeEgressConfig,
        Long pageCount,
        AlertsSortByFields sortBy,
        SortOrder sortOrder,
        String select,
        TimeRange timeRange,
        String customTimeRange,
        Context context) {
        PagedIterable<AlertInner> inner =
            this
                .serviceClient()
                .list(
                    targetResource,
                    targetResourceType,
                    targetResourceGroup,
                    monitorService,
                    monitorCondition,
                    severity,
                    alertState,
                    alertRule,
                    smartGroupId,
                    includeContext,
                    includeEgressConfig,
                    pageCount,
                    sortBy,
                    sortOrder,
                    select,
                    timeRange,
                    customTimeRange,
                    context);
        return Utils.mapPage(inner, inner1 -> new AlertImpl(inner1, this.manager()));
    }

    public Alert getById(String alertId) {
        AlertInner inner = this.serviceClient().getById(alertId);
        if (inner != null) {
            return new AlertImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Alert> getByIdWithResponse(String alertId, Context context) {
        Response<AlertInner> inner = this.serviceClient().getByIdWithResponse(alertId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Alert changeState(String alertId, AlertState newState) {
        AlertInner inner = this.serviceClient().changeState(alertId, newState);
        if (inner != null) {
            return new AlertImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Alert> changeStateWithResponse(
        String alertId, AlertState newState, String comment, Context context) {
        Response<AlertInner> inner = this.serviceClient().changeStateWithResponse(alertId, newState, comment, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AlertModification getHistory(String alertId) {
        AlertModificationInner inner = this.serviceClient().getHistory(alertId);
        if (inner != null) {
            return new AlertModificationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AlertModification> getHistoryWithResponse(String alertId, Context context) {
        Response<AlertModificationInner> inner = this.serviceClient().getHistoryWithResponse(alertId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertModificationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AlertsSummary getSummary(AlertsSummaryGroupByFields groupby) {
        AlertsSummaryInner inner = this.serviceClient().getSummary(groupby);
        if (inner != null) {
            return new AlertsSummaryImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AlertsSummary> getSummaryWithResponse(
        AlertsSummaryGroupByFields groupby,
        Boolean includeSmartGroupsCount,
        String targetResource,
        String targetResourceType,
        String targetResourceGroup,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState alertState,
        String alertRule,
        TimeRange timeRange,
        String customTimeRange,
        Context context) {
        Response<AlertsSummaryInner> inner =
            this
                .serviceClient()
                .getSummaryWithResponse(
                    groupby,
                    includeSmartGroupsCount,
                    targetResource,
                    targetResourceType,
                    targetResourceGroup,
                    monitorService,
                    monitorCondition,
                    severity,
                    alertState,
                    alertRule,
                    timeRange,
                    customTimeRange,
                    context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertsSummaryImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

    private com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager() {
        return this.serviceManager;
    }
}
