package uk.ac.ebi.subs.api.processors;

import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import uk.ac.ebi.subs.api.controllers.StatusDescriptionController;
import uk.ac.ebi.subs.api.controllers.SubmissionStatusController;
import uk.ac.ebi.subs.api.services.ValidationResultService;
import uk.ac.ebi.subs.data.status.SubmissionStatusEnum;
import uk.ac.ebi.subs.repository.model.SubmissionStatus;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

@Component
public class SubmissionStatusResourceProcessor implements ResourceProcessor<Resource<SubmissionStatus>> {

    private BasePathAwareLinks basePathAwareLinks;
    private ValidationResultService validationResultService;
    private RepositoryEntityLinks repositoryEntityLinks;

    public SubmissionStatusResourceProcessor(BasePathAwareLinks basePathAwareLinks, ValidationResultService validationResultService, RepositoryEntityLinks repositoryEntityLinks) {
        this.basePathAwareLinks = basePathAwareLinks;
        this.validationResultService = validationResultService;
        this.repositoryEntityLinks = repositoryEntityLinks;
    }

    @Override
    public Resource<SubmissionStatus> process(Resource<SubmissionStatus> resource) {

        addStatusDescriptionRel(resource);

        addStatusUpdateRel(resource);

        addAvailableStatuses(resource);

        return resource;
    }

    private void addStatusDescriptionRel(Resource<SubmissionStatus> resource) {
        resource.add(
                basePathAwareLinks.underBasePath(
                        linkTo(
                                methodOn(StatusDescriptionController.class)
                                        .submissionStatus(resource.getContent().getStatus()))
                ).withRel("statusDescription")
        );
    }

    private void addStatusUpdateRel(Resource<SubmissionStatus> submissionStatusResource) {
        SubmissionStatus submissionStatus = submissionStatusResource.getContent();

        if (submissionStatus.getStatus().equals(SubmissionStatusEnum.Draft.name()) && validationResultService.isValidationFinishedAndPassed(submissionStatus.getId())) {
            Link submissionStatusResourceLink = repositoryEntityLinks.linkToSingleResource(submissionStatus).expand();

            Assert.notNull(submissionStatusResourceLink);

            Link updateLink = submissionStatusResourceLink.withRel( "self" + LinkHelper.UPDATE_REL_SUFFIX );
            submissionStatusResource.add(updateLink);
        }
    }

    private void addAvailableStatuses(Resource<SubmissionStatus> submissionStatusResource) {
        SubmissionStatus submissionStatus = submissionStatusResource.getContent();

        if (validationResultService.isValidationFinishedAndPassed(submissionStatus.getId())) {
            submissionStatusResource.add(
                    basePathAwareLinks.underBasePath(
                            linkTo(
                                    methodOn(SubmissionStatusController.class)
                                            .availableSubmissionStatuses(submissionStatus.getId()))
                    ).withRel("availableStatuses")
            );
        }
    }
}

@@ -635,22 +635,134 @@
 bmission
-Status
+;%0Aimport uk.ac.ebi.subs.repository.model.SubmissionStatus;%0Aimport uk.ac.ebi.subs.repository.repos.SubmissionRepository
 ;%0A%0Aimpor
@@ -1197,16 +1197,71 @@
 tyLinks;
+%0A    private SubmissionRepository submissionRepository;
 %0A%0A    pu
@@ -1386,16 +1386,61 @@
 Service,
+%0A                                            
  Reposit
@@ -1475,16 +1475,59 @@
 ityLinks
+, SubmissionRepository submissionRepository
 ) %7B%0A    
@@ -1696,24 +1696,82 @@
 ntityLinks;%0A
+        this.submissionRepository = submissionRepository;%0A
     %7D%0A%0A    @
@@ -3310,32 +3310,137 @@
 ce.getContent();
+%0A        Submission submission = submissionRepository.findBySubmissionStatusId(submissionStatus.getId());
 %0A%0A        if (va
@@ -3806,38 +3806,32 @@
 tuses(submission
-Status
 .getId()))%0A     

Fix availableSubmissionStatuses URL using submission ID instead of submissionStatus ID