/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * Creation 
 * Version : 24/03/2016 : SCA : V2.2 (PSWS-H2020)
 * Update result date property on date change if slider is disable 
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.timestamp;

import java.net.URL;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;

import eu.schudt.javafx.controls.calendar.DatePicker;
import fr.tlse.irap.pt.client.component.CustomComponent;
import fr.tlse.irap.pt.client.component.ICustomComponent;
import fr.tlse.irap.pt.client.controller.common.Formatter;
import fr.tlse.irap.pt.client.controller.common.Style;
import fr.tlse.irap.pt.client.controller.dialogbox.DialogBox;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class TimestampController extends GridPane implements
		Initializable, ICustomComponent {

	@FXML private TimestampController rootTimestamp;
	@FXML private TextField resultTField;
	@FXML private ScrollBar resultTScrollBar;
	@FXML protected final DatePicker datePicker = new DatePicker("startTime");

	private TextField startTimeField = new TextField();
	private ObjectProperty<Date> resultDate = new SimpleObjectProperty<Date>();

	public TimestampController() {
		super();
		CustomComponent.initCustomComponent(this);
		datePicker.selectedDateProperty().set(new Date());
	}

	@Override
	public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
		setUpDatePicker();
		rootTimestamp.autosize();
		resultTScrollBar.setDisable(true);
	}

	public Date getSelectedDate() {
		return datePicker.selectedDateProperty().get();
	}

	public void setSelectedDate(Date date) {
		datePicker.selectedDateProperty().set(date);
		datePicker.refresh();
	}

	protected void setUpDatePicker() {
		bindPropertyDatePicker();
		resizeDatePicker();
	}

	public ObjectProperty<Date> selectedDateProperty() {
		return datePicker.selectedDateProperty();
	}

	protected void bindPropertyDatePicker() {
		datePicker.selectedDateProperty().addListener(new InvalidationListener() {
			public void invalidated(Observable observable) {
				setStartTime(getSelectedDate());
				setResultTime(getSelectedDate());
				if (resultTScrollBar.isDisable()) {
					updateResultDate();
				}
			}
		});
	}

	public void initTimeScrollBar(double minValue, double maxValue) {
		resultTScrollBar.setValue(0);
		resultTScrollBar.setMin(minValue);
		resultTScrollBar.setMax(maxValue);
	}

	public static boolean isValidateDate(Date date) {
		if (date == null) {
			return false;
		}
		return true;
	}

	public void disableDatesPickers() {
		datePicker.setDisable(true);
	}

	public void enableDatesPickers() {
		datePicker.setDisable(false);
	}

	protected void resizeDatePicker() {
		setStyleDatePicker(datePicker);
		rootTimestamp.add(datePicker, 0, 1);
	}

	private void setStyleDatePicker(DatePicker datePicker) {
		datePicker.getStyleClass().add(Style.LION_DEFAULT_DATEPICKER);
		datePicker.setPadding(new Insets(5.0));
		datePicker.setAlignment(Pos.CENTER);
		datePicker.getTextField().setMaxWidth(350.0);
		datePicker.getTextField().setPrefWidth(350.0);
		// datePicker.autosize();
	}

	public void hideComponent() {
		rootTimestamp.setVisible(false);
	}

	public StringProperty getStartTimeProperty() {
		return this.startTimeField.textProperty();
	}

	public void setStartTime(Date newDate) {
		if (isValidateDate(newDate)) {
			startTimeField.setText(Formatter.getSdf().format(newDate));
		} else {
			startTimeField.setText("");
		}
	}

	public Calendar getStartTime() {
		Calendar calendarT = Calendar.getInstance();
		try {
			calendarT.setTime(Formatter.getSdf().parse(startTimeField.getText()));
		} catch (ParseException e) {
			DialogBox.error(" Error while parsing time in Time stamp component.");
		}
		calendarT.set(Calendar.MILLISECOND, 0);
		return calendarT;
	}

	public Date getStartDate() {
		return getStartTime().getTime();
	}

	public StringProperty getResultTimeProperty() {
		return this.resultTField.textProperty();
	}

	public void setResultTime(Date newDate) {
		if (isValidateDate(newDate)) {
			resultTField.setText(Formatter.getSdf().format(newDate));
			Style.setWarning(resultTField, false);
		} else {
			// set t with a warning message
			resultTField.setText("");
		}
	}

	public Calendar getResultTime() {
		Calendar calendarT = Calendar.getInstance();
		try {
			calendarT.setTime(Formatter.getSdf().parse(resultTField.getText()));
		} catch (ParseException e) {
			DialogBox.error(" Error while parsing time in Time stamp component.");
		}
		calendarT.set(Calendar.MILLISECOND, 0);
		return calendarT;
	}

	public Date getResultDate() {
		return getResultTime().getTime();
	}

	public ScrollBar getResultTScrollBar() {
		return resultTScrollBar;
	}

	public void setResultTScrollBar(ScrollBar resultTScrollBar) {
		this.resultTScrollBar = resultTScrollBar;
	}

	public void updateResultDate() {
		resultDate.set(getResultDate());
	}

	public ObjectProperty<Date> getResultDateProperty() {
		return resultDate;
	}


}
