package com.sa.ibiz.demo.mob.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import lombok.Data;
import org.springframework.util.StringUtils;
import com.sa.ibiz.demo.ibizutil.errors.BadRequestAlertException;
import org.springframework.validation.annotation.Validated;
import org.springframework.http.ResponseEntity;
import com.sa.ibiz.demo.ibizutil.domain.ActionResult;
import com.sa.ibiz.demo.ibizutil.domain.TreeNode;
import com.sa.ibiz.demo.ibizutil.domain.TreeNodeFilter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;


@RestController
@RequestMapping("/mob/ctrl/usrtreeview")
public class UsrTreeViewController{

	/**
	 * 节点分隔符号
	 */
	public static final String TREENODE_SEPARATOR = ";";
	public static final String TREENODE_YPLX = "yplx";
	public static final String TREENODE_ROOT = "ROOT";


	@GetMapping(value="getnodes")
	public ResponseEntity<List<TreeNode>> getNodes(@Validated String srfparentkey,String srfcat,String srfnodeid,String srfnodefilter){
			List<TreeNode> list = new ArrayList<TreeNode>();
			TreeNodeFilter filter = new TreeNodeFilter();
			if (!StringUtils.hasLength(srfnodeid) || srfnodeid.equals("#")) {
				srfnodeid = TREENODE_ROOT;
			}
			String strTreeNodeId = srfnodeid;
			String strRealNodeId = "";
			boolean bRootSelect = false;
			String strNodeType = null;
			// String strRootSelectNode = this.getWebContext().getPostValue("srfnodeselect");
			String strRootSelectNode = "";// this.getWebContext().getPostValue("srfnodeselect");
			if (strTreeNodeId.equals(TREENODE_ROOT)) {
				strNodeType = TREENODE_ROOT;
				//treeNode = this.getTreeModel().getRootTreeNodeModel();
				//strRealNodeId = treeNodeFetchContext.getCatalog();
				//bRootSelect = this.getTreeModel().isEnableRootSelect();
			} else {
				int nPos = strTreeNodeId.indexOf(TREENODE_SEPARATOR);
				if (nPos == -1) {
					throw new BadRequestAlertException("树节点[%1$s]标识无效","","");
				}

				strNodeType = strTreeNodeId.substring(0, nPos);
				strRealNodeId = strTreeNodeId.substring(nPos + 1);
			}
			filter.setSrfParentKey(srfparentkey);
			filter.setSrfCat(srfcat);
			filter.setSrfNodeFilter(srfnodefilter);
			filter.setRealNodeId(strRealNodeId);
			filter.setSrfNodeId(srfnodeid);
			filter.setNodeType(strNodeType);
			/**
			 * 分解节点标识
			 */
			String[] nodeid = strRealNodeId.split(TREENODE_SEPARATOR);
			for (int i = 0; i < nodeid.length; i++) {
				switch(i){
				case 0:
					filter.setNodeId(nodeid[0]);
					break;
				case 1:
					filter.setNodeId2(nodeid[1]);
					break;
				case 2:
					filter.setNodeId3(nodeid[2]);
					break;
				case 3:
					filter.setNodeId4(nodeid[3]);
					break;
				default:
					break;
				}
			}
			if(strNodeType.equals(TREENODE_YPLX)){
				fillYplxNodeChilds(filter,list);
				return ResponseEntity.ok().body(list);
			}
			if(strNodeType.equals(TREENODE_ROOT)){
				fillRootNodeChilds(filter,list);
				return ResponseEntity.ok().body(list);
			}
				return ResponseEntity.ok().body(list);
	}

	/**
	* 填充 树视图节点[用品类型]
	*/
	protected void fillYplxNodes(@Validated TreeNodeFilter filter,List<TreeNode> list) {
	}
	
	/**
	* 填充 树视图节点[用品类型]子节点
	*/  
	protected void fillYplxNodeChilds(@Validated TreeNodeFilter filter,List<TreeNode> list) {
	}
	
	/**
	* 填充 树视图节点[默认根节点]
	*/
	protected void fillRootNodes(@Validated TreeNodeFilter filter,List<TreeNode> list) {
		TreeNode treeNode = new TreeNode();
		treeNode.setText("默认根节点");
		treeNode.setSrfmajortext("默认根节点");
		String strNodeId = "ROOT";
		treeNode.setSrfkey("root");
		strNodeId += TREENODE_SEPARATOR;
		strNodeId += "root";
		treeNode.setId(strNodeId);
		treeNode.setExpanded(filter.isAutoExpand());
		treeNode.setLeaf(false);
		list.add(treeNode);
	}
	
	/**
	* 填充 树视图节点[默认根节点]子节点
	*/  
	protected void fillRootNodeChilds(@Validated TreeNodeFilter filter,List<TreeNode> list) {
		//填充用品类型
		fillYplxNodes(filter,list);
	}
	
}