package be.uniway.rvk.test.datasource.client;

import be.uniway.rvk.test.datasource.client.data.LoadingUnitDataSource;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.ExportFormat;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

public class SmartGWTServerDataSource implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Layout mainLayout = new VLayout(5);
		mainLayout.setWidth100();
		
		final Layout topLayout = new HLayout(2);
		topLayout.setWidth100();
		final Label titleLabel = new Label("<h1>Listgrid rows bug</h1>");
		titleLabel.setWidth100();
		titleLabel.setHeight("40px");
		titleLabel.setAlign(Alignment.LEFT);
		titleLabel.setValign(VerticalAlignment.TOP);
		topLayout.addMember(titleLabel);
		mainLayout.addMember(topLayout);
		
		final Layout formLayout = new HLayout(2);
		final IButton searchButton = new IButton("Load data");
		formLayout.addMember(searchButton);
		mainLayout.addMember(formLayout);

		final Layout listGridLayout = new HLayout(2);
		listGridLayout.setWidth(450);
		listGridLayout.setHeight(350);
		
		
		// ----- ITU Code
		ListGridField ituCode = new ListGridField("ituCode","ITU Code");
		ituCode.setType(ListGridFieldType.LINK);
		ituCode.setFrozen(true);
		ituCode.setWidth(100);
        ituCode.setAttribute("linkURLPrefix","showLoadingUnit.htm?uid=");
		// ----- Last Status image
		ListGridField lastStatus = new ListGridField("lastStatus","Last status" );
		lastStatus.setAlign(Alignment.CENTER);  
		lastStatus.setType(ListGridFieldType.IMAGE);
		lastStatus.setImageURLPrefix("../../images/status/"); // lastStatus.setImageURLPrefix("/images/status/");
		lastStatus.setImageURLSuffix(".gif");  
		lastStatus.setImageWidth(32);
		lastStatus.setImageHeight(24);
		lastStatus.setFrozen(true);
		lastStatus.setWidth(75);
		lastStatus.setCanFilter(false);
		// ----- Remarks
		ListGridField remarks = new ListGridField("remarks","Remarks");
		remarks.setFrozen(true);
		remarks.setAlign(Alignment.LEFT);
		remarks.setWidth(180);
		// ----- Custommer & Operator Reference numbers
		ListGridField refNumbers = new ListGridField("refNumbers","Reference numbers");
		refNumbers.setCanFilter(false);
		refNumbers.setFrozen(false);
		refNumbers.setWidth(150);
		// ----- Custommers (actors)
		ListGridField actors = new ListGridField("actors","Actors");
		actors.setCanFilter(false);
		actors.setWidth(250);
		actors.setFrozen(false);
		// ----- Departure Date
		final ListGridField departureDate = new ListGridField("departureDate","Departure date");
		departureDate.setCanFilter(true);
		departureDate.setType(ListGridFieldType.DATE);
		departureDate.setWidth(120);
		departureDate.setFrozen(false);
		// ----- Terminals 
		ListGridField terminals = new ListGridField("terminals","Terminals");
		terminals.setWidth(175);
		terminals.setCanFilter(false);
		terminals.setFrozen(false);
		// ----- Control numbers
		ListGridField controlNumbers = new ListGridField("controlNumbers","Control numbers");
		controlNumbers.setWidth(200);
		controlNumbers.setCanFilter(false);
		// ----- HIDDEN - Departure Terminal
		final ListGridField departureTerminal = new ListGridField("departureTerminal","departure terminal");
		departureTerminal.setCanFilter(true);
		departureTerminal.setHidden(true);
		departureTerminal.setWidth(150);
		// ----- HIDDEN - Destination Terminal
		final ListGridField destinationTerminal = new ListGridField("destinationTerminal","destination Terminal");
		destinationTerminal.setCanFilter(true);
		destinationTerminal.setHidden(true);
		destinationTerminal.setWidth(150);
		// ----- HIDDEN - Active transports
		ListGridField active = new ListGridField("active","active");
		active.setType(ListGridFieldType.BOOLEAN);
		active.setHidden(true);
		active.setCanFilter(true);
		active.setWidth(75);

		final ListGrid listGrid = new ListGrid() {
			@Override  
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
				String fieldName = this.getFieldName(colNum);
				if ( "remarks".equalsIgnoreCase(fieldName) ) {
                    return null;
                } else if ("ituCode".equalsIgnoreCase(fieldName)) {
                    record.setAttribute("linkURL","test");
                    return null;
				} else {
					return null;
				}
            }
		};
		listGrid.setShowRecordComponents(true);
		listGrid.setShowRecordComponentsByCell(true);
		listGrid.setFields(ituCode,lastStatus,remarks,refNumbers,actors,departureDate,terminals,controlNumbers,departureTerminal,destinationTerminal,active);
		listGrid.setWidth100();
		listGrid.setHeight("530px");
		listGrid.setHeaderHeight(50);
		listGrid.setCellHeight(50);
		listGrid.setDataPageSize(25);
		listGrid.setAutoFetchData(true);
		listGrid.setShowFilterEditor(false);
		listGrid.setDataSource(LoadingUnitDataSource.getInstance());
		
		listGridLayout.addMember(listGrid);
		mainLayout.addMember(listGridLayout);
		searchButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Criteria c = new Criteria();
				listGrid.fetchData(c);
			}
		});
		RootPanel.get("content").add(mainLayout);
	}
}
