package com.ht.ui;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.animation.TimelineBuilder;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.InnerShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import com.ht.ui.controls.WindowButtons;

class MainToolBar extends HBox {

	public MainToolBar(Stage stage, EventHandler<ActionEvent> handler) {
		super(10);
		
		// toolBar.setTranslateY(5);
		setId("mainToolBar");
		setMaxHeight(80);
		setAlignment(Pos.CENTER);
		//GridPane.setConstraints(toolBar, 0, 0);

//		VBox vb = new VBox();
//		// vb.setTranslateY(-10);
//		vb.setAlignment(Pos.CENTER);

		
		Label title = new Label("PDM 5000");
		//title.setMinHeight(80);
		title.setTranslateY(-20);
		title.setId("title1");
		getChildren().add(title);

		DropShadow shadow = new DropShadow();
		Label title2 = new Label("能源监控管理系统");
		//title2.setMinHeight(80);
		title2.setTranslateY(-18);
		//title2.setTextFill(Color.BEIGE);
		//title2.setEffect(shadow);
		title2.setEffect(shadow);
		title2.setId("title2");
		//title2.setTranslateX(20);
		getChildren().add(title2);

		//getChildren().add(vb);

		// ImageView logo = new ImageView(new
		// Image(getClass().getResourceAsStream("images/logo.png")));
		// HBox.setMargin(logo, new Insets(0,0,0,5));
		// toolBar.getItems().add(logo);

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

		final int headerButtonWidth = 70;
		final int headerButtonHeight = 66;

		// 主监控界面
		Button homeButton = new Button();
		homeButton.getStyleClass().add("image-button");
//		homeButton.setTooltip(new Tooltip("主页"));
		homeButton.setId("homeButton");
		homeButton.setMinSize(headerButtonWidth, headerButtonHeight);
		homeButton.setPrefSize(headerButtonWidth, headerButtonHeight);
		homeButton.setOnAction(handler);

		// 能耗统计管理界面
		Button chartButton = new Button();
		chartButton.getStyleClass().add("image-button");
//		chartButton.setTooltip(new Tooltip("统计图表"));
		chartButton.setId("chartButton");
		chartButton.setMinSize(headerButtonWidth, headerButtonHeight);
		chartButton.setPrefSize(headerButtonWidth, headerButtonHeight);
		chartButton.setOnAction(handler);

		// 报警记录管理
		Button alarmButton = new Button();
		alarmButton.setId("alarmButton");
		alarmButton.getStyleClass().add("image-button");
//		alarmButton.setTooltip(new Tooltip("报警记录"));
		alarmButton.setMinSize(headerButtonWidth, headerButtonHeight);
		alarmButton.setPrefSize(headerButtonWidth, headerButtonHeight);
		alarmButton.setOnAction(handler);

		// 配置信息
		Button infoButton = new Button();
		infoButton.setId("infoButton");
		infoButton.getStyleClass().add("image-button");
		infoButton.setTooltip(new Tooltip("配置信息"));
		infoButton.setMinSize(headerButtonWidth, headerButtonHeight);
		infoButton.setPrefSize(headerButtonWidth, headerButtonHeight);
		infoButton.setOnAction(handler);

		// 用户管理
		Button userButton = new Button();
		userButton.setId("userButton");
		userButton.getStyleClass().add("image-button");
//		userButton.setTooltip(new Tooltip("用户管理"));
		userButton.setMinSize(headerButtonWidth, headerButtonHeight);
		userButton.setPrefSize(headerButtonWidth, headerButtonHeight);
		userButton.setOnAction(handler);
		
		
		final HBox hb = new HBox();
		hb.getChildren().addAll(homeButton, chartButton, alarmButton, infoButton, userButton);
		getChildren().add(hb);
		
		final Timeline tl = TimelineBuilder
		.create().delay(Duration.seconds(1))
		.keyFrames(
				new KeyFrame(Duration.seconds(1),
						new EventHandler<ActionEvent>() {
							public void handle(ActionEvent t) {
							}
						}, new KeyValue(hb.opacityProperty(), 0.1,
								Interpolator.EASE_BOTH))).build();
		
		hb.setOnMouseEntered(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				tl.stop();
				hb.setOpacity(1);
			}
		});
		hb.setOnMouseExited(new EventHandler<Event>() {
			@Override
			public void handle(Event event) {
				if (MainApplication.getMainApp().getSvgViewer().isVisible())
					tl.play();
			}
		});
		hb.setOpacity(0.1);

		Region spacer2 = new Region();
		HBox.setHgrow(spacer2, Priority.ALWAYS);
		getChildren().add(spacer2);

		if (!MainApplication.isApplet()) {
			// add close min max
			final WindowButtons windowButtons = new WindowButtons(stage);
			getChildren().add(windowButtons);
			// add window header double clicking
			setOnMouseClicked(new EventHandler<MouseEvent>() {
				@Override
				public void handle(MouseEvent event) {
					if (event.getClickCount() == 2) {
						windowButtons.toogleMaximized();
					}
				}
			});
		}
	}
}
