package ca.ulaval.ticketmaster.web;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.junit.runner.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import ca.ulaval.ticketmaster.dao.util.DataManager;
import ca.ulaval.ticketmaster.dao.util.XmlReader;
import ca.ulaval.ticketmaster.model.Ticket;
import ca.ulaval.ticketmaster.model.User;

/**
 * Handles requests for the application home page.
 */
@Controller
//@SessionAttributes
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	private DataManager datamanager;
	
	public HomeController(){
		 datamanager = new DataManager();
	}
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String MainFrame(Locale locale, Model model) {
		model.addAttribute("currentPage", "Home.jsp");
		return "MainFrame";
	}
	
	@RequestMapping(value = "/Home", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		model.addAttribute("currentPage", "Home.jsp");
		return "MainFrame";
	}
	
	@RequestMapping(value = "/CreateUser", method = RequestMethod.GET)
	public String CreateUser(Locale locale, Model model) {
		model.addAttribute("currentPage", "CreateUser.jsp");
		return "MainFrame";
	}
	
	@RequestMapping(value = "/Login", method = RequestMethod.GET)
	public String Login(Locale locale, Model model) {
		model.addAttribute("currentPage", "Login.jsp");
		return "MainFrame";
	}
	@RequestMapping(value = {"/disconnect","/event/disconnect","/event/{id}/disconnect"}, method = RequestMethod.GET)
	public String Disconnect(Locale locale, Model model, HttpSession session) {
		session.setAttribute("sesacceslevel",null);
		session.setAttribute("sesusername", null);
		return "redirect:/";
	
	}
	//Msemble ﾃｧa va ﾃｪtre ﾃ�mettre ailleurs
	@RequestMapping(value = "/AddUser", method = RequestMethod.POST)
	public String AddUser(Locale locale, @RequestParam("username")String username,@RequestParam("password")String password, Model model) {

		System.out.println("HEYHO " + username);
		//@TODO Add l'username
		
		//model.addAttribute("currentPage", "AddUser.jsp");
		model.addAttribute("username", username);
		
		User user = new User(username);
		user.setPassword(password);
		if(datamanager.saveUser(user)){
			//blahblah user create sucessfull
		}
		else{
			
		}
		//model.addAttribute("currentPage", "Home.jsp"); //wtf j'peux pas redirect sur AddUser ? D:
		
		return "MainFrame";
		//return "redirect:/MainFrame";
		//return "forward:/MainFrame";
	}
	
	//Msemble ﾃｧa va ﾃｪtre ﾃ�mettre ailleurs
	//TODO:trouve comment pas hardcoder tous les paths possibles
	@RequestMapping(value = {"/connect","/event/connect","/event/{id}/connect"}, method = RequestMethod.POST)
	public String Login(Locale locale, 
			@RequestParam("username")String username, 
			@RequestParam("password")String password,
			Model model, HttpSession session) {
		
		//check user login petit refactor :D
		//TODO terminer pour mettre sa beau mais le principe est la
		User user = datamanager.getUser(username);
		if(user != null){
			if(user.getPassword().equals(password)){
				session.setAttribute("sesacceslevel",user.getAccessLevel().toString());
				session.setAttribute("sesusername", username);
			}
		}
		else
		{
			model.addAttribute("errorMsg", "La combinaison pseudo/mot de passe est invalide");
		}
		
		return "redirect:/";
	}
	
	public String error(HttpStatus status) {
		if (status == HttpStatus.BAD_REQUEST || status == HttpStatus.NOT_FOUND)
		{
			return "redirect:/";
		}
		return null;
	}
}

@@ -293,53 +293,8 @@
 ry;%0A
-import org.springframework.http.HttpRequest;%0A
 impo
@@ -3708,106 +3708,36 @@
 %09%0A%09%09
-//check user login petit refactor :D%0A%09%09//TODO terminer pour mettre sa beau mais le principe est la
+boolean userIsOk = false;%0A%09%09
 %0A%09%09U
@@ -3842,16 +3842,37 @@
 word))%7B%0A
+%09%09%09%09userIsOk = true;%0A
 %09%09%09%09sess
@@ -3990,16 +3990,20 @@
 rname);%0A
+%09%09%09%0A
 %09%09%09%7D%0A%09%09%7D
@@ -4005,20 +4005,33 @@
 %7D%0A%09%09%7D%0A%09%09
-else
+%0A%09%09if (!userIsOk)
 %0A%09%09%7B%0A%09%09%09
@@ -4121,203 +4121,86 @@
 %09%09%7D%0A
-%09%09
 %0A%09%09
-return %22redirect:/%22;%0A%09%7D%0A%09%0A%09public String error(HttpStatus status) %7B%0A%09%09if (status == HttpStatus.BAD_REQUEST %7C%7C status == HttpStatus.NOT_FOUND)%0A%09%09%7B%0A%09%09%09return %22redirect:/
+model.addAttribute(%22currentPage%22, %22Home.jsp%22);%0A%09%09%0A%09%09return %22MainFrame
 %22;%0A
-%09
 %09%7D%0A%09
-%09return null;%0A%09%7D
 %0A%7D%0A

fix du message d'erreur de login