package test.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class CustomItemTest extends Canvas implements EntryPoint {
	
	private class CustomDataType {
		private int field1 = 0;
		private int field2 = 0;
		public CustomDataType(int field1, int field2) {
			this.field1 = field1;
			this.field2 = field2;
		}
		
		public int getField1() {
			return field1;
		}
		
		public int getField2() {
			return field2;
		}
		
		public String toString() {
			return "who are you and why are you calling toString?";
		}
	}
	
	private class CustomWidget extends HLayout {
		private CustomDataType value = null;
		private HTMLFlow field1 = null;
		private HTMLFlow field2 = null;
		
		public CustomWidget() {
			super();
			this.field1 = new HTMLFlow();
			this.field2 = new HTMLFlow();
			//setValue(new CustomDataType(0, 0)); //This works as expected
			this.addMember(field1);
			this.addMember(field2);
			this.setBackgroundColor("red"); //at least we see something red 
		}
		
		//If you uncomment above setValue(0,0)
		//Then the values stay at (0,0)
		//So the problem is that our custom setValue is never called by the framework
		public void setValue(CustomDataType newValue) {
			this.value = newValue;
			if (value != null) {
				this.field1.setContents("Field 1 is " + value.getField1());
				this.field2.setContents("Field 2 is " + value.getField2());
			} else {
				this.field1.setContents("No value given");
				this.field2.setContents("");
			}
		}
		
		
		public CustomDataType getValue() {
			return this.value;
		}
		
	}
	
	private class CustomItem extends CanvasItem {
		private CustomWidget widget = null;
		
		public CustomItem() {
			super();
			this.widget = new CustomWidget();
			this.widget.setHeight(30);
			this.setCanvas(widget);
			
			this.setEmptyDisplayValue("Empty Item"); 
			//for some reason, I see "undefined". Probably JavaScript's "undefined" value ?
			// and only after you click a few times on the rows
		}
		
		public void setValue(CustomDataType value) {
			this.widget.setValue(value);
		}
		
		
		
	}
	

	public void onModuleLoad() {
		
		
		this.setHeight100();
		this.setWidth100();
		
		VLayout layout = new VLayout();
		layout.setHeight100();
		layout.setWidth100();		
		layout.addMember(getTest1());
		
		this.addChild(layout);
		this.draw();

	}
	
	private ListGrid getTest1() {
		final ListGrid grid = new ListGrid();
		grid.setHeight("50%");
		grid.setWidth("90%");
		
		grid.setAlwaysShowEditors(true);
		//Actually, I want to always show editors only for my custom fields
		// And I only see 1 custom item when I click on a call, not in all rows.
		// But the framework text item editor, is shown on all rows.
		grid.setEditByCell(false);
		
		//grid.setCanEdit(true);
		
		DataSource ds = new DataSource();
		ds.setClientOnly(true);
		
		//our custom field
		DataSourceField dsField = new DataSourceField();
		dsField.setName("customField"); //transforms title into "Custom Field"
		dsField.setEditorType(new CustomItem());
		ds.addField(dsField);
		
		//as test, add a normal field
		dsField = new DataSourceTextField("frameworkField"); //transforms title into "Framework Field"
		ds.addField(dsField);

		
		//add test data
		//Record record = new Record(); //throw cast class exception
		ListGridRecord record1 = new ListGridRecord();
		record1.setAttribute("customField", new CustomDataType(5,6));
		record1.setAttribute("frameworkField", "Text field 5,6");
		
		ListGridRecord record2 = new ListGridRecord();
		record2.setAttribute("customField", new CustomDataType(9,0));
		record2.setAttribute("frameworkField", "Text field 9,0");
		
		ListGridRecord record3 = new ListGridRecord();
		//record3.setAttribute("customField", new CustomDataType(9,0)); //empty null value test
		record3.setAttribute("frameworkField", "Text field empty");

		ds.setTestData(new ListGridRecord[]{record1, record2, record3});

		grid.setDataSource(ds);
		grid.fetchData();
		
		return grid;
	}

}
