// 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.util.Context;
import com.azure.resourcemanager.security.fluent.models.SecurityAssessmentMetadataProperties;
import com.azure.resourcemanager.security.fluent.models.SecurityAssessmentResponseInner;
import com.azure.resourcemanager.security.models.AssessmentLinks;
import com.azure.resourcemanager.security.models.AssessmentStatus;
import com.azure.resourcemanager.security.models.AssessmentStatusResponse;
import com.azure.resourcemanager.security.models.ExpandEnum;
import com.azure.resourcemanager.security.models.ResourceDetails;
import com.azure.resourcemanager.security.models.SecurityAssessment;
import com.azure.resourcemanager.security.models.SecurityAssessmentPartnerData;
import com.azure.resourcemanager.security.models.SecurityAssessmentResponse;
import java.util.Collections;
import java.util.Map;

public final class SecurityAssessmentResponseImpl
    implements SecurityAssessmentResponse, SecurityAssessmentResponse.Definition, SecurityAssessmentResponse.Update {
    private SecurityAssessmentResponseInner innerObject;

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

    public String id() {
        return this.innerModel().id();
    }

    public String name() {
        return this.innerModel().name();
    }

    public String type() {
        return this.innerModel().type();
    }

    public AssessmentStatusResponse status() {
        return this.innerModel().status();
    }

    public ResourceDetails resourceDetails() {
        return this.innerModel().resourceDetails();
    }

    public String displayName() {
        return this.innerModel().displayName();
    }

    public Map<String, String> additionalData() {
        Map<String, String> inner = this.innerModel().additionalData();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public AssessmentLinks links() {
        return this.innerModel().links();
    }

    public SecurityAssessmentMetadataProperties metadata() {
        return this.innerModel().metadata();
    }

    public SecurityAssessmentPartnerData partnersData() {
        return this.innerModel().partnersData();
    }

    public SecurityAssessmentResponseInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceId;

    private String assessmentName;

    private SecurityAssessment createAssessment;

    private SecurityAssessment updateAssessment;

    public SecurityAssessmentResponseImpl withExistingResourceId(String resourceId) {
        this.resourceId = resourceId;
        return this;
    }

    public SecurityAssessmentResponse create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAssessments()
                .createOrUpdateWithResponse(resourceId, assessmentName, createAssessment, Context.NONE)
                .getValue();
        return this;
    }

    public SecurityAssessmentResponse create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAssessments()
                .createOrUpdateWithResponse(resourceId, assessmentName, createAssessment, context)
                .getValue();
        return this;
    }

    SecurityAssessmentResponseImpl(String name, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = new SecurityAssessmentResponseInner();
        this.serviceManager = serviceManager;
        this.assessmentName = name;
        this.createAssessment = new SecurityAssessment();
    }

    public SecurityAssessmentResponseImpl update() {
        this.updateAssessment = new SecurityAssessment();
        return this;
    }

    public SecurityAssessmentResponse apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAssessments()
                .createOrUpdateWithResponse(resourceId, assessmentName, updateAssessment, Context.NONE)
                .getValue();
        return this;
    }

    public SecurityAssessmentResponse apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAssessments()
                .createOrUpdateWithResponse(resourceId, assessmentName, updateAssessment, context)
                .getValue();
        return this;
    }

    SecurityAssessmentResponseImpl(
        SecurityAssessmentResponseInner innerObject,
        com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceId =
            Utils
                .getValueFromIdByParameterName(
                    innerObject.id(),
                    "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}",
                    "resourceId");
        this.assessmentName =
            Utils
                .getValueFromIdByParameterName(
                    innerObject.id(),
                    "/{resourceId}/providers/Microsoft.Security/assessments/{assessmentName}",
                    "assessmentName");
    }

    public SecurityAssessmentResponse refresh() {
        ExpandEnum localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAssessments()
                .getWithResponse(resourceId, assessmentName, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public SecurityAssessmentResponse refresh(Context context) {
        ExpandEnum localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAssessments()
                .getWithResponse(resourceId, assessmentName, localExpand, context)
                .getValue();
        return this;
    }

    public SecurityAssessmentResponseImpl withStatus(AssessmentStatus status) {
        if (isInCreateMode()) {
            this.createAssessment.withStatus(status);
            return this;
        } else {
            this.updateAssessment.withStatus(status);
            return this;
        }
    }

    public SecurityAssessmentResponseImpl withResourceDetails(ResourceDetails resourceDetails) {
        if (isInCreateMode()) {
            this.createAssessment.withResourceDetails(resourceDetails);
            return this;
        } else {
            this.updateAssessment.withResourceDetails(resourceDetails);
            return this;
        }
    }

    public SecurityAssessmentResponseImpl withAdditionalData(Map<String, String> additionalData) {
        if (isInCreateMode()) {
            this.createAssessment.withAdditionalData(additionalData);
            return this;
        } else {
            this.updateAssessment.withAdditionalData(additionalData);
            return this;
        }
    }

    public SecurityAssessmentResponseImpl withMetadata(SecurityAssessmentMetadataProperties metadata) {
        if (isInCreateMode()) {
            this.createAssessment.withMetadata(metadata);
            return this;
        } else {
            this.updateAssessment.withMetadata(metadata);
            return this;
        }
    }

    public SecurityAssessmentResponseImpl withPartnersData(SecurityAssessmentPartnerData partnersData) {
        if (isInCreateMode()) {
            this.createAssessment.withPartnersData(partnersData);
            return this;
        } else {
            this.updateAssessment.withPartnersData(partnersData);
            return this;
        }
    }

    private boolean isInCreateMode() {
        return this.innerModel().id() == null;
    }
}
