package com.modesteam.urutau.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.modesteam.urutau.UserSession;
import com.modesteam.urutau.model.Artifact;
import com.modesteam.urutau.model.Project;
import com.modesteam.urutau.model.User;
import com.modesteam.urutau.service.ProjectService;
import com.modesteam.urutau.service.RequirementService;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.validator.Validator;

/**
 * This class is responsible to manager simple operations of projects!
 * The systems operations are received by the path /requirement followed
 * by the operation defined path.
 */

@Controller
public class ProjectController {
	
	private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);
	
	private final Result result;
	
	private final UserSession userSession;
	
	private final ProjectService projectService;
	
	private final Validator validator;
	
	
	
	@Inject
	public ProjectController(Result result, UserSession userSession, 
			ProjectService projectService, Validator validator) {
		this.result = result;
		this.userSession = userSession;
		this.projectService = projectService;
		this.validator = validator; 
	}
	
	@Post
	public void createProject(Project project){
		logger.info("Project will be persisted: " + project.getTitle());
		
		Date currentDate = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(currentDate);
		
		project.setDateOfCreation(calendar);
		
		User logged = userSession.getUserLogged();
		project.setAuthor(logged);
		
		logger.info("Requesting for project service");
		projectService.save(project);
		
		//result.redirectTo(this).showCreationResult(project.getId());
		
	}
	
	@Post
	public void deleteProject(){
		
	}
	
	/**
	 * Show the projects that has a certain id and title 
	 * 
	 * @param id Unique attribute
	 * @param title various projects can have same title
	 * 
	 * @return {@link Project} from database
	 * 
	 * @throws UnsupportedEncodingException invalid characters or decodes fails
	 */
	@Get
	@Path("/{id}/{title}")
	public Project show(int id, String title) throws UnsupportedEncodingException{
		title = URLDecoder.decode(title, "utf-8");
		
		logger.info("Show project " + title);
		
		Project project = projectService.detail(id);
		
		return project;
	}
	
	@Post
	public void detailProject(){
		
	}

}

@@ -284,24 +284,69 @@
 serSession;%0A
+import com.modesteam.urutau.annotation.View;%0A
 import com.m
@@ -751,16 +751,70 @@
 Result;%0A
+import br.com.caelum.vraptor.validator.SimpleMessage;%0A
 import b
@@ -1176,24 +1176,104 @@
 r.class);%0A%09%0A
+%09private static final String NULL_INFORMATION_ERROR = %22nullInformationError%22;%0A%09%0A
 %09private fin
@@ -1687,24 +1687,140 @@
 ator; %0A%09%7D%0A%09%0A
+%09/**%0A%09 *  Method for create one project with what gonna have%0A%09 *  requirements inside%0A%09 *  %0A%09 * @param project%0A%09 */%0A
 %09@Post%0A%09publ
@@ -1859,16 +1859,19 @@
 oject)%7B%0A
+%09%09%0A
 %09%09logger
@@ -1932,16 +1932,232 @@
 ());%0A%09%09%0A
+%09%09if(project.getTitle() == null)%7B%0A%09%09%09%0A%09%09%09validator.add(new SimpleMessage(NULL_INFORMATION_ERROR,%22The title cant be empty!%22));%0A%09%09%09validator.onErrorForwardTo(ProjectController.class).createProject();%0A%09%09%0A%09%09%7D else %7B%0A%09%09%0A%09
 %09%09Date c
@@ -2183,16 +2183,17 @@
 te();%0A%09%09
+%09
 Calendar
@@ -2230,16 +2230,17 @@
 ce();%0A%09%09
+%09
 calendar
@@ -2264,17 +2264,19 @@
 ate);%0A%09%09
-%0A
+%09%0A%09
 %09%09projec
@@ -2308,17 +2308,19 @@
 dar);%0A%09%09
-%0A
+%09%0A%09
 %09%09User l
@@ -2356,16 +2356,17 @@
 gged();%0A
+%09
 %09%09projec
@@ -2384,25 +2384,27 @@
 (logged);%0A%09%09
-%0A
+%09%0A%09
 %09%09logger.inf
@@ -2442,16 +2442,17 @@
 ce%22);%0A%09%09
+%09
 projectS
@@ -2473,16 +2473,20 @@
 oject);%0A
+%09%09%7D%0A
 %09%09%0A%09%09//r
@@ -3198,12 +3198,60 @@
 )%7B%0A%09%09%0A%09%7D
+%0A%09%0A%09@View%0A%09public void createProject()%7B%0A%09%09%0A%09%09%0A%09%7D
 %0A%0A%7D%0A

Create validation of Project Title