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

package com.azure.resourcemanager.securityinsights.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.securityinsights.fluent.AlertRulesClient;
import com.azure.resourcemanager.securityinsights.fluent.models.AlertRuleInner;
import com.azure.resourcemanager.securityinsights.models.AlertRule;
import com.azure.resourcemanager.securityinsights.models.AlertRules;

public final class AlertRulesImpl implements AlertRules {
    private static final ClientLogger LOGGER = new ClientLogger(AlertRulesImpl.class);

    private final AlertRulesClient innerClient;

    private final com.azure.resourcemanager.securityinsights.SecurityInsightsManager serviceManager;

    public AlertRulesImpl(
        AlertRulesClient innerClient,
        com.azure.resourcemanager.securityinsights.SecurityInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<AlertRule> list(String resourceGroupName, String workspaceName) {
        PagedIterable<AlertRuleInner> inner = this.serviceClient().list(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new AlertRuleImpl(inner1, this.manager()));
    }

    public PagedIterable<AlertRule> list(String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<AlertRuleInner> inner = this.serviceClient().list(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new AlertRuleImpl(inner1, this.manager()));
    }

    public AlertRule get(String resourceGroupName, String workspaceName, String ruleId) {
        AlertRuleInner inner = this.serviceClient().get(resourceGroupName, workspaceName, ruleId);
        if (inner != null) {
            return new AlertRuleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AlertRule> getWithResponse(
        String resourceGroupName, String workspaceName, String ruleId, Context context) {
        Response<AlertRuleInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, ruleId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertRuleImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AlertRule createOrUpdate(
        String resourceGroupName, String workspaceName, String ruleId, AlertRuleInner alertRule) {
        AlertRuleInner inner = this.serviceClient().createOrUpdate(resourceGroupName, workspaceName, ruleId, alertRule);
        if (inner != null) {
            return new AlertRuleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<AlertRule> createOrUpdateWithResponse(
        String resourceGroupName, String workspaceName, String ruleId, AlertRuleInner alertRule, Context context) {
        Response<AlertRuleInner> inner =
            this
                .serviceClient()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, ruleId, alertRule, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AlertRuleImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String workspaceName, String ruleId) {
        this.serviceClient().delete(resourceGroupName, workspaceName, ruleId);
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName, String workspaceName, String ruleId, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, workspaceName, ruleId, context);
    }

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

    private com.azure.resourcemanager.securityinsights.SecurityInsightsManager manager() {
        return this.serviceManager;
    }
}
