/**
 * 
 */
package org.richfaces.component;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlForm;

import org.ajax4jsf.tests.AbstractAjax4JsfTestCase;
import org.apache.commons.lang.StringUtils;

import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlScript;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;

/**
 * @author Nick Belaevski - mailto:nbelaevski@exadel.com created 08.06.2007
 * 
 */
public class CalendarComponentTest extends AbstractAjax4JsfTestCase {

	private UIComponent form;

	private UICalendar calendar;

	private UICalendar calendar1;

	private UICalendar calendar2;
	
	private static Set javaScripts = new HashSet();

	static {
		javaScripts.add("org.ajax4jsf.javascript.PrototypeScript");
		javaScripts.add("org.ajax4jsf.javascript.AjaxScript");
		javaScripts.add("org/richfaces/renderkit/html/scripts/events.js");
		javaScripts.add("org/richfaces/renderkit/html/scripts/utils.js");
		javaScripts
				.add("org/richfaces/renderkit/html/scripts/json/json-dom.js");
		javaScripts.add("org/richfaces/renderkit/html/scripts/calendar.js");
		javaScripts.add("org/richfaces/renderkit/html/scripts/scriptaculous/effects.js");
		javaScripts.add("org/richfaces/renderkit/html/scripts/JQuerySpinBtn.js");
		javaScripts.add("org/richfaces/renderkit/html/scripts/jquery/jquery.js");
	}

	public CalendarComponentTest(String name) {
		super(name);
	}

	public void setUp() throws Exception {

		super.setUp();

		form = new HtmlForm();
		form.setId("form");
		facesContext.getViewRoot().getChildren().add(form);
		calendar = (UICalendar) application
				.createComponent(UICalendar.COMPONENT_TYPE);
		calendar1 = (UICalendar) application
				.createComponent(UICalendar.COMPONENT_TYPE);
		calendar.setLocale(Locale.UK);
		calendar.setDatePattern("d/MM/yyyy");
		calendar.setTimeZone(TimeZone.getTimeZone("GMT+10:00"));
		/*
		 * String[] strarr = TimeZone.getAvailableIDs(); for(int i=0; i <
		 * strarr.length; i++){ System.out.println(strarr[i]); }
		 */
		calendar.setButtonLabel("PopUp");
		// XXX test popup false
		// XXX test CurrentDate = null
		calendar.setPopup(true);
		Calendar calendarObject = Calendar.getInstance();
		calendarObject.set(2001, Calendar.SEPTEMBER, 11);
		calendar.setId("calendar");
		calendar.setSubmittedValue(calendarObject.getTime());
		form.getChildren().add(calendar);
		calendar1.setId("_calendar");
		calendar1.setPopup(false);
		calendar1.setCurrentDate(null);
		form.getChildren().add(calendar1);	
		
		calendar2 = (UICalendar) application
		.createComponent(UICalendar.COMPONENT_TYPE);
		calendar2.setDatePattern("dd/M/yy HH:mm");
		calendarObject.set(2001, Calendar.SEPTEMBER, 11, 13, 36);
		calendar2.setSubmittedValue(calendarObject.getTime());
		calendar2.setId("timecalendar");
		form.getChildren().add(calendar2);
	}

