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

import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.effect.Effect;
import javafx.scene.effect.SepiaTone;
import javafx.scene.input.DataFormat;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Screen;

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

import com.ht.tags.model.index.IndexTreeNode;
import com.ht.ui.chart.CustomBarChart;
import com.ht.ui.chart.CustomLineChart;
import com.ht.ui.chart.CustomPieChart;
import com.ht.ui.chart.ICmpChartWrapper;
import com.ht.ui.energy.TagIndexViewer;

/**
 * 
 * @author Administrator
 */
public class MainChartViewer extends HBox {
	private static final Logger log = LoggerFactory
			.getLogger(MainChartViewer.class);
	
	private ICmpChartWrapper chartWrapper;
	
	private EnergySumView parentViewer;

	private Effect lastEffect;
	private DataFormat dragContentKey = new DataFormat("data");
	
	private ObservableList<String> baseIndexList;

	private VBox chartContainer;

	private SimpleChartViewer simpleChartViewer1;

	private SimpleChartViewer simpleChartViewer2;
	

	public MainChartViewer(EnergySumView parentViewer,
			ICmpChartWrapper ...chartWrapper) {
		//super(5);
		
		this.parentViewer = parentViewer;
		this.chartWrapper = chartWrapper[0];
		

		//setId("MainChartView");
		//getStyleClass().add("window");
		//setAlignment(Pos.TOP_CENTER);

		Rectangle2D rect = Screen.getPrimary().getVisualBounds();
		double maxHeight = rect.getHeight() - 130;
		double width = rect.getWidth()  - TagIndexViewer.WIDTH - 60;
		
		chartWrapper[0].getChart().setPrefSize(width, maxHeight);
		chartWrapper[1].getChart().setPrefSize(width, maxHeight);
		chartWrapper[2].getChart().setPrefSize(width, maxHeight);

		configureDrag();
		chartContainer = new VBox();
		chartContainer.setId("MainChartView");
		chartContainer.getChildren().add(this.chartWrapper.getChart());
		chartContainer.setPrefSize(width, maxHeight);
//		getChildren().add(chartContainer);
		
		setPrefSize(width, maxHeight);
		VBox vb = new VBox(10);
		simpleChartViewer1 = new SimpleChartViewer(this, chartWrapper[1]);
		simpleChartViewer2 = new SimpleChartViewer(this, chartWrapper[2]);
		vb.getChildren().addAll(simpleChartViewer1, simpleChartViewer2);
		getChildren().addAll(chartContainer, vb);
		
		
	}
	
	
	public void addIndexPath(IndexTreeNode indexNode) {
		if (baseIndexList.contains(indexNode.getPath())) {
		}
	}

	private void configureDrag() {
		this.setOnDragOver(new EventHandler<DragEvent>() {

			@Override
			public void handle(DragEvent event) {
				if (event.getDragboard().hasContent(dragContentKey)) {
					event.acceptTransferModes(TransferMode.MOVE);
				}

				event.consume();
			}
		});

		final SepiaTone sepiaTone = new SepiaTone();
		sepiaTone.setLevel(0.2d);
		this.setOnDragEntered(new EventHandler<DragEvent>() {

			@Override
			public void handle(DragEvent event) {
				if (event.getDragboard().hasContent(dragContentKey)) {
					lastEffect = getEffect();
					setEffect(sepiaTone);
				}

				event.consume();
			}
		});
		this.setOnDragExited(new EventHandler<DragEvent>() {

			@Override
			public void handle(DragEvent event) {
				setEffect(lastEffect);
				event.consume();
			}
		});

		this.setOnDragDropped(new EventHandler<DragEvent>() {

			@Override
			public void handle(DragEvent event) {
				Dragboard db = event.getDragboard();
				boolean success = false;
				if (db.hasContent(dragContentKey)) {
					IndexTreeNode node = (IndexTreeNode) db
							.getContent(dragContentKey);
					MainChartViewer.this.parentViewer.addIndexPath(node);
					
					success = true;
				}
				event.setDropCompleted(success);

				event.consume();
			}
		});
	}

	public ICmpChartWrapper getChartWrapper() {
		return chartWrapper;
	}
	
	public void clearData() {
		chartWrapper.clearData();
		simpleChartViewer1.getChartWrapper().clearData();
		simpleChartViewer2.getChartWrapper().clearData();
	}
	
	public void showTitle() {
		chartWrapper.showLongTitle();
		simpleChartViewer1.getChartWrapper().showShortTitle();
		simpleChartViewer2.getChartWrapper().showShortTitle();
	}

	public void setDateTitle(String dateTitle) {
		chartWrapper.setDateTitle(dateTitle);
		simpleChartViewer1.getChartWrapper().setDateTitle(dateTitle);
		simpleChartViewer2.getChartWrapper().setDateTitle(dateTitle);
	}

	public void setPreTitle(String preTitle) {
		chartWrapper.setPreTitle(preTitle);
		simpleChartViewer1.getChartWrapper().setPreTitle(preTitle);
		simpleChartViewer2.getChartWrapper().setPreTitle(preTitle);
	}

	public void setSufTitle(String sufTitle) {
		chartWrapper.setSufTitle(sufTitle);
		simpleChartViewer1.getChartWrapper().setSufTitle(sufTitle);
		simpleChartViewer2.getChartWrapper().setSufTitle(sufTitle);
	}

	public void setChartWrapper(ICmpChartWrapper chartWrapper) {
		if (chartWrapper != null) {
			chartContainer.getChildren().clear();
			chartContainer.getChildren().add(chartWrapper.getChart());
			this.chartWrapper = chartWrapper;
			this.chartWrapper.getChart().setOnMouseClicked(null);
			
			chartWrapper.showLongTitle();
			
            if (chartWrapper instanceof CustomPieChart) {
            	((CustomPieChart)chartWrapper).setLabelsVisible(true);
            	chartWrapper.getChart().setLegendVisible(true);
            }
            if (chartWrapper instanceof CustomBarChart) {
            	((CustomBarChart)chartWrapper).getXAxis().setTickLabelsVisible(true);
            }
            if (chartWrapper instanceof CustomLineChart) {
            	chartWrapper.getChart().setLegendVisible(true);
            	((CustomLineChart)chartWrapper).getXAxis().setTickLabelsVisible(true);
            	//((CustomLineChart)chartWrapper).setCreateSymbols(true);
            }
		}
	}
	
}
