/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */
package com.icesoft.faces.facelets.example.treetable;

import com.icesoft.faces.application.showcase.model.entity.Employee;
import com.icesoft.faces.application.showcase.model.service.EmployeeService;
import com.icesoft.faces.application.showcase.view.bean.examples.component.dataTable.DataTableBase;
import com.icesoft.faces.facelets.component.treetable.ITreeTableListener;
import com.icesoft.faces.facelets.component.treetable.TreeTable;
import com.icesoft.faces.facelets.component.treetable.TreeTableNode;

import java.io.Serializable;
import java.util.*;


/**
 * <p>The TreeBean class is the backing bean for the Tree Component showcase
 * demonstration. It is used to store and display the selected tree node</p>
 *
 * @see TreeTableUserObject
 * @since 0.3.0
 */
public class TreeTableExampleBean extends DataTableBase {

    private TreeTable treeTable;

    public void setEmployeeService(EmployeeService employeeService) {
        super.setEmployeeService(employeeService);
        initializeTreeTable();
    }
    
    public class StringContainer implements Serializable{
    	
    	private String str;
    	
    	public StringContainer(String str){
    		this.str = str;
    	}
    	
    	public String toString(){
    		return str;
    	}
    	
    	public String getClazz(){
    		return "String";
    	}
    }

    private void initializeTreeTable() {
        treeTable = new TreeTable(new MyTreeTableListener());
        TreeTableNode rootTreeNode = new TreeTableNode(treeTable, new StringContainer("Acme Ltd."));
        rootTreeNode.setExpanded(true);
        Map<String, List<Employee>> subDepEmpMap = new HashMap<String, List<Employee>>();
        List<String> departments = employeeService.getDepartments();
        boolean firstDepOpened = false, firstSubDepOpened = false, firstEmpOpened = false;
        for (String dep : departments) {
            List<String> subDeps = employeeService.getSubDepartments(dep);
            if (subDeps != null) {
                for (String subDep : subDeps) {
                    subDepEmpMap.put(subDep, new ArrayList<Employee>());
                }
            }
        }
        for (Employee employee : this.getEmployees()) {
            subDepEmpMap.get(employee.getSubDepartmentName()).add(employee);
        }

        for (String dep : departments) {

            TreeTableNode departmentNode = new TreeTableNode(rootTreeNode, new StringContainer(dep));
            if (!firstDepOpened) {
                departmentNode.setExpanded(true);
                firstDepOpened = true;
            }

            for (String s : employeeService.getSubDepartments(dep)) {
                TreeTableNode subDepNode = new TreeTableNode(departmentNode,new StringContainer(s));
                if (!firstSubDepOpened) {
                    subDepNode.setExpanded(true);
                    firstSubDepOpened = true;
                }

                List<Employee> emps = subDepEmpMap.get(subDepNode.getData().toString());
                for (Employee emp : emps) {
                    TreeTableNode empNode = new TreeTableNode(subDepNode, emp);
                    if (!firstEmpOpened) {
                        empNode.setExpanded(true);
                        firstEmpOpened = true;
                    }

                }
            }
        }
        treeTable.setRootNode(rootTreeNode);
    }

    public TreeTable getTreeTable() {
        return treeTable;
    }

    class MyTreeTableListener implements ITreeTableListener {

        public boolean isNodeMoveAllowed(TreeTableNode parent,
                                         TreeTableNode child) {
            //only Employee nodes can be moved
            if (!(child.getData() instanceof Employee))
                return false;
            //dragged items can only be dropped onto 'Sub-departments'
            if (parent.getLevel() != 2)
                return false;
            else
                return true;
        }

    }
}
