package com.ht.ui.energy;

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

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

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

import com.ht.tags.model.index.IndexTreeNode;
import com.ht.tags.model.index.IndexType;
import com.ht.ui.MainApplication;
import com.ht.ui.controls.MessageInfoLabel;
import com.ht.ui.energy.sum.EnergySumView;
import com.ht.ui.energy.util.EnergyConst;
import com.ht.ui.util.HessianResponseTask;
import com.ht.ui.util.MessageConstants;
import com.ht.web.hessian.HessianFactoryManger;
import com.ht.web.remoting.HessianResponse;

public class EnergyManageView extends VBox {
	private static final Logger log = LoggerFactory.getLogger(EnergyManageView.class);

	private EnergySumView sumView;
	private EnergyCmpView cmpView;
	
	private StackPane stackPane;
	
	private TagIndexViewer tagIndexViewer;
//	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;
	
	private MessageInfoLabel message;

	public EnergyManageView() {
		super(20);
		
//		setVisible(false);
		getStylesheets().add(getClass().getResource("energy.css").toExternalForm());
		setId("EnergyManageView");
		

		visibleProperty().addListener(new ChangeListener<Boolean>() {

			@Override
			public void changed(ObservableValue<? extends Boolean> observable,
					Boolean oldValue, Boolean newValue) {
				if (newValue) {
					updateIndexTreeList();
				}
			}
		});
		
		createHeader();
		createViews();
		
		updateIndexTreeList();
		
		// 显示缺省内容 
		sumView.generateDefaultChart();
	}
	
	public TagIndexViewer getTagIndexViewer() {
		return tagIndexViewer;
	}

	private void createHeader() {
		HBox hb = new HBox(1);
		hb.setId("header");

//		final ToggleButton power = new ToggleButton("用电能耗分析");
		final ToggleButton sum = new ToggleButton("汇总对比分析");
		final ToggleButton cmp = new ToggleButton("同比环比分析");
		final ToggleButton unit = new ToggleButton("单位能耗分析");
		final ToggleButton effi = new ToggleButton("能耗效率分析");// COP分析， PUE分析

		final ToggleGroup group = new ToggleGroup();

//		power.setToggleGroup(group);
		sum.setToggleGroup(group);
		cmp.setToggleGroup(group);
		unit.setToggleGroup(group);
		effi.setToggleGroup(group);

		group.selectToggle(sum);

		group.selectedToggleProperty().addListener(
				new ChangeListener<Toggle>() {

					@Override
					public void changed(
							ObservableValue<? extends Toggle> observable,
							Toggle oldValue, Toggle selectedToggle) {
						if (selectedToggle != null) {
							if (selectedToggle.equals(sum)) {
								showView(sumView);
							} else if (selectedToggle.equals(cmp)) {
								if (cmpView == null) {

									cmpView = new EnergyCmpView(EnergyManageView.this, sumView.getEnergyIndexNode(), sumView.getNormalIndexMap());
									stackPane.getChildren().add(0, cmpView);
								}
								showView(cmpView);
							} else if (selectedToggle.equals(effi)) {
							} else {
							}
						} else {
							group.selectToggle(oldValue);
						}
					}
				});

		hb.getChildren().addAll(sum, cmp, unit, effi);
		getChildren().add(hb);
	}

	private void createViews() {
		stackPane = new StackPane();
		stackPane.setAlignment(Pos.CENTER);

		
		tagIndexViewer = new TagIndexViewer();
		tagIndexViewer.setTranslateY(-20);
		StackPane.setAlignment(tagIndexViewer, Pos.TOP_RIGHT);
		//tagIndexViewer.setOpacity(0.8);
		message = new MessageInfoLabel(3, 2);
		sumView = new EnergySumView(this);
		stackPane.getChildren().addAll(sumView, tagIndexViewer, message);

		getChildren().add(stackPane);
	}
	
	
	public void showMessage(String content) {
		message.show(content);
	}

	private void showView(Node view) {
		if (!view.equals(sumView)) {
			sumView.setVisible(false);
		}

		if (!view.equals(cmpView) && cmpView != null) {
			cmpView.setVisible(false);
		}
		
		view.setVisible(true);
	}

	private void updateIndexTreeList() {
		tagIndexViewer.updateIndexTree();
	}
	
//	public List<IndexTreeNode> getIndexTreeList() {
//		return tagIndexViewer.getIndexTreeList();
//	}
//	
//	public Map<String, IndexTreeNode> getEnergyIndexMap() {
//		return tagIndexViewer.getEnergyIndexMap();
//	}
//
//	public Map<String, IndexTreeNode> getNormalIndexMap() {
//		return tagIndexViewer.getNormalIndexMap();
//	}
//
//	public IndexTreeNode getPowerIndexNode() {
//		return tagIndexViewer.getPowerIndexNode();
//	}
//
//	public IndexTreeNode getBuildingIndexNode() {
//		return tagIndexViewer.getBuildingIndexNode();
//	}

}
