package com;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.rpc.RPCManager;
import com.smartgwt.client.types.PromptStyle;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.HTMLFlow;
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.grid.events.EditCompleteEvent;
import com.smartgwt.client.widgets.grid.events.EditCompleteHandler;
import com.smartgwt.client.widgets.grid.events.EditFailedEvent;
import com.smartgwt.client.widgets.grid.events.EditFailedHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class Initializer implements EntryPoint {

	/**
	 * This method is invoked when the application is started for the first
	 * time. This method based on appType will draw the Layout
	 * 
	 */
	public void onModuleLoad() {

		// What is the purpose of this call???
		RPCManager.setPromptStyle(PromptStyle.DIALOG);
		RPCManager.setFetchDataPrompt("Loading...");
		RPCManager.setSaveDataPrompt("Saving Changes...");
		RPCManager.setRemoveDataPrompt("Deleting data...");
		RPCManager.setDefaultPrompt("Processing...");

		try {
			SectionStack sectionstack = new SectionStack();
			sectionstack.setVisibilityMode(VisibilityMode.MULTIPLE);
			SectionStackSection stsectionst = new SectionStackSection();
			stsectionst.setTitle("Employee List");
			stsectionst.setExpanded(true);
			sectionstack.setWidth100();
			sectionstack.setHeight100();
			sectionstack.addSection(stsectionst);
			
			SectionStackSection stsectionOutPut = new SectionStackSection();
			stsectionOutPut.setTitle("Messagee List");
			stsectionOutPut.setExpanded(true);
			sectionstack.addSection(stsectionOutPut);
			
			DataSource clientDS = new DataSource();
			clientDS = DataSource.get("EmpTestDs");
			final ListGrid listgrid = new ListGrid();
			listgrid.setDataSource(clientDS);
			listgrid.setAutoFetchData(true);			
			listgrid.setCanEdit(true);
			listgrid.setAutoSaveEdits(false);
			
			stsectionst.addItem(listgrid);
			
			Button button = new Button("Save");
			stsectionst.addItem(button);
			
			final HTMLFlow htmlFlow = new HTMLFlow();  
	        htmlFlow.setWidth100();
	        htmlFlow.setHeight(300);
	        htmlFlow.setContents("<H1> Message IN addEditCompleteHandler ()</H1><Br>");  
	        htmlFlow.draw(); 
	        stsectionOutPut.addItem(htmlFlow);
			
			button.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					/*htmlFlow.clear();
					htmlFlow.setHeight(300);*/
					listgrid.saveAllEdits();
					listgrid.addEditCompleteHandler(new EditCompleteHandler() {
						@Override
						public void onEditComplete(EditCompleteEvent event) {
							com.smartgwt.client.data.DSResponse dsResponse = null;
							dsResponse = event.getDsResponse();
							if (dsResponse.getStatus() == 0) {
								JavaScriptObject jso = _getMessageElements(dsResponse.getJsObj());
								Object[] temp = JSOHelper.convertToArray(jso);
								
								for (int i = 0; i < temp.length; i++) {
									System.out.println("Message On Success:: " + temp[i].toString());
									htmlFlow.setContents(htmlFlow.getContents() + "Message  On Success :: " + temp[i].toString() + "<Br>");									
								}
							}
						}
					});

					listgrid.addEditFailedHandler(new EditFailedHandler() {
						@Override
						public void onEditFailed(EditFailedEvent event) {
							DSResponse dsResponse = event.getDsResponse();
							JavaScriptObject jso = _getMessageElements(dsResponse.getJsObj());
							Object[] temp = JSOHelper.convertToArray(jso);
							for (int i = 0; i < temp.length; i++) {
								System.out.println("Message On Fail:: " + temp[i].toString());
								htmlFlow.setContents(htmlFlow.getContents() + "Message On Fail :: " + temp[i].toString() + "<Br>");									
							}
						}
					});
				}
			});

			sectionstack.show();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}
	
	
	private static native JavaScriptObject _getMessageElements(JavaScriptObject dsResponse) /*-{
		if ($wnd.isc.isAn.Array(dsResponse.errors))
			return dsResponse.errors;
		return [];
	}-*/;

}
