// 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.ActionsClient;
import com.azure.resourcemanager.securityinsights.fluent.models.ActionResponseInner;
import com.azure.resourcemanager.securityinsights.models.ActionResponse;
import com.azure.resourcemanager.securityinsights.models.Actions;

public final class ActionsImpl implements Actions {
    private static final ClientLogger LOGGER = new ClientLogger(ActionsImpl.class);

    private final ActionsClient innerClient;

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

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

    public PagedIterable<ActionResponse> listByAlertRule(
        String resourceGroupName, String workspaceName, String ruleId) {
        PagedIterable<ActionResponseInner> inner =
            this.serviceClient().listByAlertRule(resourceGroupName, workspaceName, ruleId);
        return Utils.mapPage(inner, inner1 -> new ActionResponseImpl(inner1, this.manager()));
    }

    public PagedIterable<ActionResponse> listByAlertRule(
        String resourceGroupName, String workspaceName, String ruleId, Context context) {
        PagedIterable<ActionResponseInner> inner =
            this.serviceClient().listByAlertRule(resourceGroupName, workspaceName, ruleId, context);
        return Utils.mapPage(inner, inner1 -> new ActionResponseImpl(inner1, this.manager()));
    }

    public ActionResponse get(String resourceGroupName, String workspaceName, String ruleId, String actionId) {
        ActionResponseInner inner = this.serviceClient().get(resourceGroupName, workspaceName, ruleId, actionId);
        if (inner != null) {
            return new ActionResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public ActionResponse 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleId = Utils.getValueFromIdByName(id, "alertRules");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'alertRules'.", id)));
        }
        String actionId = Utils.getValueFromIdByName(id, "actions");
        if (actionId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'actions'.", id)));
        }
        return this.getWithResponse(resourceGroupName, workspaceName, ruleId, actionId, Context.NONE).getValue();
    }

    public Response<ActionResponse> 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleId = Utils.getValueFromIdByName(id, "alertRules");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'alertRules'.", id)));
        }
        String actionId = Utils.getValueFromIdByName(id, "actions");
        if (actionId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'actions'.", id)));
        }
        return this.getWithResponse(resourceGroupName, workspaceName, ruleId, actionId, 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleId = Utils.getValueFromIdByName(id, "alertRules");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'alertRules'.", id)));
        }
        String actionId = Utils.getValueFromIdByName(id, "actions");
        if (actionId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'actions'.", id)));
        }
        this.deleteWithResponse(resourceGroupName, workspaceName, ruleId, actionId, Context.NONE);
    }

    public Response<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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleId = Utils.getValueFromIdByName(id, "alertRules");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'alertRules'.", id)));
        }
        String actionId = Utils.getValueFromIdByName(id, "actions");
        if (actionId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'actions'.", id)));
        }
        return this.deleteWithResponse(resourceGroupName, workspaceName, ruleId, actionId, context);
    }

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

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

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