package com.pollistics.controllers;

import com.pollistics.models.Poll;
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.text.MessageFormat;
import java.util.HashMap;

@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, Model model) {
		HashMap<String, Integer> options = new HashMap<>();
		int i = 0;
		while (!request.getParameter("option" + i).trim().isEmpty()) {
			String option = request.getParameter("option" + i);
			options.put(option, 0);
			i++;
		}
		poll.setOptions(options);

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

		if(result.hasErrors()) {
			model.addAttribute("errors", result);
			return "index";
		}
		else {
			String id = pollService.createPoll(poll.getTitle(), options);
			return "redirect:/" + id;
		}
	}

	@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;
		}
	}
}

@@ -65,16 +65,51 @@
 s.Poll;%0A
+import com.pollistics.models.User;%0A
 import c
@@ -262,16 +262,145 @@
 owired;%0A
+import org.springframework.security.core.Authentication;%0Aimport org.springframework.security.core.context.SecurityContextHolder;%0A
 import o
@@ -2406,23 +2406,238 @@
 se %7B%0A%09%09%09
-String 
+Authentication auth = SecurityContextHolder.getContext().getAuthentication();%0A%09%09%09String id;%0A%09%09%09if (auth != null) %7B%0A%09%09%09%09id = pollService.createPoll(poll.getTitle(), options, (User)auth.getPrincipal());%0A%09%09%09%09%0A%09%09%09%7D else %7B%0A%09%09%09%09
 id = pol
@@ -2683,16 +2683,21 @@
 tions);%0A
+%09%09%09%7D%0A
 %09%09%09retur

add user when creating poll