package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
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.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Test implements EntryPoint {
	private RestDataSource mainDataSource;
	private RestDataSource optionDataSource;

	@Override
	public void onModuleLoad () {
		final RootPanel rootPanel = RootPanel.get ();

		final FilterBuilder filterBuilder = new FilterBuilder ();
		filterBuilder.setDataSource (getMainDataSource ());

		final ListGrid listGrid = new ListGrid ();
		listGrid.setWidth (600);
		listGrid.setDataFetchMode (FetchMode.LOCAL);
		listGrid.setAutoFetchData (true);
		listGrid.setDataSource (getMainDataSource ());

		final ListGridField codeField = new ListGridField ("code");
		final ListGridField nameField = new ListGridField ("name");
		final ListGridField optionField = new ListGridField ("option");
		//NEEDED to show the display value on the list grid (a duplication of what declared on the data source)
		optionField.setOptionDataSource (getOptionDataSource ());
		optionField.setValueField ("id");
		optionField.setDisplayField ("name");
		listGrid.setFields (codeField, nameField, optionField);

		final DynamicForm form = new DynamicForm ();
		form.setDataSource (getMainDataSource ());

		listGrid.addRecordClickHandler (new RecordClickHandler () {

			@Override
			public void onRecordClick (final RecordClickEvent event) {
				form.editRecord (event.getRecord ());
			}
		});

		final HiddenItem pkItem = new HiddenItem ("id");
		final StaticTextItem codeItem = new StaticTextItem ("code");
		codeItem.setWrap (false);
		final StaticTextItem nameItem = new StaticTextItem ("name");
		nameItem.setWrap (false);

		final StaticTextItem optionItem = new StaticTextItem ("option");
		optionItem.setWrap (false);
		//NEEDED to show the display value on the form (duplicates the data source declaration)
		optionItem.setOptionDataSource (getOptionDataSource ());
		optionItem.setValueField ("id");
		optionItem.setDisplayField ("name");
		//NEEDED to show the display value on the form WITHOUT a drop-down list
		//the screenshot was taken without the following row
		optionItem.setEditorType (new StaticTextItem ());

		form.setFields (pkItem, codeItem, nameItem, optionItem);

		rootPanel.setHeight ("600px");
		rootPanel.add (filterBuilder);
		rootPanel.add (listGrid);
		rootPanel.add (form);

	}

	private RestDataSource getMainDataSource () {
		if (mainDataSource == null) {
			mainDataSource = new RestDataSource ();
			mainDataSource.setDataFormat (DSDataFormat.JSON);
			mainDataSource.setFetchDataURL ("data/mainds_fetch.js");

			final DataSourceIntegerField pkField = new DataSourceIntegerField ("id", "ID");
			pkField.setPrimaryKey (true);
			mainDataSource.addField (pkField);

			final DataSourceTextField codeField = new DataSourceTextField ("code", "CODE");
			mainDataSource.addField (codeField);

			final DataSourceTextField nameField = new DataSourceTextField ("name", "NAME");
			mainDataSource.addField (nameField);

			final DataSourceIntegerField optionField = new DataSourceIntegerField ("option", "OPTION");
			//NEEDED to let the filter builder show a drop-down list
			final SelectItem optionsItem = new SelectItem ();
			optionsItem.setName (optionField.getName ());
			optionsItem.setOptionDataSource (getOptionDataSource ());
			optionsItem.setValueField ("id");
			optionsItem.setDisplayField ("name");
			optionField.setEditorType (optionsItem);

			mainDataSource.addField (optionField);
		}
		return mainDataSource;
	}

	private RestDataSource getOptionDataSource () {
		if (optionDataSource == null) {
			optionDataSource = new RestDataSource ();
			optionDataSource.setDataFormat (DSDataFormat.JSON);
			optionDataSource.setFetchDataURL ("data/optionds_fetch.js");

			final DataSourceIntegerField pkField = new DataSourceIntegerField ("id", "ID");
			optionDataSource.addField (pkField);

			final DataSourceTextField nameField = new DataSourceTextField ("name", "NAME");
			optionDataSource.addField (nameField);
		}
		return optionDataSource;
	}
}
