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

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.ImgButton;
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.MiniDateRangeItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * Entry point classes define <code>onModuleLoad()</code>
 */
public class SmartGWT implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		final Layout mainLayout = new VLayout(5);
		mainLayout.setWidth100();

        final DynamicForm form = new DynamicForm();
        final MiniDateRangeItem dateSelector = new MiniDateRangeItem("mndri","Select date");
        dateSelector.setRequired(false);
        form.setFields(dateSelector);
        final IButton validate = new IButton("Click");

        mainLayout.addMember(form);
        mainLayout.addMember(validate);

        // ----- 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=");
        // ----- 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);

        final ListGrid listGrid = new ListGrid();
        listGrid.setFields(ituCode,refNumbers,actors);
		listGrid.setHeight("530px");
        listGrid.setWidth("900px");
		listGrid.setHeaderHeight(50);
		listGrid.setCellHeight(50);
		listGrid.setDataPageSize(25);
		listGrid.setAutoFetchData(true);
		listGrid.setShowFilterEditor(false);
		listGrid.setDataSource(LoadingUnitDataSource.getInstance());

        validate.addClickHandler(new ClickHandler(){
            @Override
            public void onClick(ClickEvent clickEvent) {
                SC.say("Date:"+"<br/>"+
                    "From = " + dateSelector.getFromDate() + "<br/>" +
                    "To = " + dateSelector.getToDate() );
                Criteria c = new Criteria();
                c.addCriteria("dateFrom",dateSelector.getFromDate());
                c.addCriteria("dateTo",dateSelector.getToDate());
                listGrid.fetchData(c);
            }
        });

        final Layout listLayout = new VLayout(5);
        listLayout.addMember(listGrid);

        final ImgButton showContainer = new ImgButton();
        showContainer.setHeight(16);
        showContainer.setWidth(16);
        showContainer.setLayoutAlign(Alignment.CENTER);
        showContainer.setSrc("./images/ico-open.jpg");
        showContainer.setShowRollOver(false);
        showContainer.setShowDown(false);
        showContainer.setShowFocused(false);
        showContainer.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                if ( getBool() ) {
                    alert("This is a test");
                }
            }

            public native void alert(String str) /*-{
                $wnd.alert(str);
            }-*/;

            public native boolean getBool() /*-{
                return true;
            }-*/;
        });

        RootPanel.get("myButton").add(showContainer);
		RootPanel.get("gwtFormContent").add(mainLayout);
        RootPanel.get("gwtListContent").add(listLayout);
	}
}