	/*
	 * public void testTidy() throws Exception { UIViewRoot viewRoot =
	 * facesContext.getViewRoot(); List children = viewRoot.getChildren();
	 * UIComponent calendar =
	 * application.createComponent(UICalendar.COMPONENT_TYPE);
	 * children.add(calendar); HtmlOutputText output = (HtmlOutputText)
	 * application.createComponent(HtmlOutputText.COMPONENT_TYPE);
	 * output.setValue("<br>&nbsp;&amp;<a href='#'>");
	 * output.setEscape(false); calendar.getChildren().add(output);
	 * 
	 * HtmlPage renderView = renderView();
	 * System.out.println(renderView.asXml()); }
	 */
	public void testPreloadRanges() throws Exception {
		UICalendar calendar = (UICalendar) application
				.createComponent(UICalendar.COMPONENT_TYPE);
		calendar.setLocale(Locale.FRENCH);
		Calendar calendarObject = Calendar.getInstance();
		calendarObject.set(2007, Calendar.JUNE, 10);

		Calendar preloadRangeBegin = Calendar.getInstance();
		preloadRangeBegin.setTime(calendar
				.getDefaultPreloadBegin(calendarObject.getTime()));
		assertEquals(2007, preloadRangeBegin.get(Calendar.YEAR));
		assertEquals(Calendar.JUNE, preloadRangeBegin.get(Calendar.MONTH));
		assertEquals(1, preloadRangeBegin.get(Calendar.DATE));

		Calendar preloadRangeEnd = Calendar.getInstance();
		preloadRangeEnd.setTime(calendar.getDefaultPreloadEnd(calendarObject
				.getTime()));
		assertEquals(2007, preloadRangeEnd.get(Calendar.YEAR));
		assertEquals(Calendar.JUNE, preloadRangeEnd.get(Calendar.MONTH));
		assertEquals(30, preloadRangeEnd.get(Calendar.DATE));

		calendarObject.set(2007, Calendar.JANUARY, 1);
		preloadRangeBegin.setTime(calendar
				.getDefaultPreloadBegin(calendarObject.getTime()));
		assertEquals(2007, preloadRangeBegin.get(Calendar.YEAR));
		assertEquals(Calendar.JANUARY, preloadRangeBegin.get(Calendar.MONTH));
		assertEquals(1, preloadRangeBegin.get(Calendar.DATE));

		calendarObject.set(2007, Calendar.JUNE, 10);
		calendar.setLocale(Locale.US);

		preloadRangeBegin.setTime(calendar
				.getDefaultPreloadBegin(calendarObject.getTime()));
		assertEquals(2007, preloadRangeBegin.get(Calendar.YEAR));
		assertEquals(Calendar.JUNE, preloadRangeBegin.get(Calendar.MONTH));
		assertEquals(1, preloadRangeBegin.get(Calendar.DATE));

		preloadRangeEnd.setTime(calendar.getDefaultPreloadEnd(calendarObject
				.getTime()));
		assertEquals(2007, preloadRangeEnd.get(Calendar.YEAR));
		assertEquals(Calendar.JUNE, preloadRangeEnd.get(Calendar.MONTH));
		assertEquals(30, preloadRangeEnd.get(Calendar.DATE));
	}

	public void testGetPreloadDateRange() throws Exception {
		UICalendar calendar = (UICalendar) application
				.createComponent(UICalendar.COMPONENT_TYPE);
		calendar.setLocale(Locale.FRENCH);
		Calendar calendarObject = Calendar.getInstance();
		calendarObject.set(2007, Calendar.JUNE, 10);
		calendar.setCurrentDate(calendarObject.getTime());

		Date[] range = calendar.getPreloadDateRange();
		assertEquals(calendar.getPreloadDateRangeBegin(), range[0]);
		assertEquals(calendar.getPreloadDateRangeEnd(), range[range.length - 1]);
		assertEquals(30, range.length);
	}
		
