package com.acme.main.client;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.AdvancedCriteria;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.OperatorId;
import com.smartgwt.client.types.TreeModelType;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeNode;

/**
 * Entry point for the showcase.
 * 
 */
public class Main implements EntryPoint {

    private final static Logger logger = Logger.getLogger(EntryPoint.class.getName());

    private int currId = 1;

    @Override
    public void onModuleLoad() {


        TreeNode[] data = new TreeNode[4];
        
        TreeNode p1 = new TreeNode();
        p1.setAttribute("objCode", "1");
        p1.setAttribute("type", "N");
        p1.setAttribute("id", 1);
        
        TreeNode p2 = new TreeNode();
        p2.setAttribute("objCode", "1.1");
        p2.setAttribute("type", "N");
        p2.setAttribute("id", 2);
        p2.setAttribute("idNode", 1);
        
        TreeNode p3 = new TreeNode();
        p3.setAttribute("objCode", "1.2");
        p3.setAttribute("id", 3);
        p3.setAttribute("idNode", 1);
        p3.setAttribute("type", "N");
        
        TreeNode p4 = new TreeNode();
        p4.setAttribute("objCode", "1.1.1");
        p4.setAttribute("id", 4);
        p4.setAttribute("idNode", 2);
        p4.setAttribute("type", "T");
           
        
        Tree tree = new Tree();
        tree.setModelType(TreeModelType.PARENT);
        tree.setParentIdField("idNode");
        
        tree.setData(new TreeNode[]{p1});
//        tree.setRoot(p1);
        tree.add(p2, p1, 0);
        tree.add(p3, p1, 1);
        tree.add(p4, p2, 0);
                
        AdvancedCriteria adCriteria = new AdvancedCriteria("type", OperatorId.NOT_EQUAL, "T");
        Record[] filterdRecords = tree.findAll(adCriteria);
        StringBuilder builder = new StringBuilder();
        
        for(Record filterdRecord : filterdRecords) {
            builder.append("id: ").append(filterdRecord.getAttribute("id")).append(" type: ").append(filterdRecord.getAttribute("type")).append(" ");
        }
        logger.log(Level.INFO, builder.toString());
        
        TreeGrid treeGrid = new TreeGrid();
        ListGridField field = new ListGridField("id");
        treeGrid.setFields(field);
        treeGrid.setData(tree);
        
        final HLayout layout = new HLayout();
        layout.addMember(treeGrid);
        layout.setWidth100();
        layout.setHeight(300);
        layout.show();

    }
}
