package com.smartgwt.sample.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;  
import java.util.Set;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemCriterionGetter;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.RelativeDateItem;
import com.smartgwt.client.widgets.form.fields.RichTextItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;


import java.util.Date;

import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.Criterion;
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.Record;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.data.RelativeDate;
import com.smartgwt.client.types.Alignment;  
import com.smartgwt.client.types.AutoFitWidthApproach;
import com.smartgwt.client.types.AutoSelectionModel;
import com.smartgwt.client.types.Autofit;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.EmbeddedPosition;
import com.smartgwt.client.types.ListGridComponent;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.RecordComponentPoolingMode;
import com.smartgwt.client.types.RelativeDateRangePosition;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.types.TimeUnit;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.DateUtil;
import com.smartgwt.client.util.JSOHelper;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.JSONEncoder;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;  
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.TransferImgButton;
import com.smartgwt.client.widgets.cube.CubeGrid;
import com.smartgwt.client.widgets.cube.Facet;
import com.smartgwt.client.widgets.cube.FacetValue;
import com.smartgwt.client.widgets.cube.FacetValueMap;
import com.smartgwt.client.widgets.events.ClickEvent;  
import com.smartgwt.client.widgets.events.ClickHandler;  
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.events.VisibilityChangedEvent;
import com.smartgwt.client.widgets.events.VisibilityChangedHandler;
import com.smartgwt.client.widgets.grid.ListGrid;  
import com.smartgwt.client.widgets.grid.ListGridEditorContext;
import com.smartgwt.client.widgets.grid.ListGridEditorCustomizer;
import com.smartgwt.client.widgets.grid.ListGridField;  
  
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.Timer;  
  

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class BuiltInDS implements EntryPoint {
	
	public static native void logToSC(String msg) /*-{
	$wnd.isc.logWarn(msg);
}-*/;

	public void onModuleLoad() {
		

	  IButton button = new IButton("Open Popup Window");
	  button.setMargin(10);
	  button.setWidth(300);
	  button.setHeight(80);
	  Canvas canvas = new Canvas();
	  canvas.setWidth100();
	  canvas.setHeight100();
      
      String json = JsonExampleB.getJson();
      
      List<String> list = (List<String>)JSOHelper.convertToList(JSON.decode(json));
      
      List<TreeNode> nodes = new ArrayList<TreeNode>();
      
      for (String str : list) {
    	  Map<String, Object> map = (Map<String, Object>)JSOHelper.convertToMap(JSON.decode(str));
    	  TreeNode node = new TreeNode();
    	  for (String key : map.keySet()) {
    		  if (key.contains("children")) {
    			  continue;
    		  }
    		  node.setAttribute(key, map.get(key));
    	  }

    	  nodes.add(node);
      }
      
      TreeNode[] nodeArr = new TreeNode[nodes.size()];
      nodeArr = nodes.toArray(nodeArr);
      
      TreeGrid drvDetailGrid = new TreeGrid();
      drvDetailGrid.setDataSource(DataSource.get("ChipSTA_ChipDRVDetails"));
      Tree tree = new Tree();
      tree.setIdField("Level_Id");
      tree.setParentIdField("Parent_Id");
      tree.linkNodes(nodeArr);
      
      drvDetailGrid.setData(tree);
      drvDetailGrid.setAutoFitData(Autofit.BOTH);
      drvDetailGrid.setWidth100();
      drvDetailGrid.setHeight100();
      
      
      
      // our options are set below - still slow when these are disabled
		drvDetailGrid.setHeaderSpans(JsonExample.getHeaderSpans());
		drvDetailGrid.setUseAllDataSourceFields(true);

		drvDetailGrid.setVirtualScrolling(false);
		drvDetailGrid.setCanExpandRecords(false);
		drvDetailGrid.setShowAllRecords(true);
		drvDetailGrid.setShowRecordComponents(false);
		drvDetailGrid.setShowRecordComponentsByCell(false);
		drvDetailGrid.setAnimateFolderSpeed(6000);
		drvDetailGrid.setTreeRootValue("-1");
		drvDetailGrid.setFolderIcon(null);
		drvDetailGrid.setCanFreezeFields(false);
		drvDetailGrid.setNodeIcon(null);
		
		drvDetailGrid.setUseAllDataSourceFields(true);
		drvDetailGrid.setWidth100();
		drvDetailGrid.setAutoFitFieldWidths(false);
		drvDetailGrid.setAutoFitFieldsFillViewport(true);
		drvDetailGrid.setMinFieldWidth(30);
		drvDetailGrid.setAutoFitWidthApproach(AutoFitWidthApproach.BOTH);
		drvDetailGrid.setShowGridSummary(true);
		
		drvDetailGrid.setAutoSizeHeaderSpans(true);

		drvDetailGrid.setBodyOverflow(Overflow.HIDDEN);
		drvDetailGrid.setOverflow(Overflow.HIDDEN);
		drvDetailGrid.setCanFocus(false);
		drvDetailGrid.setCanHover(true);
		drvDetailGrid.setCanGroupBy(false);
		drvDetailGrid.setCanMultiGroup(false);
		drvDetailGrid.setRecordComponentPosition(EmbeddedPosition.WITHIN);
		drvDetailGrid.setLeaveHeaderMenuButtonSpace(false);
		drvDetailGrid.setRecordComponentPoolingMode(RecordComponentPoolingMode.DATA);
		drvDetailGrid.setAutoSizeHeaderSpans(true);
		drvDetailGrid.setCellPadding(8);

		
	  canvas.addChild(drvDetailGrid);

      canvas.draw();  
      
	}
	

}
