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

package com.azure.resourcemanager.security.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.security.fluent.GovernanceRulesClient;
import com.azure.resourcemanager.security.fluent.models.GovernanceRuleInner;
import com.azure.resourcemanager.security.fluent.models.OperationResultAutoGeneratedInner;
import com.azure.resourcemanager.security.models.ExecuteGovernanceRuleParams;
import com.azure.resourcemanager.security.models.GovernanceRule;
import com.azure.resourcemanager.security.models.GovernanceRules;
import com.azure.resourcemanager.security.models.GovernanceRulesOperationResultsResponse;
import com.azure.resourcemanager.security.models.OperationResultAutoGenerated;

public final class GovernanceRulesImpl implements GovernanceRules {
    private static final ClientLogger LOGGER = new ClientLogger(GovernanceRulesImpl.class);

    private final GovernanceRulesClient innerClient;

    private final com.azure.resourcemanager.security.SecurityManager serviceManager;

    public GovernanceRulesImpl(
        GovernanceRulesClient innerClient, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<GovernanceRule> list(String scope) {
        PagedIterable<GovernanceRuleInner> inner = this.serviceClient().list(scope);
        return Utils.mapPage(inner, inner1 -> new GovernanceRuleImpl(inner1, this.manager()));
    }

    public PagedIterable<GovernanceRule> list(String scope, Context context) {
        PagedIterable<GovernanceRuleInner> inner = this.serviceClient().list(scope, context);
        return Utils.mapPage(inner, inner1 -> new GovernanceRuleImpl(inner1, this.manager()));
    }

    public Response<GovernanceRule> getWithResponse(String scope, String ruleId, Context context) {
        Response<GovernanceRuleInner> inner = this.serviceClient().getWithResponse(scope, ruleId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new GovernanceRuleImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public GovernanceRule get(String scope, String ruleId) {
        GovernanceRuleInner inner = this.serviceClient().get(scope, ruleId);
        if (inner != null) {
            return new GovernanceRuleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void deleteByResourceGroup(String scope, String ruleId) {
        this.serviceClient().delete(scope, ruleId);
    }

    public void delete(String scope, String ruleId, Context context) {
        this.serviceClient().delete(scope, ruleId, context);
    }

    public void execute(String scope, String ruleId) {
        this.serviceClient().execute(scope, ruleId);
    }

    public void execute(
        String scope, String ruleId, ExecuteGovernanceRuleParams executeGovernanceRuleParams, Context context) {
        this.serviceClient().execute(scope, ruleId, executeGovernanceRuleParams, context);
    }

    public Response<OperationResultAutoGenerated> operationResultsWithResponse(
        String scope, String ruleId, String operationId, Context context) {
        GovernanceRulesOperationResultsResponse inner =
            this.serviceClient().operationResultsWithResponse(scope, ruleId, operationId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new OperationResultAutoGeneratedImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public OperationResultAutoGenerated operationResults(String scope, String ruleId, String operationId) {
        OperationResultAutoGeneratedInner inner = this.serviceClient().operationResults(scope, ruleId, operationId);
        if (inner != null) {
            return new OperationResultAutoGeneratedImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public GovernanceRule getById(String id) {
        String scope =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "scope");
        if (scope == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scope'.", id)));
        }
        String ruleId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "ruleId");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'governanceRules'.", id)));
        }
        return this.getWithResponse(scope, ruleId, Context.NONE).getValue();
    }

    public Response<GovernanceRule> getByIdWithResponse(String id, Context context) {
        String scope =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "scope");
        if (scope == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scope'.", id)));
        }
        String ruleId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "ruleId");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'governanceRules'.", id)));
        }
        return this.getWithResponse(scope, ruleId, context);
    }

    public void deleteById(String id) {
        String scope =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "scope");
        if (scope == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scope'.", id)));
        }
        String ruleId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "ruleId");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'governanceRules'.", id)));
        }
        this.delete(scope, ruleId, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Context context) {
        String scope =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "scope");
        if (scope == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'scope'.", id)));
        }
        String ruleId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{scope}/providers/Microsoft.Security/governanceRules/{ruleId}", "ruleId");
        if (ruleId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'governanceRules'.", id)));
        }
        this.delete(scope, ruleId, context);
    }

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

    private com.azure.resourcemanager.security.SecurityManager manager() {
        return this.serviceManager;
    }

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