/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */

package com.icesoft.sample.accessible.selection;

import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.GregorianCalendar;

import javax.faces.component.UIComponent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
/**
 * <p>The SelectInputDateBean Class is used to store the selected dates from the
 * selectinputdate components.</p>
 *
 * @since 0.3
 */
@ManagedBean(name="dateSelect")
@ApplicationScoped
public class DateSelectBean {
    /**
     * Variables to store the selected dates.
     */
    private Date date1=new Date();
    private Date date2=new Date();
    private Date date3=new Date();
    private Date date4=new Date();    
    private Date date5=new Date();    
    private String pattern ="date";
    private List patterns = new ArrayList();

    public DateSelectBean() {
        date2 = new GregorianCalendar().getTime();
        patterns.add(new SelectItem("date", "MM/dd/yyyy"));
        patterns.add(new SelectItem("dateTime", "MMM/dd/yyyy HH:mm"));
        patterns.add(new SelectItem("dateTimeSs", "MMM/dd/yyyy HH:mm:ss"));
        patterns.add(new SelectItem("dateTimeAmPm", "MMM/dd/yyyy hh:mm a"));
    }

	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		Date val = (Date)value;
		Calendar cal = Calendar.getInstance();
		
		cal.add(Calendar.DATE, -2);
		Date yes = cal.getTime();
		cal.add(Calendar.DATE, 3);
		Date tom = cal.getTime();
		
		if (val.before(yes) || val.after(tom)) {
			FacesMessage message = new FacesMessage();
	    	message.setDetail("Cannot set to date earlier than yesterday or later than tomorrow.");
	    	message.setSummary("Cannot set to date earlier than yesterday or later than tomorrow.");
	    	message.setSeverity(FacesMessage.SEVERITY_ERROR);
	    	throw new ValidatorException(message);		
		}
	}
    /**
     * Gets the first selected date.
     *
     * @return the first selected date
     */
    public Date getDate1() {
        return date1;
    }

    /**
     * Sets the first selected date.
     *
     * @param date the first selected date
     */
    public void setDate1(Date date) {
        date1 = date;
    }

    /**
     * Gets the 2nd selected date.
     *
     * @return the 2nd selected date
     */
    public Date getDate2() {
        return date2;
    }

    /**
     * Sets the 2nd selected date.
     *
     * @param date the 2nd selected date
     */
    public void setDate2(Date date) {
        date2 = date;
    }

    /**
     * Gets the default timezone of the host server.  The timezone is needed
     * by the convertDateTime for formatting the time dat values.
     *
     * @return timezone for the current JVM
     */
    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public List getPatterns() {
        return patterns;
    }

    public void setPatterns(List patterns) {
        this.patterns = patterns;
    }

    public Date getDate3() {
        return date3;
    }

    public void setDate3(Date date3) {
        this.date3 = date3;
    }

    public Date getDate4() {
        return date4;
    }

    public void setDate4(Date date4) {
        this.date4 = date4;
    }

    public Date getDate5() {
        return date5;
    }

    public void setDate5(Date date5) {
        this.date5 = date5;
    }

    
}