package com.ht.ui.energy.sum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ht.data.TagInfoManager;
import com.ht.tags.model.index.IndexTreeNode;
import com.ht.tags.model.index.IndexType;
import com.ht.ui.energy.sum.TopPanel.CmpType;

abstract class IndexNodeManager {

	private static Logger log = LoggerFactory.getLogger(IndexNodeManager.class);

	IndexTreeNode energyIndexNode;
	Map<String, IndexTreeNode> normalIndexMap = new HashMap<String, IndexTreeNode>();
	LinkedList<IndexTreeNode> cmpCategoryNode = new LinkedList<IndexTreeNode>();

	abstract boolean isEnergyCmp();

	abstract void setCmpType(CmpType type);

	abstract void updateChart();

	abstract void newNormalIndexNodeAdded(IndexTreeNode indexNode);

	abstract void clearChartAndTableData();

	abstract void updateTableHeaderTitle();

	boolean isNormalIndexEmpty() {
		return normalIndexMap.isEmpty();
	}

	boolean isEnergyIndexEmpty() {
		return energyIndexNode == null;
	}

	boolean isCmpIndexEmpty() {
		return cmpCategoryNode.isEmpty();
	}

	public IndexTreeNode getEnergyIndexNode() {
		return energyIndexNode;
	}

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

	public LinkedList<IndexTreeNode> getCmpCategoryNode() {
		return cmpCategoryNode;
	}

	List<String> getCmpPathList() {
		if (cmpCategoryNode.isEmpty()) {
			return new ArrayList<String>(1);
		} else {
			List<String> list = new ArrayList<String>(cmpCategoryNode.size());
			for (IndexTreeNode node : cmpCategoryNode) {
				list.add(node.getPath());
			}
			return list;
		}
	}

	List<String> getNormalPathList() {
		if (normalIndexMap.isEmpty()) {
			return new ArrayList<String>(1);
		} else {
			List<String> list = new ArrayList<String>(normalIndexMap.size());
			for (IndexTreeNode node : normalIndexMap.values()) {
				list.add(node.getPath());
			}
			return list;
		}
	}
	
	boolean canDoAirAreaCmp() {
		for (IndexTreeNode node : cmpCategoryNode) {
			if (node.getAirConditionArea() == null) {
				return false;
			}
		}
		return true;
	}
	
	boolean canDoAreaCmp() {
		for (IndexTreeNode node : cmpCategoryNode) {
			if (node.getArea() == null) {
				return false;
			}
		}
		return true;
	}
	
	boolean canDoHeatingAreaCmp() {
		for (IndexTreeNode node : cmpCategoryNode) {
			if (node.getHeatingArea() == null) {
				return false;
			}
		}
		return true;
	}

	void doEnergyCmp() {
		cmpCategoryNode.clear();
		clearChartAndTableData();

		// 显示同级分项
		if (energyIndexNode.getParent() != null) {

			for (IndexTreeNode n : energyIndexNode.getParent().getChildren()) {
				addIndexPath(n);
			}
		} else {

			if (!energyIndexNode.getChildren().isEmpty()) {

				// 显示下级分项
				for (IndexTreeNode n : energyIndexNode.getChildren()) {

					addIndexPath(n);
				}
			}
		}
	}

	void doNormalCmp() {

		cmpCategoryNode.clear();
		clearChartAndTableData();

		addIndexPath(energyIndexNode);
	}
	
	void doCmpRefresh() {
		List<IndexTreeNode> l = new ArrayList<IndexTreeNode>(
				cmpCategoryNode);
		for (IndexTreeNode node : l) {
			cmpCategoryNode.remove(node);
			cmpCategoryNode.addLast(node);
			if (node.getIndexType().equals(IndexType.ENERGY)) {// 能耗分类分项
				energyIndexNode = node;
			} else {
				newNormalIndexNodeAdded(node);
			}
			updateChart();
		}
	}
	
	void clear() {
		energyIndexNode = null;
		// cmpCategoryNode = null;
		cmpCategoryNode.clear();
		normalIndexMap.clear();
	}

