/**
 * License Agreement.
 *
 * Rich Faces - Natural Ajax for Java Server Faces (JSF)
 *
 * 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 calendar;

import java.lang.reflect.Array;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.TimeZone;

import javax.faces.component.UIComponent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.webapp.UIComponentTag;

import org.richfaces.event.CurrentDateChangeEvent;

public class CalendarBean {

	private static final String [] WEEK_DAY_SHORT = new String[] { "Sun *",
			"Mon +", "Tue +", "Wed +", "Thu +", "Fri +", "Sat *" };
	private static final String [] WEEK_DAY = new String[] { "Monday",
			"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
	private static final String [] MOUNT_LABELS = new String[] { "January",
			"February", "March", "April", "May", "June", "July", "August",
			"September", "October", "November", "December" };
	private static final String [] MOUNT_LABELS_SHORT = new String[] { "Jan +",
			"Feb +", "Mar +", "Apr +", "May +", "Jun +", "Jul +", "Aug +",
			"Sep +", "Oct +", "Nov +", "Dec +" };

	private Locale locale;
	private boolean popup;
	private boolean readonly;
	private boolean showInput;
	private boolean enableManualInput;
	private boolean ajaxSingle;
	private boolean disabled;
	private boolean bypassUpdates;
	private boolean rendered;
	private boolean required;
	private Date currentDate;
	private Date selectedDate;
	private String preloadDateRangeBegin;
	private String preloadDateRangeEnd;
	private String weekDay;
	private String month;
	private String height;
	private String width;
	private String pattern;
	private String jointPoint;
	private String direction;
	private String boundary;
	private String icon;
	private String toolTipMode;
	private String scrollMode;
	private String label;
	private String timeZone;
	private int zindex;
	private int counter;
	

	public String getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
	}

	public void changeIcons() {
		if (icon != null) {
			icon = null;
			label = "Button label";
		} else {
			icon = "/pics/item.png";
			label = null;
		}
	}

	public Locale getLocale() {
		return locale;
	}

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

	public boolean isPopup() {
		return popup;
	}

	public void setPopup(boolean popup) {
		this.popup = popup;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {

		this.pattern = pattern;
	}

	public void selectPattern(ValueChangeEvent event) {
		String tPatern = (String) event.getNewValue();
		if (tPatern != null) {
			pattern = tPatern;
		}
	}

	public CalendarBean() {
		height = "400px";
		width = "400px";
		label = "Button label";
		locale = Locale.US;
		popup = true;
		pattern = "MMM d, yyyy";
		jointPoint = "bottom-left";
		direction = "bottom-right";
		readonly = true;
		enableManualInput = false;
		showInput = true;
		boundary = "inactive";
		icon = null;
		disabled = false;
		bypassUpdates = false;
		rendered = true;
		zindex = 2;
		toolTipMode = "none";
		scrollMode = "client";
		required = false;
		weekDay = "none";
		month = "none";
		timeZone = "Eastern European Time";
		preloadDateRangeBegin = "10.09.2007";
		preloadDateRangeEnd = "11.10.2007";
	}
	
	public Date getPrDateRangeBegin() {
		Calendar cal = Calendar.getInstance();
		StringTokenizer st = new StringTokenizer(preloadDateRangeBegin,".");
		ArrayList<Integer> date = new ArrayList<Integer>();
		while(st.hasMoreTokens()) {
			date.add(Integer.parseInt(st.nextToken()));
		}
		cal.set(date.get(2), date.get(1) - 1, date.get(0), 12, 0, 0);
		System.out.println(cal.getTime());
		return cal.getTime();
		
	}

	public Date getPrDateRangeEnd() {
		Calendar cal = Calendar.getInstance();
		StringTokenizer st = new StringTokenizer(preloadDateRangeEnd, ".");
		ArrayList<Integer> date = new ArrayList<Integer>();
		while(st.hasMoreTokens()) {
			date.add(Integer.parseInt(st.nextToken()));
		}
		cal.set(date.get(2), date.get(1) - 1, date.get(0), 12, 0, 0);
		System.out.println(cal.getTime());
		return cal.getTime();
	}

	public boolean isShowInput() {
		return showInput;
	}

	public void setShowInput(boolean showInput) {
		this.showInput = showInput;
	}

	public boolean isEnableManualInput() {
		return enableManualInput;
	}

	public void setEnableManualInput(boolean enableManualInput) {
		this.enableManualInput = enableManualInput;
	}

	public boolean isReadonly() {
		return readonly;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public void selectLocale(ValueChangeEvent event) {
		String tLocale = (String) event.getNewValue();
		if (tLocale != null) {
			String lang = tLocale.substring(0, 2);
			String country = tLocale.substring(3);
			locale = new Locale(lang, country, "");
		}
	}

	public Object getWeekDayLabels() {
		if(weekDay.equals("long"))
			return CalendarBean.WEEK_DAY;
		else return null;
	}

	public Object getWeekDayLabelsShort() {
		if(weekDay.equals("short"))
			return CalendarBean.WEEK_DAY_SHORT;
		else return null;
	}

	public Object getMonthLabels() {
		if(month.equals("long"))
			return CalendarBean.MOUNT_LABELS;
		else return null;
	}
	
	public void weekDay(ValueChangeEvent event) {
		weekDay = (String) event.getNewValue();
	}
	
	public Object getMonthLabelsShort() {
		if(month.equals("short"))
			return CalendarBean.MOUNT_LABELS_SHORT;
		else return null;
		
	}


	public void month(ValueChangeEvent event) {
		month = (String) event.getNewValue();
	}

	public String getCurrentDateAsText() {
		Date currentDate = getCurrentDate();
		if (currentDate != null) {
			return DateFormat.getDateInstance(DateFormat.FULL).format(
					currentDate);
		}

		return null;
	}

	public Date getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public Date getSelectedDate() {
		return selectedDate;
	}

	public void setSelectedDate(Date selectedDate) {
		this.selectedDate = selectedDate;
	}

	public String getJointPoint() {
		return jointPoint;
	}

	public void setJointPoint(String jointPoint) {
		this.jointPoint = jointPoint;
	}

	public void selectJointPoint(ValueChangeEvent event) {
		jointPoint = (String) event.getNewValue();
	}

	public String getDirection() {
		return direction;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public void selectDirection(ValueChangeEvent event) {
		direction = (String) event.getNewValue();
	}

	public String getBoundary() {
		return boundary;
	}

	public void setBoundary(String boundary) {
		this.boundary = boundary;
	}

	public void dcl(CurrentDateChangeEvent event) {
		System.out.println(event.getCurrentDateString());
		System.out.println("ajvhckndskncs");
	}

	public void ddd(ValueChangeEvent event) {
		System.out.println(event.getOldValue());
		System.out.println(event.getNewValue());

	}

	public int getCounter() {
		return counter++;
	}

	public boolean isAjaxSingle() {
		return ajaxSingle;
	}

	public void setAjaxSingle(boolean ajaxSingle) {
		this.ajaxSingle = ajaxSingle;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public boolean isBypassUpdates() {
		return bypassUpdates;
	}

	public void setBypassUpdates(boolean bypassUpdates) {
		this.bypassUpdates = bypassUpdates;
	}

	public String getHeight() {
		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public boolean isRendered() {
		return rendered;
	}

	public void setRendered(boolean rendered) {
		this.rendered = rendered;
	}

	public String getToolTipMode() {
		return toolTipMode;
	}

	public void setToolTipMode(String toolTipMode) {
		this.toolTipMode = toolTipMode;
	}

	public String getScrollMode() {
		return scrollMode;
	}

	public void setScrollMode(String scrollMode) {
		this.scrollMode = scrollMode;
	}

	public int getZindex() {
		return zindex;
	}

	public void setZindex(int zindex) {
		this.zindex = zindex;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public TimeZone getTmZone() {
		TimeZone tZone = TimeZone.getDefault();
		System.out.println("timeZone" + timeZone + " tZone " + tZone);
		tZone.setID(timeZone);
		System.out.println("timeZone" + timeZone + " tZone " + tZone);
		return tZone;
	}

	public String getPreloadDateRangeBegin() {
		return preloadDateRangeBegin;
	}

	public void setPreloadDateRangeBegin(String preloadDateRangeBegin) {
		this.preloadDateRangeBegin = preloadDateRangeBegin;
	}

	public String getPreloadDateRangeEnd() {
		return preloadDateRangeEnd;
	}

	public void setPreloadDateRangeEnd(String preloadDateRangeEnd) {
		this.preloadDateRangeEnd = preloadDateRangeEnd;
	}
}
