// 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.AssessmentsClient;
import com.azure.resourcemanager.security.fluent.models.SecurityAssessmentInner;
import com.azure.resourcemanager.security.models.Assessments;
import com.azure.resourcemanager.security.models.ExpandEnum;
import com.azure.resourcemanager.security.models.SecurityAssessment;
import com.fasterxml.jackson.annotation.JsonIgnore;

public final class AssessmentsImpl implements Assessments {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(AssessmentsImpl.class);

    private final AssessmentsClient innerClient;

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

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

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

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

    public SecurityAssessment get(String resourceId, String assessmentName) {
        SecurityAssessmentInner inner = this.serviceClient().get(resourceId, assessmentName);
        if (inner != null) {
            return new SecurityAssessmentImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<SecurityAssessment> getWithResponse(
        String resourceId, String assessmentName, ExpandEnum expand, Context context) {
        Response<SecurityAssessmentInner> inner =
            this.serviceClient().getWithResponse(resourceId, assessmentName, expand, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SecurityAssessmentImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void deleteByResourceGroup(String resourceId, String assessmentName) {
        this.serviceClient().delete(resourceId, assessmentName);
    }

    public Response<Void> deleteWithResponse(String resourceId, String assessmentName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceId, assessmentName, context);
    }

    public SecurityAssessment getById(String id) {
        String resourceId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "resourceId");
        if (resourceId == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceId'.", id)));
        }
        String assessmentName =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "assessmentName");
        if (assessmentName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'assessments'.", id)));
        }
        ExpandEnum localExpand = null;
        return this.getWithResponse(resourceId, assessmentName, localExpand, Context.NONE).getValue();
    }

    public Response<SecurityAssessment> getByIdWithResponse(String id, ExpandEnum expand, Context context) {
        String resourceId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "resourceId");
        if (resourceId == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceId'.", id)));
        }
        String assessmentName =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "assessmentName");
        if (assessmentName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'assessments'.", id)));
        }
        return this.getWithResponse(resourceId, assessmentName, expand, context);
    }

    public void deleteById(String id) {
        String resourceId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "resourceId");
        if (resourceId == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceId'.", id)));
        }
        String assessmentName =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "assessmentName");
        if (assessmentName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'assessments'.", id)));
        }
        this.deleteWithResponse(resourceId, assessmentName, Context.NONE).getValue();
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String resourceId =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "resourceId");
        if (resourceId == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'resourceId'.", id)));
        }
        String assessmentName =
            Utils
                .getValueFromIdByParameterName(
                    id, "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}", "assessmentName");
        if (assessmentName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'assessments'.", id)));
        }
        return this.deleteWithResponse(resourceId, assessmentName, context);
    }

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

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

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