	void addIndexPath(final IndexTreeNode indexNode) {
		log.debug("添加分项: {}, {}", indexNode.getIndexName(), indexNode.getPath());

		if (indexNode.getIndexType().equals(IndexType.ENERGY)) {// 能耗分类分项
			this.energyIndexNode = indexNode;
			updateTableHeaderTitle();

			IndexTreeNode indexRoot = TagInfoManager.INSTANCE
					.getEnergyIndexMap().get(indexNode.getIndexName());
			if (!indexRoot.getChildren().isEmpty()) {// 带有能耗分项, 进行能耗分项对比

				if (cmpCategoryNode.isEmpty()) {// 未设置对比方式
					if (isEnergyCmp()) {// 采用能耗分项对比
						cmpCategoryNode.addLast(indexNode);
					} else {// 建筑分区对比方式
						// 未设置常规分类条件
						for (IndexTreeNode build : TagInfoManager.INSTANCE
								.getBuildingIndexNode().getChildren()) {
							addIndexPath(build);
						}
						return;
						// if (normalIndexMap.isEmpty()) {
						// } else {
						// }
					}
				} else if (cmpCategoryNode.getLast().getIndexType()
						.equals(IndexType.NORMAL)) {// 已设置了常规分类对比
					List<IndexTreeNode> l = new ArrayList<IndexTreeNode>(
							cmpCategoryNode);
					// 先清空当前数据
					cmpCategoryNode.clear();
					clearChartAndTableData();
					for (IndexTreeNode n : l) {
						addIndexPath(n);
					}
				} else if (!cmpCategoryNode.getLast().getIndexName()
						.equals(indexNode.getIndexName())) {// 已设置了其它能耗分类对比
					// 先清空当前数据
					cmpCategoryNode.clear();
					clearChartAndTableData();
					// 直接采用能耗分项对比
					cmpCategoryNode.addLast(indexNode);
				} else {// 同类能耗对比
					log.debug("同类能耗对比");
					cmpCategoryNode.remove(indexNode);// 防止重复
					cmpCategoryNode.addLast(indexNode);
				}
			} else {// 只有能耗分类， 进行常规分类对比
				setCmpType(CmpType.NORMAL);

				// 如果当前对比方式不是常规分类对比, 则清空当前数据
				if (cmpCategoryNode.isEmpty()) {// 未设置对比方式，默认采用建筑对比

					for (IndexTreeNode build : TagInfoManager.INSTANCE
							.getBuildingIndexNode().getChildren()) {
						// cmpCategoryNode.addLast(build);
						addIndexPath(build);
					}
				} else if (!cmpCategoryNode.getLast().getIndexType()
						.equals(IndexType.NORMAL)) {// 已设置了其它能耗分类进行对比
					// 先清空当前数据
					cmpCategoryNode.clear();
					clearChartAndTableData();
					// 再采用建筑对比方式
					for (IndexTreeNode build : TagInfoManager.INSTANCE
							.getBuildingIndexNode().getChildren()) {
						// cmpCategoryNode.addLast(build);
						addIndexPath(build);
					}

					// } else {// 已设置了常规对比方式
				}
			}
		} else {// 常规分类
			newNormalIndexNodeAdded(indexNode);
			if (energyIndexNode != null) {
				if (cmpCategoryNode.isEmpty()) {// 未设置对比项

					if (isEnergyCmp()) {// 采用能耗分项对比

					} else {// 采用常规分类对比
						cmpCategoryNode.addLast(indexNode);
					}
				} else if (cmpCategoryNode.getLast().getIndexType()
						.equals(IndexType.NORMAL)) {// 已设置了常规分类对比
					if (!cmpCategoryNode.getLast().getIndexName()
							.equals(indexNode.getIndexName())) {// 已设置了其它常规分类对比
						cmpCategoryNode.clear();
						clearChartAndTableData();
					}
					if (!cmpCategoryNode.contains(indexNode)) {
						cmpCategoryNode.addLast(indexNode);
					}
				} else {// 能耗对比
					List<IndexTreeNode> l = new ArrayList<IndexTreeNode>(
							cmpCategoryNode);
					// 先清空当前数据
					cmpCategoryNode.clear();
					clearChartAndTableData();
					for (IndexTreeNode n : l) {
						addIndexPath(n);
					}
				}
			}
		}

		if (energyIndexNode != null && normalIndexMap.size() > 0
				&& cmpCategoryNode.size() > 0) {
			updateChart();
		}

	}

}
