package com.pollistics.controllers;

import com.pollistics.models.Poll;
import com.pollistics.models.User;
import com.pollistics.models.validators.PollValidator;
import com.pollistics.services.PollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.security.Principal;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.stream.Collectors;

@Controller
public class PollController {

	@Autowired
	private PollService pollService;
	private Cookie cookie = new Cookie("id", "");

	@GetMapping(value = "/polls")
	public String allPolls(Model model) {
		model.addAttribute("polls", pollService.getAllPolls());
		return "polls/overview";
	}

	@GetMapping(value = {"/polls/{pollId}", "/{pollId}"})
	public String pollDetail(@PathVariable String pollId, Model model, HttpServletResponse response) {
		Poll poll = pollService.getPoll(pollId);
		if(poll == null) {
			response.setStatus(404);
			return "error/404";
		}
		model.addAttribute("poll", pollService.getPoll(pollId));
		return "polls/detail";
	}

	@PostMapping(value = "/polls/create")
	public String createPoll(HttpServletRequest request, @Valid @ModelAttribute("poll") Poll poll, BindingResult result, Principal principal, Model model) {
		HashMap<String, Integer> options =
			(HashMap<String, Integer>)request.getParameterMap().entrySet().stream()
				.filter((param) -> param.getKey().startsWith("option") && !param.getValue()[0].trim().isEmpty())
				.map((param) -> param.getValue()[0])
				.collect(Collectors.<String,String,Integer>toMap((option) -> option, (option) -> 0));

		poll.setOptions(options);

		PollValidator pollValidator = new PollValidator();
		pollValidator.validate(poll, result);

		if(result.hasErrors()) {
			model.addAttribute("errors", result);
			return "index";
		}
		else {
			String slug;
			// todo: figure out casing here
			if (request.getParameter("slug") != null) {
				if (principal != null) {
					slug = pollService.createPoll(poll.getTitle(), options, request.getParameter("slug"), (User)principal);
				} else {
					slug = pollService.createPoll(poll.getTitle(), options, request.getParameter("slug"));
				}
			} else {
				if (principal != null) {
					slug = pollService.createPoll(poll.getTitle(), options, (User)principal);
				} else {
					slug = pollService.createPoll(poll.getTitle(), options);
				}
			}
			return "redirect:/" + slug;
		}
	}

	@PostMapping(value = "/polls/delete/{pollId}")
	public String deletePoll(@PathVariable String pollId, HttpServletResponse response, RedirectAttributes redirectAttrs) {
		boolean result = pollService.deletePoll(pollId);
		if (result) {
			redirectAttrs.addFlashAttribute("message", "The poll has deleted successfully!");
			redirectAttrs.addFlashAttribute("message_type", "success");
			return "redirect:/";
		} else {
			response.setStatus(404);
			return "error/404";
		}
	}

	@PostMapping(value = "/polls/vote/{pollId}")
	public String voteOptions(@CookieValue(value = "id", defaultValue = "") String cookieIdValue, @PathVariable String pollId, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (cookie.getValue().contains(pollId)) {
			Poll p = pollService.getPoll(pollId);
			model.addAttribute("msg", MessageFormat.format("You already voted for poll: {0}", p.getTitle()));
			model.addAttribute("previous", MessageFormat.format("/{0}/", pollId));
			response.setStatus(403);
			return "error/403";
		} else {
			String value;
			if (cookie.getValue().equals("")) {
				value = pollId;
			} else {
				value = MessageFormat.format("{0}-{1}", pollId, cookieIdValue);
			}
			final int expiryTimeCookie = 2147483647; // maximum of int
			final String cookiePath = "/";
			cookie.setValue(value);
			cookie.setMaxAge(expiryTimeCookie);
			cookie.setPath(cookiePath);
			response.addCookie(cookie);
			String option = request.getParameter("option");
			Poll p = pollService.getPoll(pollId);
			pollService.voteOption(p, option);
			return "redirect:/" + pollId;
		}
	}
}

@@ -936,16 +936,61 @@
 Valid;%0A%0A
+import java.io.UnsupportedEncodingException;%0A
 import j
@@ -1013,16 +1013,16 @@
 ncipal;%0A
-
 import j
@@ -1106,16 +1106,44 @@
 lectors;
+%0Aimport java.net.URLEncoder;
 %0A%0A@Contr
@@ -1518,32 +1518,42 @@
 il(@PathVariable
+(%22pollId%22)
  String pollId, 
@@ -1990,24 +1990,60 @@
 Model model)
+ throws UnsupportedEncodingException
  %7B%0A%09%09HashMap
@@ -3154,16 +3154,74 @@
 %09%7D%0A%09%09%09%7D%0A
+%09%09%09String encodedSlug = URLEncoder.encode(slug, %22UTF-8%22);%0A
 %09%09%09retur
@@ -3237,17 +3237,24 @@
 ct:/%22 + 
-s
+encodedS
 lug;%0A%09%09%7D

set encoding for redirect