package com.lntusl.core.web.validator.annotation;

import java.util.Calendar;
import java.util.Date;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author konka
 *
 */
public class FutureValidator implements ConstraintValidator<Future, Date> {

	private java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(
			"(\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*)",
			java.util.regex.Pattern.CASE_INSENSITIVE);

	Future annotation;

	public void initialize(Future annotation) {
		this.annotation = annotation;
	}

	public boolean isValid(Date date, ConstraintValidatorContext context) {
		if (date == null) {
			return true;
		}

		Calendar cd = Calendar.getInstance();
		cd.set(Calendar.HOUR,
				cd.get(Calendar.HOUR) + this.annotation.afterhours());
		cd.set(Calendar.MINUTE, cd.get(Calendar.MINUTE) - 2);

		if (date.before(cd.getTime())) {
			return false;
		}

		return true;
	}
}
