package com.alea.client;

import com.alea.client.data.TransportResultDataSource;
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.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
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.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

public class TestGwt implements EntryPoint {

	protected final Messages messages = GWT.create(Messages.class);
	
	@Override
	public void onModuleLoad() {
		
		final ListGrid listGrid = new ListGrid();
		final TransportResultDataSource dataSource = TransportResultDataSource.getInstance();
		final TextBox inputItu = new TextBox();
		final Button searchItu = new Button(messages.buttonSearch());
		Layout toplayout = new HLayout(3);
		toplayout.addMember(inputItu);
		toplayout.addMember(searchItu);
		
		VLayout layout = new VLayout(0);
		ListGridField ituCode = new ListGridField("ituCode","Itu Code");
		ituCode.setFrozen(true);
		ituCode.setWidth(120);
		ListGridField lastStatus = new ListGridField("lastStatus","Last Status");
		lastStatus.setAlign(Alignment.CENTER);  
		lastStatus.setType(ListGridFieldType.IMAGE);
		lastStatus.setImageURLPrefix("status/");  
		lastStatus.setImageURLSuffix(".gif");  
		lastStatus.setImageWidth(32);
		lastStatus.setImageHeight(24);
		lastStatus.setFrozen(true);
		lastStatus.setWidth(75);
		ListGridField terminals = new ListGridField("terminals","Departure Terminal<br/>Destination Terminal<br/>Final Terminal");
		terminals.setWidth(150);
		ListGridField fullOrEmpty = new ListGridField("fullOrEmpty","Full/Empty");
		fullOrEmpty.setWidth(100);
		ListGridField weights = new ListGridField("weights","Net Weight (kg)<br/>Gross Weight(kg)");
		weights.setWidth(150);
		ListGridField statusDate = new ListGridField("statusDate","Status Date");
		statusDate.setWidth(120);
		ListGridField departureDate = new ListGridField("departureDate","Departure Date");
		departureDate.setWidth(120);
		ListGridField actors = new ListGridField("actors","Invoicing<br/>Sender<br/>Receiver");
		actors.setWidth(250);
		ListGridField refNumbers = new ListGridField("refNumbers","Customer Ref<br/>Operator Ref");
		refNumbers.setWidth(200);
		ListGridField controlNumbers = new ListGridField("controlNumbers","Ctrl train Number<br/>Railway train number<br/>Wagon number");
		controlNumbers.setWidth(200);
		
		listGrid.setFields(ituCode,lastStatus,terminals,fullOrEmpty,weights,statusDate,departureDate,actors,refNumbers,controlNumbers);
		listGrid.setWidth100();
		listGrid.setHeight("560px");
		listGrid.setHeaderHeight(50);
		listGrid.setCellHeight(50);
		listGrid.setDataPageSize(50);
		listGrid.setAutoFetchData(true);
		listGrid.setShowFilterEditor(true);
		listGrid.setFilterOnKeypress(true);
		listGrid.setDataSource(dataSource);

		layout.addChild(listGrid);
		layout.setWidth100();
		
		searchItu.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				Criteria c = new Criteria();
				c.addCriteria("ituCode",inputItu.getText());
				listGrid.fetchData(c, new DSCallback() {
					public void execute(DSResponse response, Object rawData, DSRequest request) {
						int res = response.getHttpResponseCode();
						// listGrid.setData(response.getData());
						Window.alert("response code : " + res);
					}
				});
			}
		});
		
		RootPanel.get().add(toplayout);
		RootPanel.get().add(layout);
	}

}
