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

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;

import com.ht.tags.model.index.IndexTreeNode;

/**
 * 
 * @author Administrator
 */
public class CustomLineChart extends LineChart<String, Number> implements
		ICmpChartWrapper {
	// private AreaChart<String, Number> chart;
	// private LineChart<String, Number> chart;
	// private CategoryAxis xAxis;
	private List<IndexTreeNode> categoryList = new ArrayList<IndexTreeNode>();

	private DecimalFormat decimalFormat = new DecimalFormat("0.0#");

	private String dateTitle = "";
	private String preTitle = "";
	private String sufTitle = "";

	private Tooltip tooltip = new Tooltip();
	private int displayType = 0;// 用于表示当前显示数据的方式，
								// 0表示显示总能耗，1表示显示单位建筑面积数据，2表示显示单位空调面积数据，3表示显示单位采暖面积数据

	public CustomLineChart() {
		super(new CategoryAxis(), new NumberAxis());
		// CategoryAxis xAxis = (CategoryAxis) getXAxis();
		// xAxis = new CategoryAxis();

		setAnimated(false);

	}

	@Override
	public void showShortTitle() {
		setTitle("折线图");
	}

	public void showLongTitle() {
		setTitle(dateTitle + this.preTitle + this.sufTitle + "折线图");
	}

	@Override
	public void setDateTitle(String dateTitle) {
		this.dateTitle = dateTitle;
	}

	@Override
	public void setPreTitle(String preTitle) {
		this.preTitle = preTitle;
	}

	@Override
	public void setSufTitle(String sufTitle) {
		this.sufTitle = sufTitle;
	}

	@Override
	public LineChart<String, Number> getChart() {
		return this;
	}

	public void addOrUpdateValue(IndexTreeNode category,
			List<XYChart.Data<String, Number>> values) {
		// xAxis.setLowerBound(((Number)
		// values.get(0).getXValue()).doubleValue());
		// xAxis.setUpperBound(((Number) values.get(values.size() -
		// 1).getXValue())
		// .doubleValue());
		if (categoryList.contains(category)) {
			final XYChart.Series<String, Number> series = getData().get(
					categoryList.indexOf(category));
			series.getData().setAll(values);
		} else {
			categoryList.add(category);

			String name = category.getName();
			final XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
			series.setName(name);
			series.getData().addAll(values);
			
			getData().add(series);

			// 为symbol设置工具提示
			for (final Data<String, Number> data : series.getData()) {
				final Node node = data.getNode();
				if (node == null) {
					break;
				}
				Tooltip.install(data.getNode(), tooltip);
				node.setOnMouseEntered(new EventHandler<MouseEvent>() {
					@Override
					public void handle(MouseEvent event) {
						tooltip.setText(series.getName() + ": "
								+ decimalFormat.format(data.getYValue()));
						tooltip.show(node, event.getScreenX(),
								event.getScreenY());
					}
				});
				node.setOnMouseMoved(new EventHandler<MouseEvent>() {
					@Override
					public void handle(MouseEvent event) {
						tooltip.show(node, event.getScreenX(),
								event.getScreenY());
					}
				});
				node.setOnMouseExited(new EventHandler<MouseEvent>() {
					@Override
					public void handle(MouseEvent event) {
						tooltip.hide();
					}
				});
			}

		}
	}

	@Override
	public void showAsHeatingAreaData() {
		if (displayType == 3) {
			return;
		}
		displayType = 3;

		for (int i = 0; i < categoryList.size(); i++) {
			IndexTreeNode node = categoryList.get(i);

			if (node.getHeatingArea() == null) {
				continue;
			}

			List<XYChart.Data<String, Number>> values = getData().get(i)
					.getData();
			for (XYChart.Data<String, Number> data : values) {
				double value = (Double) data.getExtraValue();
				data.setYValue(value / node.getHeatingArea());
				// data.setYValue(data.getYValue().doubleValue() /
				// node.getArea());
			}
		}

	}

	@Override
	public void showAsAirConditionAreaData() {
		if (displayType == 2) {
			return;
		}
		displayType = 2;

		for (int i = 0; i < categoryList.size(); i++) {
			IndexTreeNode node = categoryList.get(i);

			if (node.getAirConditionArea() == null) {
				continue;
			}

			List<XYChart.Data<String, Number>> values = getData().get(i)
					.getData();
			for (XYChart.Data<String, Number> data : values) {
				double value = (Double) data.getExtraValue();
				data.setYValue(value / node.getAirConditionArea());
				// data.setYValue(data.getYValue().doubleValue() /
				// node.getArea());
			}
		}

	}

	@Override
	public void showAsAreaData() {
		if (displayType == 1) {
			return;
		}
		displayType = 1;

		for (int i = 0; i < categoryList.size(); i++) {
			IndexTreeNode node = categoryList.get(i);

			if (node.getArea() == null) {
				continue;
			}

			List<XYChart.Data<String, Number>> values = getData().get(i)
					.getData();
			for (XYChart.Data<String, Number> data : values) {
				double value = (Double) data.getExtraValue();
				data.setYValue(value / node.getArea());
				// data.setYValue(data.getYValue().doubleValue() /
				// node.getArea());
			}
		}

	}

	@Override
	public void showAsNormalData() {
		if (displayType == 0) {
			return;
		}
		displayType = 0;

		for (int i = 0; i < categoryList.size(); i++) {

			List<XYChart.Data<String, Number>> values = getData().get(i)
					.getData();
			for (XYChart.Data<String, Number> data : values) {
				// data.setYValue(data.getYValue().doubleValue() *
				// node.getArea());
				double value = (Double) data.getExtraValue();
				data.setYValue(value);
			}
		}

	}

	@Override
	public void clearData() {
		this.categoryList.clear();
		getData().clear();
	}

	@Override
	public List<IndexTreeNode> getCategoryList() {
		return categoryList;
	}
}