	public void testCalendarRenderer() throws Exception {

		HtmlPage page = renderView();
		assertNotNull(page);
		//System.out.println(page.asXml());

		HtmlElement htmlSpan = page.getHtmlElementById(calendar.getClientId(facesContext)+"Popup");
		assertNotNull(htmlSpan);

		HtmlInput htmlCalendarInput = (HtmlInput) page
				.getHtmlElementById(calendar.getClientId(facesContext)
						+ "InputDate");
		HtmlButton htmlCalendarButton = (HtmlButton) page
				.getHtmlElementById(calendar.getClientId(facesContext)
						+ "PopupButton");
		HtmlSpan htmlCalendarSpan = (HtmlSpan) page.getHtmlElementById(calendar
				.getClientId(facesContext)
				+ "Popup");
		assertNotNull(htmlCalendarInput);
		assertNotNull(htmlCalendarButton);
		assertNotNull(htmlCalendarSpan);

		assertEquals("11/09/2001", htmlCalendarInput.getValueAttribute());

		HtmlElement htmlCalendar1Span = page.getHtmlElementById(calendar1
				.getClientId(facesContext)+"Popup");
		assertNotNull(htmlCalendar1Span);
		assertEquals("display: none", htmlCalendar1Span.getAttributeValue("style"));

		try {
			HtmlInput htmlCalendarInput1 = (HtmlInput) page
					.getHtmlElementById(calendar1.getClientId(facesContext)
							+ "InputDate");
		} catch (ElementNotFoundException e) {
			assertNotNull(e);
		}
		
		try {
			HtmlButton htmlCalendarButton1 = (HtmlButton) page
					.getHtmlElementById(calendar1.getClientId(facesContext)
							+ "PopupButton");
		} catch (ElementNotFoundException e) {
			assertNotNull(e);
		}
		try {
			HtmlSpan htmlCalendarSpan1 = (HtmlSpan) page
					.getHtmlElementById(calendar1.getClientId(facesContext)
							+ "Popup");
		} catch (ElementNotFoundException e) {
			assertNotNull(e);
		}
		//System.out.println(page.asXml());
	}

	public void testCalendarDecode() throws Exception {
		
		Calendar calendarObject = Calendar.getInstance();
		calendarObject.set(2001, Calendar.SEPTEMBER, 11);
		calendar.decode(facesContext);
		assertEquals(calendarObject.getTime(), calendar.getSubmittedValue());
		calendarObject.set(2002, Calendar.SEPTEMBER, 11, 1, 1, 1);
		calendar.updateCurrentDate(facesContext, calendarObject.getTime());
		assertEquals(calendarObject.getTime(), calendar.getCurrentDate());

	}

	public void testCalendarStyles() throws Exception {
		HtmlPage page = renderView();
		assertNotNull(page);
		//System.out.println(page.asXml());
		List links = page.getDocumentElement().getHtmlElementsByTagName("link");
		assertEquals(1, links.size());
		HtmlElement link = (HtmlElement) links.get(0);
		assertTrue(link.getAttributeValue("href").contains("css/calendar.xcss"));
	}

	public void testCalendarScrits() throws Exception {
		HtmlPage page = renderView();
		assertNotNull(page);

		List scripts = page.getDocumentElement().getHtmlElementsByTagName(
				"script");
		for (Iterator it = scripts.iterator(); it.hasNext();) {
			HtmlScript item = (HtmlScript) it.next();
			String srcAttr = item.getSrcAttribute();
			if (item.getFirstChild() != null) {
				String scriptBodyString = item.getFirstChild().toString();
				if (scriptBodyString.contains("new Calendar")&&scriptBodyString.contains("form:calendar")) {
					
					assertTrue(scriptBodyString.contains("datePattern"));
					assertTrue(scriptBodyString.contains("d/MM/yyyy"));
					assertTrue(scriptBodyString.contains("selectedDate"));
					assertTrue(scriptBodyString.contains("popup: true"));
					assertTrue(scriptBodyString.contains("currentDate:"));
					assertTrue(scriptBodyString.contains("selectedDate:"));
				}else if(scriptBodyString.contains("form:timecalendar")){
					
					assertTrue(scriptBodyString.contains("selectedDate: new Date(2001,8,11,13,36,0)"));					
				}
			}
			

			if (StringUtils.isNotBlank(srcAttr)) {
				boolean found = false;
				for (Iterator srcIt = javaScripts.iterator(); srcIt.hasNext();) {
					String src = (String) srcIt.next();
					found = srcAttr.contains(src);
					if (found) {
						break;
					}
				}
				assertTrue(found);
			}
		}
	}

	public void testCalendarTime() throws Exception{
		
			Date time =  (Date) calendar2.getValue();
			Calendar calendarObject = Calendar.getInstance();
			calendarObject.set(2001, Calendar.SEPTEMBER, 11, 13, 36);
			assertEquals(calendarObject.getTime(), calendar2.getSubmittedValue());
			
	}
	
	public void tearDown() throws Exception {
		super.tearDown();
		form = null;
		calendar = null;
	}
}
