import java.util.Date;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.HasHandlers;
import com.smartgwt.client.event.Cancellable;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.events.BrowserEvent;

public class CalendarEventAdd extends BrowserEvent<EventAddHandler> implements Cancellable
{

	private static Type<EventAddHandler>	TYPE;
	
	private Date													startTime;
	private Date													endTime;
	private String												viewName;
	
	private boolean												cancelled	= false;
	
	public CalendarEventAdd( JavaScriptObject jsObj )
	{
		super( jsObj );
		
		startTime = JSOHelper.getAttributeAsDate( jsObj, "startTime" );
		endTime = JSOHelper.getAttributeAsDate( jsObj, "endTime" );
		viewName = JSOHelper.getAttribute( jsObj, "viewName" );
	}
	
	/**
	 * Fires a open event on all registered handlers in the handler manager.If no
	 * such handlers exist, this method will do nothing.
	 *
	 * @param <S> The event source
	 * @param source the source of the handlers
	 * @param jsObj the native event
	 */
	public static <S extends HasEventAddHandlers & HasHandlers> void fire( S source, JavaScriptObject jsObj )
	{
		if ( TYPE != null )
		{
			CalendarEventAdd event = new CalendarEventAdd( jsObj );
			source.fireEvent( event );
		}
	}
	
	@Override
	protected void dispatch( EventAddHandler handler )
	{
		handler.onEventAdd( this );
	}
	
	/**
	 * Gets the type associated with this event.
	 *
	 * @return returns the handler type
	 */
	public static Type<EventAddHandler> getType()
	{
		if ( TYPE == null )
		{
			TYPE = new Type<EventAddHandler>();
		}
		return TYPE;
	}

	@Override
	public com.google.gwt.event.shared.GwtEvent.Type<EventAddHandler> getAssociatedType()
	{
		return TYPE;
	}

	/**
	 * Gets the event start time, 
	 * if week view is not selected, returns null
	 * @return startTime
	 */
	public Date getStartTime()
	{
		return startTime;
	}

	/**
	 * Gets the event end time, 
	 * if week view is not selected, returns null
	 * @return endTime
	 */
	public Date getEndTime()
	{
		return endTime;
	}
	
	/**
	 * Returns the current view name
	 * @return view Name
	 */
	public String getViewName()
	{
		return viewName;
	}

	/**
	 * Cancel the add event
	 * Use it to prevent default event windows to show
	 */
	public void cancel()
	{
		cancelled = true;
	}

	/**
	 * Returns if the event has been cancelled
	 */
	public boolean isCancelled()
	{
		return cancelled;
	}

	
}
