import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.HandlerRegistration;
import com.smartgwt.client.core.Function;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.calendar.Calendar;
import com.smartgwt.client.widgets.calendar.events.DayBodyClickEvent;
import com.smartgwt.client.widgets.calendar.events.DayBodyClickHandler;

public class ExtendedCalendar extends Calendar implements HasEventAddHandlers
{
	
	public ExtendedCalendar()
	{
	}
	
	/**
	 * Event add handler occurs when the user is trying to add a new event to the calendar.
	 * Adding this handler will disable any DayBodyClickHandler
	 * 
	 * @param handler
	 * @return
	 */
	public HandlerRegistration addEventAddHandler( final EventAddHandler handler )
	{
		JavaScriptObject calendarJS = this.getOrCreateJsObj();
		JavaScriptObject eventDialog = JSOHelper.getAttributeAsJavaScriptObject( calendarJS, "eventDialog" );
		
		
		// in monthView
		this.addDayBodyClickHandler( new DayBodyClickHandler()
		{
			public void onDayBodyClick( DayBodyClickEvent event )
			{
				CalendarEventAdd eventAdd = getNewEventAdd();
				handler.onEventAdd( eventAdd );
				
				if ( eventAdd.isCancelled() )
				{
					event.cancel();
				}
			}
		} );
		
		// overrides the eventDialogWindow show method
		Function function = new Function()
		{
			public void execute()
			{
				CalendarEventAdd eventAdd = getNewEventAdd();
				handler.onEventAdd( eventAdd );
				if ( !eventAdd.isCancelled() )
				{	
					openDefaultWindows();
				}
			}
		};
		//saves the initial function before overriding
		JavaScriptObject initialShowFunction = JSOHelper.getAttributeAsJavaScriptObject( eventDialog, "show" );
		JSOHelper.setAttribute( eventDialog, "initialShowFunction", initialShowFunction );
		JSOHelper.setAttribute( eventDialog, "show", function );

		
		return doAddHandler( handler, CalendarEventAdd.getType() );
	}


	
	/**
	 * Should not be called if no EventAddHandler is not defined
	 */
	private native void openDefaultWindows()
	/*-{
	 obj = this.@com.smartgwt.client.widgets.BaseWidget::getJsObj()();
	 if(obj.eventDialog){
	 	obj.eventDialog.initialShowFunction();
	 }
	}-*/;

	/**
	 * Unselect the cells on calendar
	 */
	public native void clearCalendarSelection() /*-{
	 obj = this.@com.smartgwt.client.widgets.BaseWidget::getJsObj()();
	 if(obj.weekView){	 	
	 	obj.weekView.clearSelection();
	 }
	}-*/;

	private CalendarEventAdd getNewEventAdd()
	{
		JavaScriptObject eventDialog = JSOHelper.getAttributeAsJavaScriptObject( this.getOrCreateJsObj(), "eventDialog" );
		
		JavaScriptObject eventJS = JSOHelper.createJavaScriptArray();
		JSOHelper.setAttribute( eventJS, "startTime", JSOHelper.getAttributeAsDate( eventDialog, "currentStart" ) );
		JSOHelper.setAttribute( eventJS, "endTime", JSOHelper.getAttributeAsDate( eventDialog, "currentEnd" ) );
		try
		{
			JSOHelper.setAttribute( eventJS, "viewName", this.getCurrentViewName() );
		}
		catch( Exception e )
		{
			GWT.log( "No view is currently selected", e );
		}
		
		return new CalendarEventAdd( eventJS );
	}
	
}

