/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ht.ui.energy;

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

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TitledPane;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

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.MainApplication;
import com.ht.ui.controls.IndexNodeTreeItem;
import com.ht.ui.energy.util.EnergyConst;
import com.ht.ui.util.MessageConstants;
import com.ht.web.hessian.HessianFactoryManger;
import com.ht.web.remoting.HessianResponse;

/**
 * 
 * @author Administrator
 */
public class TagIndexViewer extends VBox {
	private static final Logger log = LoggerFactory
			.getLogger(TagIndexViewer.class);

	public static final double WIDTH = 200;

	private Label title;
	private Accordion tagIndexAccordion;

	private double mouseDragOffsetX = 0;
	private double mouseDragOffsetY = 0;
	private double translateX = 0;
	private double translateY = 0;

	private TreeView<IndexTreeNode> energyTree;
	private TreeView<IndexTreeNode> normalTree;

	public TagIndexViewer() {

		setId("TagIndexViewer");
		setMinSize(WIDTH, 400);
		setMaxSize(WIDTH, 400);

		setAlignment(Pos.TOP_LEFT);

		// setOpacity(.8);
		HBox hb = new HBox();
		hb.setId("header");
		title = new Label("标签索引");
		title.setMinHeight(25);
		title.setPrefSize(WIDTH, 25);
		title.setAlignment(Pos.CENTER);
		hb.getChildren().add(title);

		tagIndexAccordion = new Accordion();
		// tagIndexAccordion.setOpacity(0.618);
		tagIndexAccordion.setMaxWidth(WIDTH);

		getChildren().addAll(hb, tagIndexAccordion);

		hb.setOnMousePressed(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				mouseDragOffsetX = event.getScreenX();
				mouseDragOffsetY = event.getScreenY();
				translateX = TagIndexViewer.this.getTranslateX();
				translateY = TagIndexViewer.this.getTranslateY();
			}
		});
		hb.setOnMouseDragged(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {

				double offsetX = event.getScreenX() - mouseDragOffsetX;
				double offsetY = event.getScreenY() - mouseDragOffsetY;
				if (offsetX > 2 || offsetX < -2 || offsetY > 2 || offsetY < -2) {

					translateY = translateY + offsetY;
					if (translateY < -300) {
						translateY = -300;
					}
					translateX = translateX + offsetX;

					TagIndexViewer.this.setTranslateX(translateX);
					TagIndexViewer.this.setTranslateY(translateY);

					mouseDragOffsetX = event.getScreenX();
					mouseDragOffsetY = event.getScreenY();
				}
			}
		});

	}

	public void updateIndexTree() {
		List<IndexTreeNode> data = TagInfoManager.INSTANCE.getIndexTreeList();
		
		if (data == null) {
			return;
		}
		log.debug("更新索引列表");

		tagIndexAccordion.getPanes().clear();
		if (data != null) {

			log.debug("返回列表数 {}", data.size());
			for (IndexTreeNode root : data) {
				TreeView<IndexTreeNode> tree = buildTreeView(root);

				TitledPane t = new TitledPane(root.getName(), tree);
				t.setAnimated(false);
				tagIndexAccordion.getPanes().add(t);

				if (root.getIndexType() == IndexType.ENERGY) {

					log.debug("能耗分类分项");

					energyTree = tree;

				} else if (root.getIndexType() == IndexType.NORMAL) {// 常规索引

					log.debug("常规分类");

					normalTree = tree;

				}

			}
		}
	}

	private TreeView<IndexTreeNode> buildTreeView(IndexTreeNode root) {
		final TreeView<IndexTreeNode> treeView = new TreeView<IndexTreeNode>(
				new IndexNodeTreeItem(root, null));
		treeView.setMaxWidth(WIDTH - 5);
		// treeView.setStyle("-fx-background-color: transparent;");
		treeView.setStyle("-fx-background-color: #d1dee3;");
		treeView.setShowRoot(false);
		treeView.setEditable(false);

		// treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		// treeView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		treeView.setOnDragDetected(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				if (treeView.getSelectionModel().getSelectedItem() == null) {
					return;
				}
				IndexTreeNode tag = ((TreeItem<IndexTreeNode>) treeView
						.getSelectionModel().getSelectedItem()).getValue();

				if (tag.getIndexType().equals(IndexType.NORMAL)
						&& !tag.getPath().contains("/")) {// 常规分类名称不可拖拽
					return;
				}

				Dragboard db = treeView.startDragAndDrop(TransferMode.MOVE);

				ClipboardContent content = new ClipboardContent();
				content.put(new DataFormat("data"), tag);
				// content.putString(tag.getPath());
				db.setContent(content);

				event.consume();
			}
		});
		return treeView;
	}

}
