package ch.zhaw.gartenverwaltung;

import ch.zhaw.gartenverwaltung.types.GrowthPhaseType;
import ch.zhaw.gartenverwaltung.types.Plant;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.util.Callback;

import java.time.LocalDate;

public class SelectSowDayController {
    private Plant selectedPlant;

    @FXML
    private DatePicker datepicker;

    @FXML
    private RadioButton harvest_radio;
    @FXML
    private RadioButton sow_radio;
    @FXML
    public ToggleGroup phase_group;

    /**
     * if sow date radio button was selected return sow date
     * if sow date was not selected get sow from harvest day and return sow date
     * @return {@link LocalDate} of the sow date
     */
    public LocalDate retrieveResult() {
        LocalDate sowDate = datepicker.getValue();
        if (harvest_radio.isSelected()) {
            sowDate = selectedPlant.sowDateFromHarvestDate(sowDate);
        }
        return sowDate;
    }

    /**
     * Set the {@link Plant} for which a date should be selected.
     *
     * @param plant Plant
     */
    public void setSelectedPlant(Plant plant) {
        selectedPlant = plant;
    }

    /**
     * add listener and set default values
     */
    @FXML
    public void initialize() {
        clearDatePickerEntries();

        Callback<DatePicker, DateCell> dayCellFactory = getDayCellFactory();
        datepicker.setDayCellFactory(dayCellFactory);
        datepicker.setEditable(false);

        sow_radio.setUserData(GrowthPhaseType.SOW);
        harvest_radio.setUserData(GrowthPhaseType.HARVEST);
    }

    /**
     * Disable save button when date picker is empty
     * @param saveButton {@link Button} to be disabled
     */
    public void initSaveButton(Button saveButton) {
        saveButton.disableProperty().bind(datepicker.valueProperty().isNull());
    }

    /**
     * clear date picker editor when radio button is changed
     */
    private void clearDatePickerEntries() {
        harvest_radio.selectedProperty().addListener((observable, oldValue, isNowSelected) -> datepicker.setValue(null));
    }

    /**
     * date picker disable/enable dates according to selected plant: sow or harvest day
     *
     * @return cellFactory of datePicker
     */
    private Callback<DatePicker, DateCell> getDayCellFactory() {

        return (datePicker) -> new DateCell() {
            private final LocalDate today = LocalDate.now();

            @Override
            public void updateItem(LocalDate item, boolean empty) {
                super.updateItem(item, empty);
                setDisable(true);
                setStyle("-fx-background-color: #ffc0cb;");

                if (item.compareTo(today) > 0 && (!harvest_radio.isSelected() || selectedPlant.sowDateFromHarvestDate(item).compareTo(today) >= 0)) {
                    GrowthPhaseType selectedPhase = (GrowthPhaseType) phase_group.getSelectedToggle().getUserData();

                    if (selectedPlant.isDateInPhase(item, selectedPhase)) {
                        setDisable(false);
                        setStyle("-fx-background-color: #32CD32;");
                    }
                }
            }
        };
    }
}
