package com.ht.ui.energy.sum;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.text.TextAlignment;
import javafx.util.Callback;

import org.javafxdata.control.cell.LabelCellFactory;

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

class EnergySumTable extends TableView<EnergyData> {

	private ObservableList<EnergyData> energyDataList = FXCollections
			.observableArrayList();
	private TableColumn<EnergyData, Number> sumCol;
	private TableColumn<EnergyData, Number> convertCol;

	private Map<IndexTreeNode, EnergyData> tableDataMap = new HashMap<IndexTreeNode, EnergyData>();

	public EnergySumTable() {
		DecimalFormat decimalFormat = new DecimalFormat("0.0#");
		Callback<TableColumn<EnergyData, Number>, TableCell<EnergyData, Number>> valueCellCallback = LabelCellFactory
				.<EnergyData, Number> forTableColumn(TextAlignment.RIGHT,
						decimalFormat);

		TableColumn<EnergyData, String> energyCategoryCol = new TableColumn<EnergyData, String>(
				"能耗分项");
		energyCategoryCol.setMinWidth(100);
		energyCategoryCol
				.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, String>, ObservableValue<String>>() {

					@Override
					public ObservableValue<String> call(
							CellDataFeatures<EnergyData, String> param) {
						return param.getValue().getEnergyCategory();
					}
				});

		getColumns().add(energyCategoryCol);

		TableColumn<EnergyData, Number> maxCol = new TableColumn<EnergyData, Number>(
				"最大值");
		maxCol.setMinWidth(120);
		maxCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, Number>, ObservableValue<Number>>() {

			@Override
			public ObservableValue<Number> call(
					CellDataFeatures<EnergyData, Number> param) {
				return param.getValue().getMax();
			}
		});
		maxCol.setCellFactory(valueCellCallback);
		getColumns().add(maxCol);

		TableColumn<EnergyData, Number> minCol = new TableColumn<EnergyData, Number>(
				"最小值");
		minCol.setMinWidth(120);
		minCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, Number>, ObservableValue<Number>>() {

			@Override
			public ObservableValue<Number> call(
					CellDataFeatures<EnergyData, Number> param) {
				return param.getValue().getMin();
			}
		});
		minCol.setCellFactory(valueCellCallback);

		getColumns().add(minCol);

		TableColumn<EnergyData, Number> avgCol = new TableColumn<EnergyData, Number>(
				"平均值");
		avgCol.setMinWidth(120);
		avgCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, Number>, ObservableValue<Number>>() {

			@Override
			public ObservableValue<Number> call(
					CellDataFeatures<EnergyData, Number> param) {
				return param.getValue().getAvg();
			}
		});
		avgCol.setCellFactory(valueCellCallback);

		getColumns().add(avgCol);

		sumCol = new TableColumn<EnergyData, Number>("当天总能耗");
		sumCol.setMinWidth(120);
		sumCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, Number>, ObservableValue<Number>>() {

			@Override
			public ObservableValue<Number> call(
					CellDataFeatures<EnergyData, Number> param) {
				return param.getValue().getSum();
			}
		});
		sumCol.setCellFactory(valueCellCallback);
		getColumns().add(sumCol);

		convertCol = new TableColumn<EnergyData, Number>("标准煤折算(kg)");
		convertCol.setMinWidth(120);
		convertCol
				.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, Number>, ObservableValue<Number>>() {

					@Override
					public ObservableValue<Number> call(
							CellDataFeatures<EnergyData, Number> param) {
						return param.getValue().getConvertSum();
					}
				});
		convertCol.setCellFactory(valueCellCallback);
		getColumns().add(convertCol);

		setItems(energyDataList);
	}

	void setSumColText(String value) {
		sumCol.setText(value);
	}

	/**
	 * 增加新的表示常规分类的列
	 * 
	 * @param colText
	 */
	void addNewNormalCol(final String colText) {
		TableColumn<EnergyData, String> col = new TableColumn<EnergyData, String>(
				colText);
		col.setPrefWidth(100);
		col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, String>, ObservableValue<String>>() {

			@Override
			public ObservableValue<String> call(
					CellDataFeatures<EnergyData, String> param) {
				return param.getValue().getNormalCategoryMap().get(colText);
			}
		});
		getColumns().add(1, col);
		getProperties().put(colText, col);
	}

	/**
	 * 更新常规分类列的名称
	 * 
	 * @param colText
	 */
	void updateNormalCol(final String colText) {

		TableColumn<EnergyData, String> col = (TableColumn<EnergyData, String>) getProperties()
				.get(colText);
		col.setText(colText);
		col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<EnergyData, String>, ObservableValue<String>>() {

			@Override
			public ObservableValue<String> call(
					CellDataFeatures<EnergyData, String> param) {
				return param.getValue().getNormalCategoryMap().get(colText);
			}
		});
	}
	
	void removeAllNormalCols() {
		for (Object obj : getProperties().values()) {
			getColumns().remove(obj);
		}
	}

	void clearData() {
		getItems().clear();
		tableDataMap.clear();
	}

	EnergyData getData(IndexTreeNode node) {

		return tableDataMap.get(node);
	}

	void putData(IndexTreeNode node, EnergyData data) {

		getItems().add(data);
		tableDataMap.put(node, data);
	}

}
