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

package com.azure.resourcemanager.policyinsights.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.policyinsights.fluent.RemediationsClient;
import com.azure.resourcemanager.policyinsights.fluent.models.RemediationDeploymentInner;
import com.azure.resourcemanager.policyinsights.fluent.models.RemediationInner;
import com.azure.resourcemanager.policyinsights.models.Remediation;
import com.azure.resourcemanager.policyinsights.models.RemediationDeployment;
import com.azure.resourcemanager.policyinsights.models.Remediations;

public final class RemediationsImpl implements Remediations {
    private static final ClientLogger LOGGER = new ClientLogger(RemediationsImpl.class);

    private final RemediationsClient innerClient;

    private final com.azure.resourcemanager.policyinsights.PolicyInsightsManager serviceManager;

    public RemediationsImpl(
        RemediationsClient innerClient, com.azure.resourcemanager.policyinsights.PolicyInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtManagementGroup(
        String managementGroupId, String remediationName) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtManagementGroup(managementGroupId, remediationName);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtManagementGroup(
        String managementGroupId, String remediationName, Integer top, Context context) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtManagementGroup(managementGroupId, remediationName, top, context);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public Response<Remediation> cancelAtManagementGroupWithResponse(
        String managementGroupId, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().cancelAtManagementGroupWithResponse(managementGroupId, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation cancelAtManagementGroup(String managementGroupId, String remediationName) {
        RemediationInner inner = this.serviceClient().cancelAtManagementGroup(managementGroupId, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<Remediation> listForManagementGroup(String managementGroupId) {
        PagedIterable<RemediationInner> inner = this.serviceClient().listForManagementGroup(managementGroupId);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public PagedIterable<Remediation> listForManagementGroup(
        String managementGroupId, Integer top, String filter, Context context) {
        PagedIterable<RemediationInner> inner =
            this.serviceClient().listForManagementGroup(managementGroupId, top, filter, context);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public Response<Remediation> createOrUpdateAtManagementGroupWithResponse(
        String managementGroupId, String remediationName, RemediationInner parameters, Context context) {
        Response<RemediationInner> inner =
            this
                .serviceClient()
                .createOrUpdateAtManagementGroupWithResponse(managementGroupId, remediationName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation createOrUpdateAtManagementGroup(
        String managementGroupId, String remediationName, RemediationInner parameters) {
        RemediationInner inner =
            this.serviceClient().createOrUpdateAtManagementGroup(managementGroupId, remediationName, parameters);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> getAtManagementGroupWithResponse(
        String managementGroupId, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().getAtManagementGroupWithResponse(managementGroupId, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation getAtManagementGroup(String managementGroupId, String remediationName) {
        RemediationInner inner = this.serviceClient().getAtManagementGroup(managementGroupId, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> deleteAtManagementGroupWithResponse(
        String managementGroupId, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().deleteAtManagementGroupWithResponse(managementGroupId, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation deleteAtManagementGroup(String managementGroupId, String remediationName) {
        RemediationInner inner = this.serviceClient().deleteAtManagementGroup(managementGroupId, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtSubscription(String remediationName) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtSubscription(remediationName);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtSubscription(
        String remediationName, Integer top, Context context) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtSubscription(remediationName, top, context);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public Response<Remediation> cancelAtSubscriptionWithResponse(String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().cancelAtSubscriptionWithResponse(remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation cancelAtSubscription(String remediationName) {
        RemediationInner inner = this.serviceClient().cancelAtSubscription(remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<Remediation> list() {
        PagedIterable<RemediationInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public PagedIterable<Remediation> list(Integer top, String filter, Context context) {
        PagedIterable<RemediationInner> inner = this.serviceClient().list(top, filter, context);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public Response<Remediation> createOrUpdateAtSubscriptionWithResponse(
        String remediationName, RemediationInner parameters, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().createOrUpdateAtSubscriptionWithResponse(remediationName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation createOrUpdateAtSubscription(String remediationName, RemediationInner parameters) {
        RemediationInner inner = this.serviceClient().createOrUpdateAtSubscription(remediationName, parameters);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> getAtSubscriptionWithResponse(String remediationName, Context context) {
        Response<RemediationInner> inner = this.serviceClient().getAtSubscriptionWithResponse(remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation getAtSubscription(String remediationName) {
        RemediationInner inner = this.serviceClient().getAtSubscription(remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> deleteAtSubscriptionWithResponse(String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().deleteAtSubscriptionWithResponse(remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation deleteAtSubscription(String remediationName) {
        RemediationInner inner = this.serviceClient().deleteAtSubscription(remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtResourceGroup(
        String resourceGroupName, String remediationName) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtResourceGroup(resourceGroupName, remediationName);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtResourceGroup(
        String resourceGroupName, String remediationName, Integer top, Context context) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtResourceGroup(resourceGroupName, remediationName, top, context);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public Response<Remediation> cancelAtResourceGroupWithResponse(
        String resourceGroupName, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().cancelAtResourceGroupWithResponse(resourceGroupName, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation cancelAtResourceGroup(String resourceGroupName, String remediationName) {
        RemediationInner inner = this.serviceClient().cancelAtResourceGroup(resourceGroupName, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<Remediation> listByResourceGroup(String resourceGroupName) {
        PagedIterable<RemediationInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public PagedIterable<Remediation> listByResourceGroup(
        String resourceGroupName, Integer top, String filter, Context context) {
        PagedIterable<RemediationInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, top, filter, context);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public Response<Remediation> getByResourceGroupWithResponse(
        String resourceGroupName, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation getByResourceGroup(String resourceGroupName, String remediationName) {
        RemediationInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> deleteByResourceGroupWithResponse(
        String resourceGroupName, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().deleteWithResponse(resourceGroupName, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation deleteByResourceGroup(String resourceGroupName, String remediationName) {
        RemediationInner inner = this.serviceClient().delete(resourceGroupName, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtResource(String resourceId, String remediationName) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtResource(resourceId, remediationName);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public PagedIterable<RemediationDeployment> listDeploymentsAtResource(
        String resourceId, String remediationName, Integer top, Context context) {
        PagedIterable<RemediationDeploymentInner> inner =
            this.serviceClient().listDeploymentsAtResource(resourceId, remediationName, top, context);
        return Utils.mapPage(inner, inner1 -> new RemediationDeploymentImpl(inner1, this.manager()));
    }

    public Response<Remediation> cancelAtResourceWithResponse(
        String resourceId, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().cancelAtResourceWithResponse(resourceId, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation cancelAtResource(String resourceId, String remediationName) {
        RemediationInner inner = this.serviceClient().cancelAtResource(resourceId, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<Remediation> listForResource(String resourceId) {
        PagedIterable<RemediationInner> inner = this.serviceClient().listForResource(resourceId);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public PagedIterable<Remediation> listForResource(String resourceId, Integer top, String filter, Context context) {
        PagedIterable<RemediationInner> inner = this.serviceClient().listForResource(resourceId, top, filter, context);
        return Utils.mapPage(inner, inner1 -> new RemediationImpl(inner1, this.manager()));
    }

    public Response<Remediation> createOrUpdateAtResourceWithResponse(
        String resourceId, String remediationName, RemediationInner parameters, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().createOrUpdateAtResourceWithResponse(resourceId, remediationName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation createOrUpdateAtResource(
        String resourceId, String remediationName, RemediationInner parameters) {
        RemediationInner inner = this.serviceClient().createOrUpdateAtResource(resourceId, remediationName, parameters);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> getAtResourceWithResponse(String resourceId, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().getAtResourceWithResponse(resourceId, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation getAtResource(String resourceId, String remediationName) {
        RemediationInner inner = this.serviceClient().getAtResource(resourceId, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Remediation> deleteAtResourceWithResponse(
        String resourceId, String remediationName, Context context) {
        Response<RemediationInner> inner =
            this.serviceClient().deleteAtResourceWithResponse(resourceId, remediationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RemediationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Remediation deleteAtResource(String resourceId, String remediationName) {
        RemediationInner inner = this.serviceClient().deleteAtResource(resourceId, remediationName);
        if (inner != null) {
            return new RemediationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.policyinsights.PolicyInsightsManager manager() {
        return this.serviceManager;
    }

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