package com.ht.ui.energy.sum;

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

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;

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

class TopPanel extends HBox {
	private static Logger log = LoggerFactory.getLogger(TopPanel.class);
	
	private ToggleGroup cmpTypeGroup;
	private ToggleButton energyCmpBtn;
	private ToggleButton normalCmpBtn;
	
	private ICmpTypeChangeListener cmpTypeChangeListener;
	private IAreaTypeChangeListener areaTypeChangeListener;
	
	public TopPanel(EventHandler<ActionEvent> handler) {
		super(5);
		
		Button clearBtn = new Button("重置");
		clearBtn.setPrefWidth(80);
		clearBtn.setStyle("-fx-base: lightblue;");
		clearBtn.setOnAction(handler);

		Button reportBtn = new Button("报表");
		reportBtn.setPrefWidth(80);
		reportBtn.setStyle("-fx-base: lightblue;");
		reportBtn.setOnAction(handler);

		Button historyBtn = new Button("历史数据");
		historyBtn.setPrefWidth(80);
		historyBtn.setStyle("-fx-base: lightblue;");
		historyBtn.setOnAction(handler);

		getChildren().addAll(clearBtn, reportBtn, historyBtn);

		Region spacer = new Region();
		HBox.setHgrow(spacer, Priority.ALWAYS);

		getChildren().addAll(createToggleButtons1(), spacer,
				createToggleButtons2());
	}
	
	private HBox createToggleButtons1() {
		HBox hb = new HBox();

		energyCmpBtn = new ToggleButton("能耗分项对比");
		normalCmpBtn = new ToggleButton("建筑分区对比");

		energyCmpBtn.setPrefWidth(120);
		normalCmpBtn.setPrefWidth(120);

		energyCmpBtn.setStyle("-fx-base: deepskyblue;");
		normalCmpBtn.setStyle("-fx-base: deepskyblue;");
		energyCmpBtn.getStyleClass().add("left");
		normalCmpBtn.getStyleClass().add("right");

		cmpTypeGroup = new ToggleGroup();
		energyCmpBtn.setToggleGroup(cmpTypeGroup);
		normalCmpBtn.setToggleGroup(cmpTypeGroup);
		cmpTypeGroup.selectToggle(energyCmpBtn);
		// tb1.setDisable(true);

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

					@Override
					public void changed(
							ObservableValue<? extends Toggle> observable,
							Toggle oldValue, Toggle selectedToggle) {
						log.debug("oldValue: {}", oldValue);
						log.debug("newValue: {}", selectedToggle);
						if (selectedToggle != null) {
							if (oldValue != null
									&& !oldValue.equals(selectedToggle)) {

								if (cmpTypeChangeListener != null) {
									if (!cmpTypeChangeListener.changed(getCmpType())) {
										cmpTypeGroup.selectToggle(oldValue);
									}
								} else {
									cmpTypeGroup.selectToggle(oldValue);
								}
							}
						} else {
							cmpTypeGroup.selectToggle(oldValue);
						}
					}
				});

		hb.getChildren().addAll(energyCmpBtn, normalCmpBtn);
		hb.getStyleClass().add("ab-toggle");
		return hb;
	}

	private HBox createToggleButtons2() {
		HBox hb = new HBox();
		hb.getStyleClass().add("ab-toggle");

		final ToggleButton tb1 = new ToggleButton("总能耗");
		final ToggleButton tb2 = new ToggleButton("单位建筑面积");
		final ToggleButton tb3 = new ToggleButton("单位空调面积");
		final ToggleButton tb4 = new ToggleButton("单位采暖面积");

		tb1.setPrefWidth(120);
		tb2.setPrefWidth(120);
		tb3.setPrefWidth(120);
		tb4.setPrefWidth(120);

		tb1.getStyleClass().add("left");
		tb4.getStyleClass().add("right");

		final ToggleGroup group = new ToggleGroup();
		tb1.setToggleGroup(group);
		tb2.setToggleGroup(group);
		tb3.setToggleGroup(group);
		tb4.setToggleGroup(group);
		group.selectToggle(tb1);
		// tb1.setDisable(true);

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

					@Override
					public void changed(
							ObservableValue<? extends Toggle> observable,
							Toggle oldValue, Toggle selectedToggle) {
						log.debug("oldValue: {}", oldValue);
						log.debug("newValue: {}", selectedToggle);
						if (selectedToggle != null) {
							if (oldValue != null
									&& !oldValue.equals(selectedToggle)) {

								if (areaTypeChangeListener == null) {
									group.selectToggle(oldValue);
									return;
								}
								if (selectedToggle.equals(tb1)) {
									log.debug("显示总能耗数据");
									areaTypeChangeListener.changed(null);

								} else if (selectedToggle.equals(tb2)) {// 单位建筑面积能耗
									log.debug("显示单位面积能耗数据");
									if (!areaTypeChangeListener.changed(AreaType.AREA)) {
										group.selectToggle(oldValue);
									}
								} else if (selectedToggle.equals(tb3)) {// 单位空调面积能耗
									if (!areaTypeChangeListener.changed(AreaType.AIR_AREA)) {
										group.selectToggle(oldValue);
									}
								} else if (selectedToggle.equals(tb4)) {// 单位采暖面积能耗
									if (!areaTypeChangeListener.changed(AreaType.HEATING_AREA)) {
										group.selectToggle(oldValue);
									}
								}
							}
						} else {
							group.selectToggle(oldValue);
						}
					}
				});

		hb.getChildren().addAll(tb1, tb2, tb3, tb4);
		return hb;
	}
	
	void onCmpTypeChanged(ICmpTypeChangeListener listener) {
		this.cmpTypeChangeListener = listener;
	}
	
	void onAreaTypeChanged(IAreaTypeChangeListener listener) {
		this.areaTypeChangeListener = listener;
	}
	
	void setCmpType(CmpType cmpType) {
		if (cmpType.equals(CmpType.ENERGY)) {
			cmpTypeGroup.selectToggle(energyCmpBtn);
		} else {
			cmpTypeGroup.selectToggle(normalCmpBtn);
		}
	}
	
	CmpType getCmpType() {
		if (cmpTypeGroup.getSelectedToggle().equals(energyCmpBtn)) {
			return CmpType.ENERGY;
		} else {
			return CmpType.NORMAL;
		}
	}
	
	interface ICmpTypeChangeListener {
		public boolean changed(CmpType  cmpType);
	}
	
	interface IAreaTypeChangeListener {
		public boolean changed(AreaType  areaType);
	}
	
	static enum CmpType {
		NORMAL, ENERGY;
	}
	static enum AreaType {
		AREA, AIR_AREA, HEATING_AREA;
	}
}
