package de.mwea.sgwt25.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Cursor;
import com.smartgwt.client.types.JSONDateFormat;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.JSONEncoder;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.FilterBuilder;
import com.smartgwt.client.widgets.form.fields.SelectItem;
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.DrawAreaChangedEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;

public class Sgwt25_checks implements EntryPoint {
	
	public void onModuleLoad() {
	
		final ToolStrip ts = new ToolStrip();
				
		// Filter Builder
		final ToolStripButton addFilter = new ToolStripButton("Filter");
		addFilter.setID("AddFilter");
		addFilter.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new MyFilterBuilder().show();
			}
		});
		
		ts.addButton(addFilter);
		RootPanel.get().add(ts);
	}
	
	private class MyFilterBuilder extends Window {

		String currentFilter;
		AdvancedCriteria ac;
		
		public MyFilterBuilder() {

			setIsModal(true);
			setHeight(600);
			setWidth(750);
			setTitle("Filter editor");
			setShowCloseButton(true);
			setShowMinimizeButton(false);
			setShowMaximizeButton(false);

			final VLayout layout = new VLayout();
			layout.setOverflow(Overflow.AUTO);
			layout.setWidth100();
			layout.setHeight100();
			layout.setPadding(5);

			// Create FilterBuilder
			final FilterBuilder filterBuilder = new FilterBuilder();
			filterBuilder.setTop(40);
			filterBuilder.setDataSource(DataSource.get("SAMPLE_DS"));
			layout.addMember(filterBuilder);

			// Toolstrip
			final ToolStrip toolStrip = new ToolStrip();
			toolStrip.setWidth100();

			final ToolStripButton saveButton = new ToolStripButton();
			saveButton.setTitle("Save filter");
			saveButton.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					ac = filterBuilder.getCriteria();
					JSONEncoder e = new JSONEncoder();
					e.setPrettyPrint(true);
					e.setStrictQuoting(true);
					e.setShowDebugOutput(true);
					e.setDateFormat(JSONDateFormat.DATE_CONSTRUCTOR);
					currentFilter = JSON.encode(ac.getJsObj(), e);
					System.out.println(currentFilter);
				}
			});
			toolStrip.addButton(saveButton);
			

			final ToolStripButton loadButton = new ToolStripButton();
			loadButton.setTitle("Load filter from JSON-String");
			loadButton.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					final JavaScriptObject jso = JSON.decode(currentFilter);
					final AdvancedCriteria c = new AdvancedCriteria(jso);
					filterBuilder.setCriteria(c);
				}
			});
			toolStrip.addButton(loadButton);

			final ToolStripButton loadButton2 = new ToolStripButton();
			loadButton2.setTitle("Load filter from Criteria");
			loadButton2.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					filterBuilder.setCriteria(ac);
				}
			});
			toolStrip.addButton(loadButton2);

			
			final ToolStripButton clearButton = new ToolStripButton();
			clearButton.setTitle("Clear filter");
			clearButton.addClickHandler(new ClickHandler() {
				
				public void onClick(ClickEvent event) {
					filterBuilder.clearCriteria();
				}
			});
			toolStrip.addButton(clearButton);

			layout.addMember(toolStrip);
			addItem(layout);			
		}
	}		
}
