package com.dev.swms.client.ui.page;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Position;
import com.gwtext.client.data.Node;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.tree.TreeNode;
import com.gwtext.client.widgets.tree.event.TreeNodeListenerAdapter;
import com.dev.energy.client.rpc.DefineRPCAdapter;
import com.dev.energy.client.utils.StringUtils;

/**
 * 
 * @author  dev
 * @description 抽象边框布局西边面板
 * 
 */

public class AbstractWestPanel extends Panel {
	protected BrowseRulePage mainPage;
	
	protected WestTreePanel treePanel;
	
	protected TabPanel tabPanel;
	
	protected Node currentNode;
	
	protected boolean isExpand = false;
	
	private String managerName;
	
	
	/**
	 * 左边树上的Tab标题
	 */
	protected String westTitle = "";
	
	public AbstractWestPanel(String title,BrowsPutRackRulePage browsPutRackRulePage,String managerName){
		this.westTitle = title;
		this.mainPage = browsPutRackRulePage;
		this.managerName = managerName;
		init();
		draw();
	}
	
	public AbstractWestPanel(String title,BrowsPickItemRulePage browsPickItemRulePage,String managerName){
		this.westTitle = title;
		this.mainPage = browsPickItemRulePage;
		this.managerName = managerName;
		init();
		draw();
	}
	
	
	protected void init() {
		setTitle(westTitle);
		setLayout(new FitLayout());
		setWidth(210);
		setCollapsible(false);
		setHideCollapseTool(true);

		tabPanel = new TabPanel();
		tabPanel.setActiveTab(0);
		tabPanel.setDeferredRender(true);
		tabPanel.setTabPosition(Position.TOP);
		treePanel = new WestTreePanel("");
	}
	protected void draw() {
//		tabPanel.add(treePanel);
		add(treePanel);
		rpcInitTree(null);
		
	}
	
	/**
	 * 异步触发选择组织下拉列表,获得组织下所有客户,承运商
	 * @param params
	 */
	protected void rpcInitTree(Map params){
		DefineRPCAdapter rcp = new DefineRPCAdapter() {
			public void successProcessor(Map map) {
				List items = (List) map.get("result");
				appendChildRoot(treePanel.getRoot(),items,"rule_node_root.gif");
			}
		};
		rcp.ajaxRpcCallBackExec(this.managerName,
				"getPutRackRuleByWareHouseId",
				params);
	}
	
	/**
	 * 往传入节点中追加子节点
	 * @param rootNode
	 * @param items
	 * @param icon
	 */
	
	protected void appendChildRoot(Node rootNode,List items,final String icon){
		
		//如果当前规则列表为空，则构造一个临时对象用于打开右边规则显示面板，方便用户新增操作
		if(items.size() <= 0) {
			TreeNode node = new TreeNode();
			node.setUserObject(new CustomEntity());
			addNodeClickListener(node);
			onNodeClick(node,null);
			return;
		}
		for(Iterator it = items.iterator();it.hasNext();){
			CustomEntity entity = (CustomEntity)it.next();
			TreeNode node = createTreeNode(entity,icon);
			rootNode.appendChild(node);
			if(((CustomEntity)node.getUserObject()).getType()!=2){
				node.appendChild(createDefaultTreeNode());
				
				//构造一个临时对象用于打开右边规则显示面板，方便用户新增操作
				TreeNode temp = new TreeNode();
				temp.setUserObject(new CustomEntity());
				onNodeClick(temp,null);
			}
			
		}
	}
	
	/**
	 * 清理传入节点
	 * @param root
	 */
	protected void clearRoot(Node root){
		Node[] node = root.getChildNodes();
		for (int i = 0; i < node.length; i++) {
			root.removeChild(node[i]);
		}
	}
	
	protected void addNodeClickListener(TreeNode node) {
        node.addListener(new TreeNodeListenerAdapter() {
        		public void onExpand(Node node) {
        			onNodeExpand(node);
        		}
             	public void onClick(Node node, EventObject e) {
             		onNodeClick(node,e);
             	}
             	public void onCollapse(Node node) {
             		collapseNode(node);
             		clearRoot(node);
             		node.appendChild(createDefaultTreeNode());
                }
        });
    }
	
