package com.pollistics.controllers;

import com.pollistics.models.User;
import com.pollistics.models.validators.UserValidator;
import com.pollistics.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

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

@Controller
public class AccountController {
	@Autowired
	private UserService userService;

	@GetMapping(value = "/account")
	public String account(Model model) {
		return "account/index";
	}

	@GetMapping(value = "/login")
	public String login(HttpServletRequest request, Model model) {
		model.addAttribute("request", request);
		return "account/login";
	}

	@GetMapping(value = "/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response, Model model) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (auth != null){
			new SecurityContextLogoutHandler().logout(request, response, auth);
		}
		return "redirect:/login?logout";
	}

	@GetMapping(value = "/register")
	public String register(Model model) {
		return "account/register";
	}

	@PostMapping(value = "/register")
	public String postRegister(HttpServletRequest request, @Valid @ModelAttribute("user") User user, BindingResult result, Model model) {
		if(!request.getParameter("password1").equals(request.getParameter("password"))) {
			result.addError(new FieldError("user", "password1", "Passwords don't match"));
		}
		if(userService.userExists(user.getUsername())) {
			result.addError(new FieldError("user", "username", "Username is already taken"));
		}

		UserValidator userValidator = new UserValidator();
		userValidator.validate(user, result);

		if(result.hasErrors()) {
			model.addAttribute("errors", result);
			return "account/register";
		}
		else {
			userService.createUser(user);
			return "redirect:/";
		}
	}
	
	@GetMapping(value = "/account/polls")
	public String polls() {
		return "account/polls";
	}
}

@@ -116,24 +116,68 @@
 rValidator;%0A
+import com.pollistics.services.PollService;%0A
 import com.p
@@ -1080,16 +1080,64 @@
 Service;
+%0A%09%0A%09@Autowired%0A%09private PollService pollService;
 %0A%0A%09@GetM
@@ -2658,19 +2658,160 @@
 g polls(
-) %7B
+Model model) %7B%0A%09%09model.addAttribute(%22polls%22, pollService.getPolls((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()));
 %0A%09%09retur
@@ -2832,10 +2832,14 @@
 ls%22;%0A%09%7D%0A
+%09%0A%09%0A
 %7D%0A

put polls in model