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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.stereotype.Component;
import uk.ac.ebi.subs.api.controllers.StatusDescriptionController;
import uk.ac.ebi.subs.api.controllers.TeamController;
import uk.ac.ebi.subs.repository.model.ProcessingStatus;
import uk.ac.ebi.subs.repository.model.Submission;
import uk.ac.ebi.subs.repository.model.SubmissionStatus;

import java.util.Arrays;
import java.util.List;

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

@Component
public class RootEndpointLinkProcessor implements ResourceProcessor<RepositoryLinksResource> {

    private static final Logger logger = LoggerFactory.getLogger(RootEndpointLinkProcessor.class);

    public RootEndpointLinkProcessor(RepositoryEntityLinks repositoryEntityLinks, LinkHelper linkHelper) {
        this.repositoryEntityLinks = repositoryEntityLinks;
        this.linkHelper = linkHelper;
    }

    private RepositoryEntityLinks repositoryEntityLinks;
    private LinkHelper linkHelper;

    private void addLinks(List<Link> links) {
        addStatusDescriptions(links);
        addTeams(links);
    }

    @Override
    public RepositoryLinksResource process(RepositoryLinksResource resource) {
        logger.debug("processing resource: {}",resource.getLinks());
        clearAllLinks(resource);

        addLinks(resource.getLinks());

        return resource;
    }

    private void clearAllLinks(RepositoryLinksResource resource) {
        logger.debug("clearing links: {}",resource.getLinks());
        resource.removeLinks();
    }

      private void addTeams(List<Link> links) {
        links.add(
                linkTo(methodOn(TeamController.class).getTeams(null)
                ).withRel("teams")
        );
    }

    private void addStatusDescriptions(List<Link> links) {
        links.add(
                linkTo(
                        methodOn(StatusDescriptionController.class)
                                .allProcessingStatus(null))
                        .withRel("processingStatusDescriptions")
        );
        links.add(
                linkTo(
                        methodOn(StatusDescriptionController.class)
                                .allReleaseStatus(null))
                        .withRel("releaseStatusDescriptions")
        );
        links.add(
                linkTo(
                        methodOn(StatusDescriptionController.class)
                                .allSubmissionStatus(null))
                        .withRel("submissionStatusDescriptions")
        );
    }
}


@@ -360,341 +360,423 @@
 ork.
-stereotype.Component;%0Aimport uk.ac.ebi.subs.api.controllers.StatusDescriptionController;%0Aimport uk.ac.ebi.subs.api.controllers.TeamController;%0Aimport uk.ac.ebi.subs.repository.model.ProcessingStatus;%0Aimport uk.ac.ebi.subs.repository.model.Submission;%0Aimport uk.ac.ebi.subs.repository.model.SubmissionStatus;%0A%0Aimport java.util.Arrays;
+hateoas.TemplateVariable;%0Aimport org.springframework.hateoas.UriTemplate;%0Aimport org.springframework.hateoas.mvc.ControllerLinkBuilder;%0Aimport org.springframework.stereotype.Component;%0Aimport org.springframework.web.util.UriComponents;%0Aimport org.springframework.web.util.UriComponentsBuilder;%0Aimport uk.ac.ebi.subs.api.controllers.StatusDescriptionController;%0Aimport uk.ac.ebi.subs.api.controllers.TeamController;%0A
 %0Aimp
@@ -1715,24 +1715,25 @@
 source: %7B%7D%22,
+ 
 resource.get
@@ -1957,16 +1957,17 @@
 ks: %7B%7D%22,
+ 
 resource
@@ -2023,18 +2023,16 @@
  %7D%0A%0A    
-  
 private 
@@ -2069,34 +2069,40 @@
 ) %7B%0A        
-links.add(
+Link teamsLink =
 %0A           
@@ -2193,25 +2193,193 @@
 %22teams%22)
-%0A
+;%0A%0A 
        
- 
+links.add(teamsLink);%0A%0A        ControllerLinkBuilder linkBuilder = linkTo(methodOn(TeamController.class).getTeam(null));%0A        links.add(linkBuilder.withRel(%22team%22)
 );%0A    %7D

add templated link for a team to root endpoint