	/**
	 * 点击节点
	 * @param node
	 * @param e
	 */
	protected void onNodeClick(final Node node, final EventObject e){
		currentNode = node;
		final Object userObject = node.getUserObject();
		mainPage.displayRule((CustomEntity)userObject);
	}
	
	
	/**
	 * 展开节点
	 * @param node
	 */
	protected void onNodeExpand(Node node){
		currentNode = node;
		if(refreshParent){
			refreshParent = false;
			return;
		}
		if(!((TreeNode)node).isExpandable())
			refreshCurrentNode(node,false);
		else
			node.removeChild(node.getFirstChild());
	}
	
	/**
	 *刷新当前节点 
	 *
	 */
	public void refreshCurrentNode(){
		refreshCurrentNode(currentNode,true);
	}
	boolean refreshParent = false;
	/**
	 * 刷新当前父节点
	 *
	 */
	public void refreshParentNode(){
		Node node = currentNode.getParentNode();
		if(node.getUserObject() == null){
			node = currentNode;
		}
		currentNode = node;
		((TreeNode)currentNode).setExpandable(true);
		clearRoot(currentNode);
		currentNode.appendChild(createDefaultTreeNode());
		refreshParent = true;
		refreshCurrentNode(currentNode,true);
	}
	
	public void refreshCurrentNode(Node refreshNode){
		currentNode = refreshNode;
		refreshCurrentNode(currentNode,true);
	}
	
	/**
	 * 创建默认,查询中节点
	 * @return
	 */
	protected Node createDefaultTreeNode(){
		TreeNode defaltNode = new TreeNode("查找");
		defaltNode.setId("searching");
		defaltNode.setIcon("images/silk/treeLoading.gif");
		defaltNode.setDisabled(true);
		return defaltNode;
	}
	
	protected Node createNoFindChildNode(String text){
		TreeNode defaltNode = new TreeNode(text);
		defaltNode.setDisabled(true);
		return defaltNode;
	}
	
	/**
	 * 创建树节点
	 * @param userObject
	 * @param icon
	 * @return
	 */
	protected TreeNode createTreeNode(final CustomEntity userObject,final String icon){
		TreeNode node = new TreeNode();
		node.setId(userObject.getId() + userObject.getName() + userObject.getRandomNum());
		node.setText(userObject.getName());
		node.setUserObject(userObject);
		
		if(!StringUtils.isEmpty(icon))
			node.setIcon("images/" + icon);
		addNodeClickListener(node);
		return node;
	}
	
	protected void setNodeText(Node node,String text){
		((TreeNode)node).setText(text);
	}
	
	/**
	 * 展开传入节点,并设置展开属性
	 * @param node
	 */
	protected void expandNode(Node node){
		((TreeNode)node).setExpandable(true);
		((TreeNode)node).expand();
	}
	
	/**
	 * 收缩传入节点
	 * @param node
	 */
	protected void collapseNode(Node node){
		((TreeNode)node).setExpandable(false);
		((TreeNode)node).collapse();
	}
	
	public Node getCurrentNode() {
		return currentNode;
	}

	public void setCurrentNode(Node currentNode) {
		this.currentNode = currentNode;
	}
	
	/**
	 * 展开客户,承运商树节点,回调后添加子节点
	 * @param node
	 * @param map
	 */
	private void expandCompanyNode(final Node node,Map map){
		clearRoot(node);
		expandNode(node);
		appendChildRoot(node,(List)map.get("billTypes"),"rule_node_child.gif");
	}
	
	/**
	 * 展开客户节点,异步获得子节点
	 * @param node
	 * @param params
	 * @param showMsg
	 */
	private void rpcCompanyNode(final Node node,Map params,final boolean showMsg){
		DefineRPCAdapter rcp = new DefineRPCAdapter() {
			public void successProcessor(Map map) {
				expandCompanyNode(node,map);
			}
		};
		rcp.rpcCallBackExec(this.managerName,
				"getBillTypeByCompanyId",
				params,showMsg);
	}
	
	/**
	 * 刷新传入树节点
	 * @param node
	 * @param showMsg
	 */
	protected void refreshCurrentNode(final Node node,final boolean showMsg){
		final Object userObject = node.getUserObject();
		Map params = new HashMap();
		params.put("id", ((CustomEntity)userObject).getId());
        if(((CustomEntity)userObject).getType()==1){//如果点击的节点是客户
        	rpcCompanyNode(node,params,showMsg);
        }
	}
}


