package org.yacos.web.chairman.validation;

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

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.yacos.web.chairman.form.FormConference;

public class ConferenceValidator implements Validator {

	public boolean supports(Class commandClass) {
		return FormConference.class.isAssignableFrom(commandClass);
	}

	public void validate(Object obj, Errors errors) {
		validateFirstPage((FormConference) obj, errors);
		validateSecondPage((FormConference) obj, errors);
		validateThirdPage((FormConference) obj, errors);
	}

	public void validateFirstPage(FormConference conference, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "TITLE_REQUIRED", "A title is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "DESCRIPTION_REQUIRED", "A description is required");
	}

	public void validateSecondPage(FormConference conference, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateStart", "DATE_REQUIRED", "A date is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateEnd", "DATE_REQUIRED", "A date is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateArticle", "DATE_REQUIRED", "A date is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateEvaluation", "DATE_REQUIRED", "A date is required");

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date dateStart = null, dateEnd = null, dateArticle = null, dateEvaluation = null;
		try {
			dateStart = format.parse(conference.getDateStart());
			conference.setDateStartParsed(dateStart);
		} catch (ParseException e) {
			errors.rejectValue("dateStart", "conference.errors.dateStartNonValid");
		}
		try {
			dateEnd = format.parse(conference.getDateEnd());
			conference.setDateEndParsed(dateEnd);			
		} catch (ParseException e) {
			errors.reject("dateEnd", "conference.errors.dateStartNonValid");
		}
		try {
			dateArticle = format.parse(conference.getDateArticle());
			conference.setDateArticleParsed(dateArticle);
		} catch (ParseException e) {
			errors.reject("dateArticle", "conference.errors.dateStartNonValid");
		}
		try {
			dateEvaluation = format.parse(conference.getDateEvaluation());
			conference.setDateEvaluationParsed(dateEvaluation);
		} catch (ParseException e) {
			errors.reject("dateEvaluation", "conference.errors.dateStartNonValid");
		}

		if (dateStart != null && dateEnd != null)
			if (dateStart.after(dateEnd))
				errors.rejectValue("dateStart", "conference.errors.dateStartSuperiorEnd");
		
		if (dateStart != null && dateArticle != null)
			if (!dateStart.after(dateArticle))
				errors.rejectValue("dateArticle", "conference.errors.dateStartSuperiorArticle");
		
		if (dateStart != null && dateEvaluation != null)
			if (!dateStart.after(dateEvaluation))
				errors.rejectValue("dateEvaluation", "conference.errors.dateStartSuperiorEvaluation");
		
		if (dateEvaluation != null && dateArticle != null)
			if (!dateArticle.before(dateEvaluation))
				errors.rejectValue("dateArticle", "conference.errors.dateArticleSuperiorEvaluation");
	}

	public void validateThirdPage(FormConference conference, Errors errors) {

	}

}
