// 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.AutomationRulesClient;
import com.azure.resourcemanager.securityinsights.fluent.models.AutomationRuleInner;
import com.azure.resourcemanager.securityinsights.models.AutomationRule;
import com.azure.resourcemanager.securityinsights.models.AutomationRules;

public final class AutomationRulesImpl implements AutomationRules {
    private static final ClientLogger LOGGER = new ClientLogger(AutomationRulesImpl.class);

    private final AutomationRulesClient innerClient;

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

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

    public AutomationRule get(String resourceGroupName, String workspaceName, String automationRuleId) {
        AutomationRuleInner inner = this.serviceClient().get(resourceGroupName, workspaceName, automationRuleId);
        if (inner != null) {
            return new AutomationRuleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public Object delete(String resourceGroupName, String workspaceName, String automationRuleId) {
        return this.serviceClient().delete(resourceGroupName, workspaceName, automationRuleId);
    }

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

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

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

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

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

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

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

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

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

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