package com.bug;

import java.lang.reflect.Array;
import java.util.Date;

import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.calendar.*;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;
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.RelativeDate;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TimeUnit;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.DateUtil;
import com.google.gwt.core.client.EntryPoint;

public class TestBug implements EntryPoint {

    public void onModuleLoad() {
        VLayout layout = new VLayout();
        layout.setWidth("100%");
        layout.setHeight("100%");

    	HLayout treeGridsLayout = new HLayout();
        treeGridsLayout.setWidth("100%");
        treeGridsLayout.setHeight("*");

        TreeGridField titleField = new TreeGridField("title", "title");
        titleField.setCanFilter(true);

        final TreeGrid courseTreeGrid = new TreeGrid();
        courseTreeGrid.setWidth("50%");
        courseTreeGrid.setHeight("100%");
        courseTreeGrid.setCanDrag(false);
        courseTreeGrid.setSelectionType(SelectionStyle.SINGLE);
        courseTreeGrid.setShowHeader(false);
        courseTreeGrid.setLoadDataOnDemand(false);
        courseTreeGrid.setCanEdit(false);
        courseTreeGrid.setShowFilterEditor(false);
        courseTreeGrid.setFields(titleField);
        courseTreeGrid.setAlternateRecordStyles(true);
        courseTreeGrid.setIconSize(16);
        courseTreeGrid.setAutoFetchData(true);
        courseTreeGrid.setDataSource(getDataSource());

    	ListGrid tasksLineProps = new ListGrid();
    	tasksLineProps.setSelectionType(SelectionStyle.SINGLE);
    	tasksLineProps.setShowSelectedStyle(true);

    	tasksLineProps.addSelectionChangedHandler(new SelectionChangedHandler() {
			@Override
			public void onSelectionChanged(SelectionEvent event) {
				ListGridRecord item = event.getRecord();
				boolean state = event.getState();
			}
		});

    	Date startDate = new Date(116, 5, 2);
    	startDate = DateUtil.getAbsoluteDate(new RelativeDate("-1w"), startDate);
    	
    	final Timeline tasksLine = new Timeline();
    	tasksLine.setWidth("50%");
    	tasksLine.setHeight("100%");  
    	tasksLine.setStartDate(startDate);  
    	tasksLine.setEndDate(new Date(116, 5, 22));  
    	tasksLine.setCanEditLane(true);  
    	tasksLine.setShowEventDescriptions(false);  
    	tasksLine.setAutoChildProperties("timelineView", tasksLineProps);

        HeaderLevel[] headerLevels = new HeaderLevel[]{  
            new HeaderLevel(TimeUnit.WEEK),   
            new HeaderLevel(TimeUnit.DAY)  
        };  
        tasksLine.setHeaderLevels(headerLevels);  
        tasksLine.setLaneFields(new ListGridField[]{ new ListGridField("title", "title", 120)});  
  
        treeGridsLayout.addMember(courseTreeGrid);
        treeGridsLayout.addMember(tasksLine);

        Button button = new Button("set lanes");
        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
            	Tree projectTree = courseTreeGrid.getData();
           		TreeNode courseItemNode = projectTree.getRoot();
            	Record[] records = projectTree.getAllNodes(courseItemNode);
            	Lane[] tasksLanes = castRecordsToLanes(records);
            	projectTree.openAll();
                tasksLine.setLanes(tasksLanes);
            }
        });

        layout.addMember(button);
        layout.addMember(treeGridsLayout);
        layout.draw();

       
    }

    public DataSource getDataSource() {
        DataSourceTextField idField = new DataSourceTextField("@id");
        idField.setPrimaryKey(true);
        idField.setRequired(true);
        idField.setHidden(true);

        DataSourceTextField pidField = new DataSourceTextField("@pid");
        pidField.setRequired(true);
        pidField.setHidden(true);

        DataSourceField childrenData = new DataSourceField("children", FieldType.ANY);
        childrenData.setChildrenProperty(true);
        childrenData.setHidden(true);

        DataSourceTextField titleField = new DataSourceTextField("title");

        String url  = "projectTree.json";
        
    	DataSource dataSource = new DataSource() {
            @Override
            protected void transformResponse(DSResponse response, DSRequest request, Object data) {
                Record[] childs = response.getData();
                setProjectNodesProperties(childs);
                super.transformResponse(response, request, data);
            }
        };
        dataSource.setID("projectTree");
        dataSource.setFields(idField, pidField, childrenData, titleField);
        dataSource.setChildrenField("children");
        dataSource.setClientOnly(true);
        dataSource.setDataURL(url);
        dataSource.fetchData(null, new DSCallback() {
			@Override
			public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
			}
		});
        return dataSource;
    }

    public static void setProjectNodesProperties(Record[] records) {
        for (Record record : records) {
          	record.setAttribute("name", record.getAttribute("@id"));
        	record.setAttribute("icon", record.getAttribute("node") + ".png");
            Record[] childs = record.getAttributeAsRecordArray("children");
            if(childs != null && childs.length > 0) {
            	setProjectNodesProperties(childs);
            } else {
            	record.setAttribute("isFolder", false);
            }
        }
    }

    public static Lane[] castRecordsToLanes(Record[] srcArray) {
    	Lane[] retArray = new Lane[] {};
    	for (int i = 0; i < srcArray.length; i++) {
    		Array.set(retArray, i, new Lane(srcArray[i].getJsObj()));
    	}
    	return retArray;
    };
}
