package com.ht.data;

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

import javafx.scene.control.TitledPane;
import javafx.scene.control.TreeView;

import com.ht.tags.model.index.IndexTreeNode;
import com.ht.tags.model.index.IndexType;
import com.ht.tags.model.tag.TagVar;
import com.ht.ui.energy.util.EnergyConst;
import com.ht.web.hessian.HessianFactoryManger;
import com.ht.web.remoting.HessianResponse;

public enum TagInfoManager {
	INSTANCE;
	
	/* 变量名称相关 */
	private Map<String, TagVar> tagVarNameMap;
	
	/* 索引标签相关 */
	private List<IndexTreeNode> indexTreeList;
	private Map<String, IndexTreeNode> energyIndexMap = new HashMap<String, IndexTreeNode>();
	private Map<String, IndexTreeNode> normalIndexMap = new HashMap<String, IndexTreeNode>();
	private IndexTreeNode powerIndexNode;
	private IndexTreeNode buildingIndexNode;
	
	public Map<String, TagVar> getTagVarNameMap() {
		if (tagVarNameMap == null) {
			try {
				HessianResponse resp = HessianFactoryManger.INSTANCE
						.getTagService().getAllTagVar();
				if (resp.getData() != null) {
					tagVarNameMap = (Map<String, TagVar>) resp.getData();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}	
		}
		return tagVarNameMap;
	}
	
	public List<IndexTreeNode> getIndexTreeList() {
		if (indexTreeList == null) {
			try {
				HessianResponse resp = HessianFactoryManger.INSTANCE.getTagService().getNormalAndEnergyIndex();
				if (resp.isSuccessful())
					indexTreeList = (List<IndexTreeNode>) resp.getData();
				if (indexTreeList != null) {
					for (IndexTreeNode root : indexTreeList) {
						if (root.getIndexType() == IndexType.ENERGY) {

							for (IndexTreeNode e : root.getChildren()) {
								if (e.getType().equals(EnergyConst.POWER_TYPE)) {
									powerIndexNode = e;
								}
								energyIndexMap.put(e.getName(), e);

							}

						} else if (root.getIndexType() == IndexType.NORMAL) {// 常规索引
							for (IndexTreeNode e : root.getChildren()) {
								if (e.getType().equals(EnergyConst.BUILDING_TYPE)) {
									buildingIndexNode = e;
								}
								normalIndexMap.put(e.getName(), e);
							}

						}

					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return indexTreeList;
	}
	
	public Map<String, IndexTreeNode> getEnergyIndexMap() {
		return energyIndexMap;
	}

	public Map<String, IndexTreeNode> getNormalIndexMap() {
		return normalIndexMap;
	}

	public IndexTreeNode getPowerIndexNode() {
		return powerIndexNode;
	}

	public IndexTreeNode getBuildingIndexNode() {
		return buildingIndexNode;
	}
}
