// 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.SmartGroupsClient;
import com.azure.resourcemanager.alertsmanagement.fluent.models.SmartGroupInner;
import com.azure.resourcemanager.alertsmanagement.fluent.models.SmartGroupModificationInner;
import com.azure.resourcemanager.alertsmanagement.models.AlertState;
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.SmartGroup;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupModification;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroups;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsChangeStateResponse;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsGetByIdResponse;
import com.azure.resourcemanager.alertsmanagement.models.SmartGroupsSortByFields;
import com.azure.resourcemanager.alertsmanagement.models.SortOrder;
import com.azure.resourcemanager.alertsmanagement.models.TimeRange;

public final class SmartGroupsImpl implements SmartGroups {
    private static final ClientLogger LOGGER = new ClientLogger(SmartGroupsImpl.class);

    private final SmartGroupsClient innerClient;

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

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

    public PagedIterable<SmartGroup> list() {
        PagedIterable<SmartGroupInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new SmartGroupImpl(inner1, this.manager()));
    }

    public PagedIterable<SmartGroup> list(
        String targetResource,
        String targetResourceGroup,
        String targetResourceType,
        MonitorService monitorService,
        MonitorCondition monitorCondition,
        Severity severity,
        AlertState smartGroupState,
        TimeRange timeRange,
        Long pageCount,
        SmartGroupsSortByFields sortBy,
        SortOrder sortOrder,
        Context context) {
        PagedIterable<SmartGroupInner> inner =
            this
                .serviceClient()
                .list(
                    targetResource,
                    targetResourceGroup,
                    targetResourceType,
                    monitorService,
                    monitorCondition,
                    severity,
                    smartGroupState,
                    timeRange,
                    pageCount,
                    sortBy,
                    sortOrder,
                    context);
        return Utils.mapPage(inner, inner1 -> new SmartGroupImpl(inner1, this.manager()));
    }

    public SmartGroup getById(String smartGroupId) {
        SmartGroupInner inner = this.serviceClient().getById(smartGroupId);
        if (inner != null) {
            return new SmartGroupImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<SmartGroup> getByIdWithResponse(String smartGroupId, Context context) {
        SmartGroupsGetByIdResponse inner = this.serviceClient().getByIdWithResponse(smartGroupId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SmartGroupImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public SmartGroup changeState(String smartGroupId, AlertState newState) {
        SmartGroupInner inner = this.serviceClient().changeState(smartGroupId, newState);
        if (inner != null) {
            return new SmartGroupImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<SmartGroup> changeStateWithResponse(String smartGroupId, AlertState newState, Context context) {
        SmartGroupsChangeStateResponse inner =
            this.serviceClient().changeStateWithResponse(smartGroupId, newState, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SmartGroupImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public SmartGroupModification getHistory(String smartGroupId) {
        SmartGroupModificationInner inner = this.serviceClient().getHistory(smartGroupId);
        if (inner != null) {
            return new SmartGroupModificationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<SmartGroupModification> getHistoryWithResponse(String smartGroupId, Context context) {
        Response<SmartGroupModificationInner> inner =
            this.serviceClient().getHistoryWithResponse(smartGroupId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SmartGroupModificationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

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