package com.mars.helios.heliosweb.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.events.DoubleClickEvent;
import com.smartgwt.client.widgets.events.DoubleClickHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
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 java.util.logging.Logger;

/**
 * Entry point classes define
 * <code>onModuleLoad()</code>.
 */
public class HeliosWeb implements EntryPoint {

    private Logger logger = Logger.getLogger(HeliosWeb.class.getName());
    private final HeliosGinjector injector = GWT.create(HeliosGinjector.class);

    /**
     * This is the entry point method.
     */
    @Override
    public void onModuleLoad() {
        GWT.setUncaughtExceptionHandler(new HeliosUncaughtExceptionHandler());
        logger.info("Starting sandbox setup");

        TreeGrid treeGrid = new TreeGrid();
        treeGrid.setWidth(300);
        treeGrid.setHeight(400);
        treeGrid.setSelectionType(SelectionStyle.MULTIPLE);

        TreeGridField field = new TreeGridField("Name", "Tree from local data");
        field.setCanSort(false);

        treeGrid.setFields(field);

        final Tree tree = new Tree();
        tree.setModelType(TreeModelType.PARENT);
        tree.setNameProperty("Name");
        tree.setIdField("EmployeeId");
        tree.setParentIdField("ReportsTo");
        tree.setShowRoot(true);

        EmployeeTreeNode root = new EmployeeTreeNode("4", "1", "Charles Madigen");
        EmployeeTreeNode node2 = new EmployeeTreeNode("188", "4", "Rogine Leger");
        EmployeeTreeNode node3 = new EmployeeTreeNode("189", "4", "Gene Porter");
        EmployeeTreeNode node4 = new EmployeeTreeNode("265", "189", "Olivier Doucet");
        EmployeeTreeNode node5 = new EmployeeTreeNode("264", "189", "Cheryl Pearson");
        tree.setData(new TreeNode[]{root, node2, node3, node4, node5});

        treeGrid.addDrawHandler(new DrawHandler() {
            public void onDraw(DrawEvent event) {
                tree.openAll();
            }
        });

        treeGrid.addDoubleClickHandler(new DoubleClickHandler() {
            @Override
            public void onDoubleClick(DoubleClickEvent event) {
                logger.info("Double click !");
            }
        });

        treeGrid.setData(tree);


        VLayout layout = new VLayout();
        layout.addMember(treeGrid);
        layout.draw();
        logger.info("Setup done !");
    }

    public static class EmployeeTreeNode extends TreeNode {

        public EmployeeTreeNode(String employeeId, String reportsTo, String name) {
            setEmployeeId(employeeId);
            setReportsTo(reportsTo);
            setName(name);
        }

        public void setEmployeeId(String value) {
            setAttribute("EmployeeId", value);
        }

        public void setReportsTo(String value) {
            setAttribute("ReportsTo", value);
        }

        public void setName(String name) {
            setAttribute("Name", name);
        }
    }
}
