package kr.ac.kaist.se.tardis.web.validator;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import kr.ac.kaist.se.tardis.task.api.Task;
import kr.ac.kaist.se.tardis.task.api.TaskService;
import kr.ac.kaist.se.tardis.users.api.UserService;
import kr.ac.kaist.se.tardis.web.form.CreateTaskForm;
import kr.ac.kaist.se.tardis.web.form.SetTaskForm;

@Component
public class TaskFormValidator implements Validator {

	@Autowired
	UserService userService;
	@Autowired
	TaskService taskService;
	
	@Override
	public boolean supports(Class<?> clazz) {
		// TODO Auto-generated method stub
		return CreateTaskForm.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		// TODO Auto-generated method stub
		CreateTaskForm taskForm = (CreateTaskForm) target;
		Set<Task> tasks =taskService.findTaskByName(taskForm.getTaskName());
		
		for (Task t : tasks) {
			if(t.getProjectId().getId().equals(taskForm.getProjectId())){
				errors.rejectValue("taskName", "error.taskName.dup", CreateTaskForm.DUP_TASK_NAME_ERROR);
				break;
			}
		}
		if (userService.findUserByName(taskForm.getOwner()) == null) {
			errors.rejectValue("owner", "error.owner.empty", CreateTaskForm.OWNER_NOT_EXIST_ERROR);
		}
		
		if(taskForm.getDueDate().length()!=0){
			if(Character.isLetter(taskForm.getDueDate().charAt(0))){
				errors.rejectValue("dueDate", "error.dueDate.WrongType", SetTaskForm.WRONG_DUE_DATE_ERROR);
			}else{
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				format.parse(taskForm.getDueDate());
			} catch (ParseException e) {
				errors.rejectValue("dueDate", "error.dueDate.WrongType", CreateTaskForm.WRONG_DUE_DATE_ERROR);
			}
			}
		}
	}

}

@@ -110,30 +110,8 @@
 mat;
-%0Aimport java.util.Set;
 %0A%0Aim
@@ -320,103 +320,8 @@
 r;%0A%0A
-import kr.ac.kaist.se.tardis.task.api.Task;%0Aimport kr.ac.kaist.se.tardis.task.api.TaskService;%0A
 impo
@@ -425,59 +425,8 @@
 orm;
-%0Aimport kr.ac.kaist.se.tardis.web.form.SetTaskForm;
 %0A%0A@C
@@ -502,16 +502,24 @@
 owired%0A%09
+private 
 UserServ
@@ -539,47 +539,8 @@
 ce;%0A
-%09@Autowired%0A%09TaskService taskService;%0A%09
 %0A%09@O
@@ -845,286 +845,8 @@
 et;%0A
-%09%09Set%3CTask%3E tasks =taskService.findTaskByName(taskForm.getTaskName());%0A%09%09%0A%09%09for (Task t : tasks) %7B%0A%09%09%09if(t.getProjectId().getId().equals(taskForm.getProjectId()))%7B%0A%09%09%09%09errors.rejectValue(%22taskName%22, %22error.taskName.dup%22, CreateTaskForm.DUP_TASK_NAME_ERROR);%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09if
@@ -1005,16 +1005,16 @@
 %09%09%7D%0A
-%09%09
 %0A%09%09if
-(
+ (!
 task
@@ -1035,186 +1035,26 @@
 e().
-length()!=0)%7B%0A%09%09%09if(Character.isLetter(taskForm.getDueDate().charAt(0)))%7B%0A%09%09%09%09errors.rejectValue(%22dueDate%22, %22error.dueDate.WrongType%22, SetTaskForm.WRONG_DUE_DATE_ERROR);%0A%09%09%09%7Delse
+trim().isEmpty()) 
 %7B%0A%09%09
@@ -1302,21 +1302,16 @@
 );%0A%09%09%09%7D%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

optimized the TaskFormValidator