package com.ht.ui.taginfo;

import java.text.SimpleDateFormat;
import java.util.Date;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;

import com.ht.ui.controls.MessageInfoLabel;
import com.ht.ui.controls.calendar.FXCalendar;

public class ManualPanel extends VBox {
	private MessageInfoLabel messageLabel;
	private TextField timeValue;
	private TextField dateValue;

	public ManualPanel(EventHandler<ActionEvent> onOK) {
		super(5);
		
		Label title = new Label("数据录入");
		title.getStyleClass().add("info-pane-header");

		final GridPane gridPane = new GridPane();
		gridPane.setHgap(6);
		gridPane.setVgap(10);
		VBox.setMargin(gridPane, new Insets(0, 0, 10, 10));

		Label info = new Label("请在上方表格的【能耗量】中输入相应数据.");
		info.setStyle("-fx-text-fill: blue;");
		GridPane.setConstraints(info, 0, 0, 4, 1);
		GridPane.setHalignment(info, HPos.LEFT);
		gridPane.getChildren().add(info);

		Label label = new Label("日期:");
		GridPane.setConstraints(label, 0, 2);
		GridPane.setHalignment(label, HPos.RIGHT);
		gridPane.getChildren().add(label);

		dateValue = new TextField();
		dateValue.setMaxWidth(100);
		GridPane.setConstraints(dateValue, 1, 2);
		GridPane.setHalignment(dateValue, HPos.LEFT);
		gridPane.getChildren().add(dateValue);

		label = new Label("时间:");
		GridPane.setConstraints(label, 2, 2);
		GridPane.setHalignment(label, HPos.RIGHT);
		gridPane.getChildren().add(label);

		timeValue = new TextField("00:00");
		timeValue.setMaxWidth(80);
		GridPane.setConstraints(timeValue, 3, 2);
		GridPane.setHalignment(timeValue, HPos.LEFT);
		gridPane.getChildren().add(timeValue);
		
		label = new Label("能耗:");
		GridPane.setConstraints(label, 0, 3);
		GridPane.setHalignment(label, HPos.RIGHT);
		gridPane.getChildren().add(label);

		TextField value = new TextField();
		value.setMaxWidth(80);
		GridPane.setConstraints(value, 1, 3);
		GridPane.setHalignment(value, HPos.LEFT);
		gridPane.getChildren().add(value);

		Button ok = new Button("提交");
		GridPane.setConstraints(ok, 2, 3, 2, 1);
		GridPane.setHalignment(ok, HPos.CENTER);
		gridPane.getChildren().add(ok);

		messageLabel = new MessageInfoLabel();
		GridPane.setConstraints(messageLabel, 0, 1, 4, 1);
		GridPane.setHalignment(messageLabel, HPos.CENTER);
		gridPane.getChildren().add(messageLabel);
		
		final Popup popup = new Popup();
		popup.setAutoHide(true);
		popup.setAutoFix(true);
		popup.setHideOnEscape(true);

		final FXCalendar fxCalendar = new FXCalendar(false);
		final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		fxCalendar.setOnDateSelected(new Runnable() {

			@Override
			public void run() {
				popup.hide();
				dateValue.setText(dateFormat.format(fxCalendar.getValue()));
				dateValue.setUserData(fxCalendar.getValue());
			}
		});
		popup.getContent().add(fxCalendar);

		dateValue.setText(dateFormat.format(new Date()));
		dateValue.setUserData(new Date());
		dateValue.setOnMouseClicked(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {

				popup.show(dateValue, event.getSceneX(), event.getSceneY());
			};
		});

		getChildren().addAll(title, gridPane);
	}
	
	public String getDateTimeText() {
		return dateValue.getText().trim() + " " + timeValue.getText().trim();
	}
	
	public void showMessage(String msg) {
		this.messageLabel.show(msg);
	}
}
