package com.foo.moo.client;

import java.util.Date;

import com.foo.moo.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class JavaDateBugTest implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final TextBox nameField = new TextBox();
		nameField.setText("Enter Date in format YYYY/MM/DD");
		
		final Button convertButton = new Button("Show Date");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(convertButton);

		convertButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String dateString = nameField.getText();
				String[] ymd = dateString.split("/");
				if (ymd == null || ymd.length != 3) return;
				Integer year = Integer.parseInt(ymd[0]);
				Integer month = Integer.parseInt(ymd[1]);
				Integer date = Integer.parseInt(ymd[2]);
				
				Date javaDate = doDateConversion(year,month,date);
				Window.alert("Java date from JS date:" + javaDate.toString());
			}
		});
	}
	
	// doDateConversion - JSNI method to demonstrate issue
	public native Date doDateConversion(int year, int month, int date) /*-{
		var jsdate = new Date(year, month-1, date, 0,0,0);
		alert("Passed:"+ [year,month,date].join("/") + " - js date:" + jsdate.toString());
		
		return @com.foo.moo.client.JavaDateBugTest::toJavaDate(D)(jsdate.getTime());
		
	}-*/;
	
	public static Date toJavaDate(double millis) {
		return new Date((long)millis);
	}
}
