package com.lstreetc.pathway.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.MultiComboBoxLayoutStyle;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.widgets.IButton;
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.FormItem;
import com.smartgwt.client.widgets.form.fields.MultiComboBoxItem;
import com.smartgwt.client.widgets.layout.VStack;


public class MultIntStringPickerTest implements EntryPoint {

	public static DataSourceTextField pkTextDSField( String name, String title )
	{
		DataSourceTextField field = new DataSourceTextField( name, title );
		field.setPrimaryKey( true );
		field.setCanEdit( false );
		field.setCanSave( false );
		return field;
	}

	public static class LookupDS extends DataSource {

		public static final String	INT_STR_PK_ID	= "id";
		public static final String	NAME			= "name";

		private static LookupDS instance;
		public static LookupDS instance()
		{
			if ( instance == null )
				instance = new LookupDS( "lookupDS" );

			return instance;
		}

		public LookupDS( String id )
		{
			super();

			setID( id );

			setDataFormat( DSDataFormat.JSON );
			setClientOnly( true );
			setDataURL( "ds/test_data/lookupdata.json" );
			setFields( newDSFields() );

//			setTestData( newLookupRecords() );
		}

		private DataSourceField[] newDSFields()
		{
			ArrayList<DataSourceField> array = new ArrayList<DataSourceField>();

			array.add( pkTextDSField( INT_STR_PK_ID, "ID" ) );
			array.add( new DataSourceTextField( NAME ) );

			return array.toArray( new DataSourceField[array.size()] );
		}

//		private Record[] newLookupRecords()
//		{
//			ArrayList<Record> array = new ArrayList<Record>();
//
//			array.add( createLookupRecord( "1", "one" ) );
//			array.add( createLookupRecord( "2", "two" ) );
//			array.add( createLookupRecord( "3", "three" ) );
//			array.add( createLookupRecord( "4", "four" ) );
//			array.add( createLookupRecord( "5", "five" ) );
//
//			return array.toArray( new Record[array.size()] );
//		}

		public Record createLookupRecord( String intStrPkID, String name )
		{
			Record record = new Record();

			record.setAttribute( INT_STR_PK_ID, intStrPkID );
			record.setAttribute( NAME, name );

			return record;
		}
	}

	public static class EditRecordDS extends DataSource {

		public static final String	ID				= "id";
		public static final String	FK_ID_STRINGS	= "fkIDStrings";

		private static EditRecordDS instance;
		public static EditRecordDS instance()
		{
			if ( instance == null )
				instance = new EditRecordDS( "editRecordDS" );

			return instance;
		}

//		private static Record editRecord;
//		public static Record editRecord()
//		{
//			if ( editRecord == null )
//			{
//				Record record = new Record();
//				record.setAttribute( FK_ID_STRINGS, new String[] { "1", "3", "5" } );
//				editRecord = record;
//			}
//
//			return editRecord;
//		}

		public EditRecordDS( String id )
		{
			super();

			setID( id );

			setDataFormat( DSDataFormat.JSON );
			setClientOnly( true );
			setDataURL( "ds/test_data/editdata.json" );
			setFields( newDSFields() );

//			setTestData( newEditRecords() );
		}

		private DataSourceField[] newDSFields()
		{
			ArrayList<DataSourceField> array = new ArrayList<DataSourceField>();

			array.add( pkTextDSField( ID, "ID" ) );
			array.add( multipleIntStrFieldDSField() );

			return array.toArray( new DataSourceField[array.size()] );
		}

		private DataSourceTextField multipleIntStrFieldDSField()
		{
			DataSourceTextField field = new DataSourceTextField( FK_ID_STRINGS );

			field.setForeignKey( LookupDS.instance().getID() + "." + LookupDS.INT_STR_PK_ID );
			field.setValidOperators( OperatorId.EQUALS );
			field.setMultiple( true );

			return field;
		}

//		private Record[] newEditRecords()
//		{
//			ArrayList<Record> array = new ArrayList<Record>();
//
//			array.add( editRecord() );
//
//			return array.toArray( new Record[array.size()] );
//		}
	}

	public void onModuleLoad()
	{
		VStack vStack = new VStack();

		vStack.addMember( form() );
		vStack.addMember( editButton() );

		vStack.draw();
	}

	private IButton editButton()
	{
		IButton button = new IButton( "Edit" );

		button.addClickHandler( new ClickHandler() {

			public void onClick( ClickEvent event )
			{
				EditRecordDS.instance().fetchData( null, new DSCallback() {

					public void execute( DSResponse dsResponse, Object data, DSRequest dsRequest )
					{
						Record[] records = dsResponse.getData();
						form().editRecord( records[0] );
					}
				} );
			}
		} );

		return button;
	}

	private DynamicForm form;
	public DynamicForm form()
	{
		if ( form == null )
		{
			DynamicForm _form = new DynamicForm();

			_form.setDataSource( EditRecordDS.instance() );
			_form.setFields( newFields() );
			_form.setAutoFetchData( false );

			form = _form;
		}

		return form;
	}

	private FormItem[] newFields()
	{
		ArrayList<FormItem> array = new ArrayList<FormItem>();

		array.add( newPickListField() );

		return array.toArray( new FormItem[array.size()] );
	}

	private MultiComboBoxItem newPickListField()
	{
		MultiComboBoxItem field = new MultiComboBoxItem( EditRecordDS.FK_ID_STRINGS );

		field.setOptionDataSource( LookupDS.instance() );
		field.setDisplayField( LookupDS.NAME );
		field.setValueField( LookupDS.INT_STR_PK_ID );
		field.setLayoutStyle( MultiComboBoxLayoutStyle.VERTICALREVERSE );

		return field;
	}
}
