package dnss.controller;

import dnss.model.Job;
import dnss.model.Jobs;
import dnss.model.SP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import java.util.List;
import java.util.Random;

import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;

@Controller
public class JobController {
    @Autowired
    private WebApplicationContext context;

    @Autowired
    private SP sp;

    @Resource(name="all_jobs_primary")
    private List<Job> primaries; // singleton, do not alter

    @Resource(name="all_jobs_secondary")
    private List<Job> secondaries; // singleton, do not alter

    @Resource(name="all_jobs_tertiary")
    private List<Job> tertiaries; // singleton, do not alter

    @Resource(name="skill_types")
    private List<String> skillTypes; // singleton, do not alter


    @RequestMapping("/job/{identifier:[a-z]+}-{level:[1-9][0-9]*}")
    public String job(HttpServletRequest request, HttpServletResponse response,
                      @PathVariable("identifier") String identifier,
                      @PathVariable("level") int level,
                      ModelMap model) throws Exception {
        String bean = "jobs_"  +identifier;
        if (! context.containsBean(bean)) {
            response.sendError(SC_NOT_FOUND, "No tertiary job '" + identifier + "'");
        }

        Jobs jobs = (Jobs)context.getBean(bean);

        jobs.setLevel(level);
        jobs.setMaxSP(sp.forCap(level));

        // the jobs
        model.addAttribute("jobs", jobs);

        // the list of all the primary/secondary/tertiary jobs
        model.addAttribute("primaries", primaries);
        model.addAttribute("secondaries", secondaries);
        model.addAttribute("tertiaries", tertiaries);

        // the path to this job simulator
        model.addAttribute("path", jobs.getTertiary().getIdentifier() + "-" + level);

        // the skill + weapon types
        model.addAttribute("skill_types", skillTypes);
        model.addAttribute("weapon_types", context.getBean(jobs.getPrimary().getIdentifier() + "_weapons"));

        // setting the cookies
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                Cookie cookie = null;
                if (c.getName().equals("mru_job") && ! identifier.equals(c.getValue())) {
                    cookie = new Cookie("mru_job", identifier);
                } else if (c.getName().equals("mru_level") && ! Integer.toString(level).equals(c.getValue())) {
                    cookie = new Cookie("mru_level", Integer.toString(level));
                }

                if (cookie != null) {
                    cookie.setMaxAge(31556926);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
        }

        return "home";
    }

    @RequestMapping("/job/{identifier:[a-z]+}")
    public String job(HttpServletRequest request, HttpServletResponse response,
                      @PathVariable("identifier") String identifier,
                      ModelMap model) throws Exception {
        int cap = sp.getLatestCap();
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("mru_level")) {
                    try {
                        int c = Integer.parseInt(cookie.getValue());
                        if (c > 0 && c <= cap) {
                            cap = c;
                        }
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        }

        return job(request, response, identifier, cap, model);
    }

    @RequestMapping("/")
    public String job(HttpServletRequest request, HttpServletResponse response,
                      ModelMap model) throws Exception {
        String identifier = null;
        int cap = -1;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("mru_job")) {
                    String bean = "jobs_"  +cookie.getValue();
                    if (context.containsBean(bean)) {
                        identifier = cookie.getValue();
                    }
                } else if (cookie.getName().equals("mru_level")) {
                    try {
                        int c = Integer.parseInt(cookie.getValue());
                        if (c > 0 && c <= sp.getLatestCap()) {
                            cap = c;
                        }
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        }

        if (identifier == null || cap == -1) {
            Random random = new Random();
            identifier = tertiaries.get(random.nextInt(tertiaries.size())).getIdentifier();
            cap = sp.getLatestCap();
        }

        return job(request, response, identifier, cap, model);
    }
}
@@ -2521,116 +2521,38 @@
  set
-ting
+s
  the 
-cookies%0A        Cookie%5B%5D cookies = request.getCookies();%0A        if (cookies != null) %7B%0A   
+most recent job%0A
         
  for
@@ -2551,112 +2551,34 @@
     
- for (
 Cookie
- c :
  cookie
-s) %7B%0A                Cookie cookie = null;%0A                if (c.getName().equals
+ = new Cookie
 (%22mr
@@ -2583,22 +2583,17 @@
 mru_job%22
-) && !
+,
  identif
@@ -2599,357 +2599,180 @@
 fier
-.equals(c.getValue())) %7B%0A                    cookie = new Cookie(%22mru_job%22, identifier);%0A                %7D else if (c.getName().equals(%22mru_level%22) && ! Integer.toString(level).equals(c.getValue())) %7B%0A                    cookie = new Cookie(%22mru_level%22, Integer.toString(level));%0A                %7D%0A%0A                if (cookie != null) %7B%0A            
+);%0A        cookie.setMaxAge(31556926);%0A        cookie.setPath(%22/%22);%0A        response.addCookie(cookie);%0A%0A        cookie = new Cookie(%22mru_level%22, Integer.toString(level));%0A
     
@@ -2803,28 +2803,16 @@
 56926);%0A
-            
         
@@ -2840,28 +2840,16 @@
         
-     
-       
 response
@@ -2871,50 +2871,8 @@
 ie);
-%0A                %7D%0A            %7D%0A        %7D
 %0A%0A  

Revert "Setting cookies only if it has changed."