package ems.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.validation.Valid;

import ems.model.Todo;
import ems.service.TodoService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
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.SessionAttributes;

@Controller
@SessionAttributes( "name" )
public class TodoController {
    @Autowired
    private TodoService mTodoService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        binder.registerCustomEditor( Date.class, new CustomDateEditor( dateFormat, false ) );
    }

    @RequestMapping( value = "/list-todos", method = RequestMethod.GET )
    public String showTodosList( ModelMap model ) {
        String user = (String) model.get( "name" );
        model.addAttribute( "todos", mTodoService.retrieveTodos( user ) );
        return "list-todos";
    }

    @RequestMapping( value = "/add-todo", method = RequestMethod.GET )
    public String showAddTodoPage( ModelMap model ) {
        model.addAttribute( "todo", new Todo() );
        return "todo";
    }

    @RequestMapping( value = "/add-todo", method = RequestMethod.POST )
    public String addTodo( ModelMap model, @Valid Todo todo, BindingResult result ) {
        if ( result.hasErrors() ) {
            return "todo";
        }
        mTodoService.addTodo( (String) model.get( "name" ), todo.getDesc(), todo.getTargetDate(), false );
        model.clear();
        return "redirect:/list-todos";
    }

    @RequestMapping( value = "/delete-todo", method = RequestMethod.GET )
    public String deleteTodo( @RequestParam int id ) {
        mTodoService.deleteTodo( id );
        return "redirect:/list-todos";
    }

    @RequestMapping( value = "/update-todo", method = RequestMethod.GET )
    public String showUpdateTodoPage( ModelMap model, @RequestParam int id ) {
        model.addAttribute( "todo", mTodoService.retrieveTodo( id ) );
        return "todo";
    }

    @RequestMapping( value = "/update-todo", method = RequestMethod.POST )
    public String updateTodo( ModelMap model, @Valid Todo todo, BindingResult result ) {
        if ( result.hasErrors() ) {
            return "todo";
        }
        todo.setUser( (String) model.get( "name" ) );
        mTodoService.updateTodo( todo );
        model.clear();
        return "redirect:/list-todos";
    }
}

@@ -295,16 +295,154 @@
 Editor;%0A
+import org.springframework.security.core.context.SecurityContextHolder;%0Aimport org.springframework.security.core.userdetails.UserDetails;%0A
 import o
@@ -1337,16 +1337,22 @@
  value =
+ %7B%22/%22,
  %22/list-
@@ -1357,16 +1357,17 @@
 t-todos%22
+%7D
 , method
@@ -1445,60 +1445,8 @@
 ) %7B%0A
-        String user = (String) model.get( %22name%22 );%0A
     
@@ -1506,20 +1506,30 @@
 eTodos( 
-user
+getPrincipal()
  ) );%0A  
@@ -2032,35 +2032,21 @@
 do( 
-(String) model.get( %22name%22 
+getPrincipal(
 ), t
@@ -2891,35 +2891,21 @@
 er( 
-(String) model.get( %22name%22 
+getPrincipal(
 ) );
@@ -3013,11 +3013,391 @@
 %22;%0A    %7D
+%0A%0A    private String getPrincipal() %7B%0A        String userName = null;%0A        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();%0A        if(principal instanceof UserDetails) %7B%0A            userName = ((UserDetails)principal).getUsername();%0A        %7D else %7B%0A            userName = principal.toString();%0A        %7D%0A        return userName;%0A    %7D
 %0A%7D%0A

Add authentication to Todo controller