/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 12/05/2016 : SCA : V2.2
 * Creation 
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.searchComets;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import eu.schudt.javafx.controls.calendar.DatePicker;
import fr.tlse.irap.pt.client.component.DialogComponent;
import fr.tlse.irap.pt.client.component.IDialogComponent;
import fr.tlse.irap.pt.client.controller.common.Formatter;
import fr.tlse.irap.pt.client.controller.common.Item;
import fr.tlse.irap.pt.client.controller.common.MapMenu;
import fr.tlse.irap.pt.client.controller.dialogbox.DialogBox;
import fr.tlse.irap.spaceElement.SpaceElement;
import fr.tlse.irap.spaceElement.Sun;
import fr.tlse.irap.utils.CoordConverter;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.MenuButton;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;

public class SearchCometsComponent extends AnchorPane implements Initializable, IDialogComponent, ISearchComets {

	private DialogComponent dialog;

	@FXML protected GridPane startTimeGridPane;
	@FXML protected GridPane endTimeGridPane;
    @FXML private TextField distance;
    @FXML private MenuButton centerMenuBtn;
    @FXML private TextField center;
    @FXML private ChoiceBox<String> unitDist;
    @FXML public Button okBtn;

	protected final DatePicker datePicker = new DatePicker("startTime");
	protected final DatePicker datePickerEnd = new DatePicker("endTime");

	private MapMenu centerMenu;
	private Date startDate;
	private SpaceElement startSource = new Sun();
	private long searchTime = 10 * 86400 * 1000; // 10 days

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		//okBtn.setDisable(true);
		// init combo boxes
		unitDist.getItems().clear();
		unitDist.getItems().add("km");
		unitDist.getItems().add("AU");
		// set default values
		unitDist.setValue(unitDist.getItems().get(1));

		// init date picker
		datePicker.selectedDateProperty().set(new Date());
		setStyleDate(datePicker);
		startTimeGridPane.add(datePicker, 0, 0);
		datePickerEnd.selectedDateProperty().set(new Date());
		setStyleDate(datePickerEnd);
		endTimeGridPane.add(datePickerEnd, 0, 0);

		// init context menu
		centerMenu = new MapMenu();
		List<Item> menus = new ArrayList<Item>();
		menus.add(SearchCometsItemBuilder.getSunItem(centerMenu.getMap(), this));
		menus.add(SearchCometsItemBuilder.getPlanetItem(centerMenu.getMap(), this));
		centerMenu.addAll(menus);

		centerMenuBtn.getItems().clear();
		centerMenuBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				event.consume();
				if (event.getButton() == MouseButton.PRIMARY) {
					centerMenu.show(centerMenuBtn, event.getScreenX(), event.getScreenY());
					event.consume();
				}
				if (event.getButton() == MouseButton.SECONDARY) {
					centerMenu.hide();
					event.consume();
				}
			}
		});
		center.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				event.consume();
				if (event.getButton() == MouseButton.PRIMARY) {
					centerMenu.show(centerMenuBtn, event.getScreenX(), event.getScreenY());
					event.consume();
				}
				if (event.getButton() == MouseButton.SECONDARY) {
					centerMenu.hide();
					event.consume();
				}
			}
		});
		
		unitDist.valueProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if (newValue.equals("AU")) {
					distance.setText(Formatter.toDecimal4D(CoordConverter.toAU(Formatter.toDouble(distance.getText()))));
				}
				else {
					distance.setText(Formatter.toDecimal0D(CoordConverter.toKm(Formatter.toDouble(distance.getText()))));
				}
			}
		});
		
	}

	public void initComponentWith(Date pStartDate) {
		// init start date
		this.startDate = pStartDate;
		// init start
		center.setText(startSource.getName());
		initComponent();
		dialog.show();
	}

	private void initComponent() {
		datePicker.selectedDateProperty().set(null);
		datePickerEnd.selectedDateProperty().set(null);
		datePicker.selectedDateProperty().set(startDate);
		datePickerEnd.selectedDateProperty().set(new Date(startDate.getTime() + searchTime));
	}

	public void setDialog(DialogComponent dialog) {
		this.dialog = dialog;
	}

	public DialogComponent getDialog() {
		return dialog;
	}

	public void close() {
		dialog.close();
	}

	@Override
	public void runChoose(SpaceElement spaceElement) {
		startSource = spaceElement;
		center.setText(spaceElement.getName());
		okBtn.setDisable(false);
	}

	/**
	 * get distance in km
	 * @return
	 */
	public long getDistance() {
		if (unitDist.getValue().equals("AU")) {
			return (long) CoordConverter.toKm(Formatter.toDouble(distance.getText()));
		}
		else {
			return Formatter.toDouble(distance.getText()).longValue();
		}
	}

	public String getCenter() {
		return center.getText();
	}

	public SpaceElement getStartSource() {
		return startSource;
	}

	public Date getStartDate() {
		return startDate;
	}

	public Date getEndDate() {
		return datePickerEnd.selectedDateProperty().get();
	}
	
	public boolean checkValues() {
		try {
			long dist = getDistance();
			return true;
		}
		catch (Exception e) {
			DialogBox.warn("invalid values : " + e.getMessage());
		}
		return false;
	}
	
	private static void setStyleDate(DatePicker datePicker) {
		datePicker.getTextField().setMaxWidth(350.0);
		datePicker.getTextField().setPrefWidth(350.0);
		datePicker.getTextField().setMaxHeight(20.0);
		datePicker.getTextField().setPrefHeight(20.0);
		datePicker.autosize();
	}

}
