/**
 * @author tyl
 * @Action  ini
 * 2011-3-22
 */
package cn.com.origin.autocodemanager.imports;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import jxl.read.biff.BiffException;
import cn.com.origin.autocodemanager.common.AbstractTreeViewPart;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.operations.ExcelOperation;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.TreeViewOperation;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;

public class ImportNodeOperation {

	private TreeViewOperation treeViewOperation;
	// private TCSession session;
	private AbstractTreeViewPart viewPart;
	private AbstractTreeData treeData;
	private String filePath;
	TCComponent ruleComponent = null;

	public ImportNodeOperation(AbstractTreeViewPart viewPart, AbstractTreeData treeData, String filePath) {
		this.viewPart = viewPart;
		this.treeData = treeData;
		this.filePath = filePath;
		initParams();
	}

	public void initParams() {
		// this.treeAndListCodeOperation = new TreeAndListCodeOperation();
		this.treeViewOperation = new TreeViewOperation();
		// session = (TCSession) AIFUtility.getCurrentApplication().getSession();
	}

	public void importNode() {
		try {
			importTreeNode(treeData, filePath);
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private ArrayList<HashMap<String, Object>> getImportFromExcel(String filePath) throws BiffException, IOException {
		return ExcelOperation.readExcel(new File(filePath), 1, AutoCodeManagerPropertyName.TreeAndListCodeNode_Titles);
	}

	public void importTreeNode(AbstractTreeData parentTreeData, String filePath) throws TCException, BiffException, IOException {
		HashMap<String, Object> levelMap = new HashMap<String, Object>();
		ArrayList<HashMap<String, Object>> arrayList = getImportFromExcel(filePath);
		// System.out.println("arrayList:" + arrayList);
		if (arrayList != null && arrayList.size() > 0) {
			HashMap<String, Object> hashMap = (HashMap<String, Object>) arrayList.get(0);
			int levelIndex = 0;
			String level = (String) hashMap.get("层号");
			String name = (String) hashMap.get("名称");
			try {
				levelIndex = Integer.parseInt(level);
			} catch (NumberFormatException e) {
				MessageBox.post(name + "层号填写有误!", "INFO", MessageBox.WARNING);
				e.printStackTrace();
				return;
			}
			String parentLevel = String.valueOf(levelIndex - 1);
			levelMap.put(parentLevel, parentTreeData);

			for (int i = 0; i < arrayList.size(); i++) {
				hashMap = (HashMap<String, Object>) arrayList.get(i);
				level = (String) hashMap.get("层号");
				name = (String) hashMap.get("名称");
				String type = (String) hashMap.get("类型");
				try {
					levelIndex = Integer.parseInt(level);
				} catch (NumberFormatException e) {
					MessageBox.post(name + "层号填写有误!", "INFO", MessageBox.WARNING);
					e.printStackTrace();
					return;
				}
				parentLevel = String.valueOf(levelIndex - 1);
				parentTreeData = (AbstractTreeData) levelMap.get(parentLevel);
				if (parentTreeData == null) {
					MessageBox.post("检查" + name + "层号填写是否有误!", "INFO", MessageBox.WARNING);
					return;
				}
				HashMap<String, String> newHashMap = new HashMap<String, String>();
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_index, "");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name, name);
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_value, (String) hashMap.get("码值"));
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type, type);
				if ((type.equalsIgnoreCase("tree")) || (type.equalsIgnoreCase("list"))) {
					newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root, "true");
				} else {
					newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root, "false");
				}
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_style, (String) hashMap.get("编码样式"));
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_code_length, (String) hashMap.get("编码长度"));
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_rule_node, "");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_reference_nodes, "");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_desc, (String) hashMap.get("备注"));
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, "");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, "");
				String isDefault = (String) hashMap.get("是否为默认值");
				if ((isDefault != null) && ((isDefault.equalsIgnoreCase("false")) || (isDefault.equalsIgnoreCase("true")))) {
					newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, (String) hashMap.get("是否为默认值"));
				} else {
					newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, "false");
				}

				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "false");
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_begin_value, (String) hashMap.get("流水码初始值"));
				newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_max_value, (String) hashMap.get("流水码最大值"));
				String maxValue = (String) hashMap.get("流水码最大值");
				if ((maxValue != null) && (!maxValue.equalsIgnoreCase("0")) && (maxValue.trim().length() > 0)) {
					newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, String.valueOf(maxValue.trim().length()));
				} else {
					newHashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_seq_length, "0");
				}
				if (newHashMap.get(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root).equalsIgnoreCase("true")) {
					ruleComponent = null;
				}
				if (newHashMap.get(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_type).equalsIgnoreCase(AutoCodeManagerPropertyName.CodeGroupName)) {
					ruleComponent = null;
				}

				TCComponent newComponent = new TreeAndListCodeOperation().createNewTreeORListNode(ruleComponent, parentTreeData, newHashMap);
				if (newComponent == null) {
					return;
				}
				if ((newHashMap.get(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root) != null) && (newHashMap.get(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_root)).equalsIgnoreCase("true")) {
					ruleComponent = newComponent;
				}
				TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
				
				PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(newComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
				AbstractTreeData treeData = new AbstractTreeData(parentTreeData, packCodeNodeInfo);

				treeViewOperation.addChildrenRelations(parentTreeData, treeData);
				viewPart.createView.treeViewer.refresh(parentTreeData);
				levelMap.put(level, treeData);
			}
		}
	}
}
