/**
 * 
 */
package com.test.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.rpc.DMI;
import com.smartgwt.client.rpc.LoginRequiredCallback;
import com.smartgwt.client.rpc.RPCCallback;
import com.smartgwt.client.rpc.RPCManager;
import com.smartgwt.client.rpc.RPCRequest;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.Layout;


/**
 * @author ftoader
 *
 */
public class CoreModuleEntryPoint implements EntryPoint {
	
	/** the panel of application*/
	private Layout appWidget = new Layout();
	
	
	/* (non-Javadoc)
	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
	 */
	@Override
	public void onModuleLoad() {

		
		RPCManager.setLoginRequiredCallback(new LoginRequiredCallback() {			
			@Override
			public void loginRequired(int transactionNum, RPCRequest request,
					RPCResponse response) {
				BooleanCallback callback = new BooleanCallback() {					
					@Override
					public void execute(Boolean value) {
						Cookies.removeCookie("JSESSIONID");
						Window.Location.reload();					
					}
				};
				SC.warn("Session time-out", callback);		
			}
		});		
		DMI.call("test", "com.test.service.SessionService", "setSession", new RPCCallback() {
			
			@Override
			public void execute(RPCResponse response, Object rawData, RPCRequest request) {			
				final ListGrid grid = new ListGrid();
				final DataSource ds = DataSource.get("test");
				grid.setDataSource(ds);
				grid.fetchData();
				IButton but = new IButton("Export");
				IButton but1 = new IButton("Refresh");
				but.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						grid.exportData();				
					}
				});
				but1.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						System.out.println("It works");						
						Criteria c = new Criteria();
						ds.fetchData(c, new DSCallback() {
							
							@Override
							public void execute(DSResponse response, Object rawData, DSRequest request) {								
								
							}
						});
					}
				});
				appWidget.addMember(grid);
				appWidget.addMember(but);
				appWidget.addMember(but1);
				appWidget.setWidth(1000);
				appWidget.setHeight(800);
				RootPanel.get().add(appWidget);	
			}
		}, new Object[] {});	;
		
	}

}
