package org.innovateuk.ifs.management.controller;

import org.innovateuk.ifs.management.model.AssessorProfileModelPopulator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import static org.innovateuk.ifs.util.MapFunctions.asMap;

/**
 * This controller will handle all Competition Management requests to view assessor profiles.
 */
@Controller
@RequestMapping("/competition/{competitionId}/assessors")
@PreAuthorize("hasAuthority('comp_admin')")
public class CompetitionManagementAssessorProfileController {

    @Autowired
    private AssessorProfileModelPopulator assessorProfileModelPopulator;

    enum AssessorProfileOrigin {
        APPLICATION_PROGRESS("/assessment/competition/{competitionId}/application/{applicationId}/assessors"),
        ASSESSOR_FIND("/competition/{competitionId}/assessors/find"),
        ASSESSOR_INVITE("/competition/{competitionId}/assessors/invite"),
        ASSESSOR_OVERVIEW("/competition/{competitionId}/assessors/overview"),
        MANAGE_ASSESSORS("/assessment/competition/{competitionId}/assessors");

        private String baseOriginUrl;

        AssessorProfileOrigin(String baseOriginUrl) {
            this.baseOriginUrl = baseOriginUrl;
        }

        public String getBaseOriginUrl() {
            return baseOriginUrl;
        }
    }

    @GetMapping("/profile/{assessorId}")
    public String profile(Model model,
                          @PathVariable("competitionId") long competitionId,
                          @PathVariable("assessorId") long assessorId,
                          @RequestParam(value = "origin", defaultValue = "APPLICATION_PROGRESS") String origin,
                          @RequestParam(value = "applicationId", required = false) Long applicationId,
                          @RequestParam MultiValueMap<String, String> queryParams) {

        model.addAttribute("model", assessorProfileModelPopulator.populateModel(assessorId, competitionId));
        model.addAttribute("backUrl", buildBackUrl(origin, competitionId, applicationId, queryParams));

        return "assessors/profile";
    }

    private String buildBackUrl(String origin, Long competitionId, Long applicationId, MultiValueMap<String, String> queryParams) {
        String baseUrl = AssessorProfileOrigin.valueOf(origin).getBaseOriginUrl();
        queryParams.remove("origin");

        if (queryParams.containsKey("applicationId")) {
            queryParams.remove("applicationId");
        }

        return UriComponentsBuilder.fromPath(baseUrl)
                .queryParams(queryParams)
                .buildAndExpand(asMap("competitionId", competitionId, "applicationId", applicationId ))
                .encode()
                .toUriString();
    }
}

@@ -1359,16 +1359,109 @@
 essors%22)
+,%0A        ASSESSOR_PROGRESS(%22/assessment/competition/%7BcompetitionId%7D/assessors/%7BassessorId%7D%22)
 ;%0A%0A     
@@ -2427,16 +2427,28 @@
 ationId,
+ assessorId,
  queryPa
@@ -2585,16 +2585,33 @@
 ationId,
+ Long assessorId,
  MultiVa
@@ -2878,32 +2878,142 @@
 d%22);%0A        %7D%0A%0A
+        if (queryParams.containsKey(%22assessorId%22)) %7B%0A            queryParams.remove(%22assessorId%22);%0A        %7D%0A%0A
         return U
@@ -3192,17 +3192,42 @@
 cationId
- 
+, %22assessorId%22, assessorId
 ))%0A     

Fix breadcrumb navigation to go to the correct page