/**
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

package org.ajax4jsf.bean;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import javax.faces.convert.DateTimeConverter;

public class CalendarTestBean {

    public static final String DATE_PATTERN = "MM/dd/yyyy HH:mm";

    public static final Locale LOCALE = new Locale("US");

    public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("GMT+2");

    public static Date DEFAULT_DATE;

    private static DateTimeConverter DEFAULT_CONVERTER;

    public static DateFormat DATE_FORMAT;

    // constructor for static fields 
    static {
	setupDefaultConverter();
	setupDefaultDate();
    }
    
    private String datePattern ;
    
    private Locale locale;
    
    private TimeZone timeZone;
    
    private Date selectedDate;
    
    private String selectedDateString;
    
    public CalendarTestBean() {
	selectedDate = DEFAULT_DATE;
	resetSelectedDateString();
	
	datePattern = DATE_PATTERN;
	locale = LOCALE;
	timeZone = TIME_ZONE;
    }

    public Date getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
        resetSelectedDateString();
    }
    
    public void resetSelectedDateString() {
	setSelectedDateString(DATE_FORMAT.format(getSelectedDate()));
    }
    
    public String getSelectedDateString() {
        return selectedDateString;
    }

    public void setSelectedDateString(String selectedDateString) {
        this.selectedDateString = selectedDateString;
    }

    public String getDatePattern() {
        return datePattern;
    }

    public Locale getLocale() {
        return locale;
    }

    public TimeZone getTimeZone() {
        return timeZone;
    }

    public void setDatePattern(String datePattern) {
        this.datePattern = datePattern;
    }

    public void setLocale(Locale locale) {
	this.locale = locale;
    }

    public void setTimeZone(TimeZone timeZone) {
	this.timeZone = timeZone;
    }
    
    public static Calendar getCalendar() {
	return Calendar.getInstance(CalendarTestBean.TIME_ZONE, CalendarTestBean.LOCALE);
    }
    
    /**
     * Setup the default date
     */
    private static void setupDefaultDate() {
	DEFAULT_DATE = getDayInMay(10);
    }
    
    public static Date getDayInMay(int dayOfMonth) {
	Calendar calendar = getCalendar();
	calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	calendar.set(Calendar.MONTH, Calendar.MAY);
	calendar.set(Calendar.YEAR, 2008);
	calendar.set(Calendar.HOUR_OF_DAY, 18);
	calendar.set(Calendar.MINUTE, 25);
	calendar.set(Calendar.SECOND, 0);
	calendar.set(Calendar.MILLISECOND, 0);
	
	return calendar.getTime();
    }
    
    /**
     * Setup the default converter
     */
    private static void setupDefaultConverter() {	
	DEFAULT_CONVERTER = new DateTimeConverter();

	DEFAULT_CONVERTER.setPattern(DATE_PATTERN);
	DEFAULT_CONVERTER.setLocale(LOCALE);
	DEFAULT_CONVERTER.setTimeZone(TIME_ZONE);
	
	try {
	    Method method = null;
	    Method[] declaredMethods = DateTimeConverter.class.getDeclaredMethods();
	    for (int i = 0; i < declaredMethods.length; i++) {
		if (declaredMethods[i].getName().equals("getDateFormat")) {
		    method = declaredMethods[i];
		    break;
		}
	    }
	    if (method != null) {

		method.setAccessible(true);
		DATE_FORMAT = (DateFormat) method.invoke(DEFAULT_CONVERTER, DEFAULT_CONVERTER.getLocale());
		DATE_FORMAT.setTimeZone(DEFAULT_CONVERTER.getTimeZone());
	    }
	} catch (Exception e) {
	    // skip exception
	}
    }
}
