package com.ht.ui.controls;

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

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.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.ToolBar;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.stage.Popup;

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

public class RecorderNavigator extends ToolBar {

	private FXCalendar fxCalendar;
	private Popup popup;
	private TextField selText = null;

	private Label dateLabel;
	private TextField countText;
	private TextField textStart;
	private TextField textEnd;

	private Calendar startCal = Calendar.getInstance();
	private int dayRange = 1;
	private final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");

	private Runnable reload = null;
	private Calendar endCal;
	private ToggleGroup group;
	private ChangeListener<Toggle> toggleChangeListener;

	public RecorderNavigator(Runnable reload) {
		super();

		this.reload = reload;

		getStyleClass().add("mini-toolbar");
		setStyle("-fx-border-width: 1 1 0 1;-fx-border-color:dimgray;");

		final ToggleButton tb1 = new ToggleButton("1天");
		final ToggleButton tb2 = new ToggleButton("7天");
		final ToggleButton tb3 = new ToggleButton("30天");

		group = new ToggleGroup();
		tb1.setToggleGroup(group);
		tb2.setToggleGroup(group);
		tb3.setToggleGroup(group);
		group.selectToggle(tb1);

		toggleChangeListener = new ChangeListener<Toggle>() {

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


					if (selectedToggle.equals(tb1)) {
						startCal.add(Calendar.DATE, dayRange-1);
						dayRange = 1;
					} else if (selectedToggle.equals(tb2)) {
						startCal.add(Calendar.DATE, dayRange-7);
						dayRange = 7;
					} else if (selectedToggle.equals(tb3)) {
						startCal.add(Calendar.DATE, dayRange-30);
						dayRange = 30;
					}
					setDate(startCal.getTime());
						
				} else {
					group.selectToggle(oldValue);
				}
			}
		};
		group.selectedToggleProperty().addListener( toggleChangeListener );

		Button undoButton = new Button();
		undoButton.setGraphic(new ImageView(new Image(MainApplication.class
				.getResourceAsStream("images/back.png"))));
		undoButton.setTooltip(new Tooltip("向前"));
		undoButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				startCal.add(Calendar.DATE, -dayRange);
				setDate(startCal.getTime());
			}
		});
		undoButton.setMaxHeight(Double.MAX_VALUE);

		Button redoButton = new Button();
		redoButton.setGraphic(new ImageView(new Image(MainApplication.class
				.getResourceAsStream("images/forward.png"))));
		redoButton.setTooltip(new Tooltip("向后"));
		redoButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				startCal.add(Calendar.DATE, dayRange);
				setDate(startCal.getTime());
			}
		});

		Button reloadButton = new Button();
		reloadButton.setGraphic(new ImageView(new Image(getClass()
				.getResourceAsStream("/com/ht/ui/images/reload.png"))));
		reloadButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				reload();
			}

		});
		reloadButton.setMaxHeight(Double.MAX_VALUE);

		dateLabel = new Label();
		dateLabel.setMinWidth(100);

		countText = new TextField();
		countText.setEditable(false);
		countText.setDisable(true);
		countText.setPrefWidth(60);

		textStart = new TextField();
		textStart.setPrefWidth(110);
		textStart.setEditable(false);
		textStart.setOnMouseClicked(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				selText = textStart;
				fxCalendar.setValue(startCal.getTime());
				popup.show(textStart, event.getScreenX(), event.getScreenY());
			};
		});

		textEnd = new TextField();
		textEnd.setPrefWidth(110);
		textEnd.setEditable(false);
		textEnd.setOnMouseClicked(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				selText = textEnd;
				fxCalendar.setValue(endCal.getTime());
				popup.show(textEnd, event.getScreenX(), event.getScreenY());
			};
		});

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

		Region spacer2 = new Region();
		spacer2.setPrefWidth(10);

		// getItems().addAll(undoButton, tb1, tb2, tb3, redoButton,
		// reloadButton,
		// spacer, dateLabel, spacer2, countLabel);
		getItems().addAll(undoButton, tb1, tb2, tb3, redoButton, reloadButton,
				spacer, textStart, new Label(" ~ "), textEnd, spacer2,
				new Label("共 "), countText, new Label(" 项记录"));

		createPopup();
	}

	private void createPopup() {
		popup = new Popup();
		popup.setAutoHide(true);
		popup.setAutoFix(true);
		popup.setHideOnEscape(true);

		fxCalendar = new FXCalendar(false);
		fxCalendar.setOnDateSelected(new Runnable() {

			@Override
			public void run() {
				popup.hide();
				if (selText != null) {
					selText.setText(dateFormat.format(fxCalendar.getValue()));
					selText.setUserData(fxCalendar.getValue());
					
					group.selectedToggleProperty().removeListener(toggleChangeListener);
					group.selectToggle(null);
					group.selectedToggleProperty().addListener( toggleChangeListener );
					
					//group.selectedToggleProperty().addListener(to)
					if (selText.equals(textStart)) {
						startCal.setTime(fxCalendar.getValue());
						startCal.set(Calendar.HOUR_OF_DAY, 0);
						startCal.set(Calendar.MINUTE, 0);
						startCal.set(Calendar.SECOND, 0);
						startCal.set(Calendar.MILLISECOND, 0);
					} else if (selText.equals(textEnd)) {
						endCal.setTime(fxCalendar.getValue());
						endCal.set(Calendar.HOUR_OF_DAY, 0);
						endCal.set(Calendar.MINUTE, 0);
						endCal.set(Calendar.SECOND, 0);
						endCal.set(Calendar.MILLISECOND, 0);
						endCal.add(Calendar.DATE, 1);// 天数加1,用于设置查询条件
					}

					if (startCal.after(endCal)) {
						// showMessage("起始时间应该在结束时间之前");
					}
				}
			}
		});
		popup.getContent().add(fxCalendar);
	}

	private void reload() {

		if (reload != null) {
			reload.run();
		}
	}

	public void setDate(Date date) {
		if (group.getSelectedToggle() == null) {
			return;
		}

		startCal.setTime(date);
		startCal.set(Calendar.HOUR_OF_DAY, 0);
		startCal.set(Calendar.MINUTE, 0);
		startCal.set(Calendar.SECOND, 0);
		startCal.set(Calendar.MILLISECOND, 0);

		switch (dayRange) {
		case 1:

			endCal = (Calendar) startCal.clone();
			endCal.add(Calendar.DATE, dayRange);

			textStart.setText(dateFormat.format(startCal.getTime()));
			textEnd.setText(dateFormat.format(startCal.getTime()));
			break;

		default:

			endCal = (Calendar) startCal.clone();
			endCal.add(Calendar.DATE, dayRange);

			Calendar end = (Calendar) startCal.clone();
			end.add(Calendar.DATE, dayRange - 1);
			
			textStart.setText(dateFormat.format(startCal.getTime()));
			textEnd.setText(dateFormat.format(end.getTime()));
			break;
		}

		reload();
	}

	public void setInfo(String text) {
		countText.setText(text);
	}

	public Date getStart() {
		return startCal.getTime();
	}

	public Date getEnd() {
		if (endCal == null) {
			return null;
		} else {
			return endCal.getTime();
		}
	}

}
