package ch.zhaw.gartenverwaltung;

import ch.zhaw.gartenverwaltung.bootstrap.AfterInject;
import ch.zhaw.gartenverwaltung.bootstrap.Inject;
import ch.zhaw.gartenverwaltung.io.PlantList;
import ch.zhaw.gartenverwaltung.models.Garden;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.models.GardenSchedule;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Plant;
import ch.zhaw.gartenverwaltung.types.Task;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyScheduleController {
    private static final Logger LOG = Logger.getLogger(MyScheduleController.class.getName());

    private Crop selectedCrop = null;

    @Inject
    private GardenSchedule gardenSchedule;
    @Inject
    private Garden garden;
    @Inject
    private PlantList plantList;

    @FXML
    private Label day1_label;

    @FXML
    private Pane day1_pane;

    @FXML
    private Label day2_label;

    @FXML
    private Pane day2_pane;

    @FXML
    private Label day3_label;

    @FXML
    private Pane day3_pane;

    @FXML
    private Label day4_label;

    @FXML
    private Pane day4_pane;

    @FXML
    private Label day5_label;

    @FXML
    private Pane day5_pane;

    @FXML
    private Label day6_label;

    @FXML
    private Pane day6_pane;

    @FXML
    private Label day7_label;

    @FXML
    private Pane day7_pane;

    @FXML
    private Label information_label;

    @FXML
    private ListView<Crop> scheduledPlants_listview;

    @AfterInject
    @SuppressWarnings("unused")
    public void init() {
        setCellFactoryListView();
        scheduledPlants_listview.itemsProperty().bind(garden.getPlantedCrops());
        lookForSelectedListEntries();
        setDayLabels();
        information_label.setText("");
        try {
            loadTaskList();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void lookForSelectedListEntries() {
        scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            selectedCrop = newValue;
            try {
                loadTaskList();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    private void setDayLabels() {
        LocalDate today = LocalDate.now();
        day1_label.setText(today.getDayOfWeek().toString());
        day2_label.setText(today.plusDays(1).getDayOfWeek().toString());
        day3_label.setText(today.plusDays(2).getDayOfWeek().toString());
        day4_label.setText(today.plusDays(3).getDayOfWeek().toString());
        day5_label.setText(today.plusDays(4).getDayOfWeek().toString());
        day6_label.setText(today.plusDays(5).getDayOfWeek().toString());
        day7_label.setText(today.plusDays(6).getDayOfWeek().toString());
    }

    private void setCellFactoryListView() {
        scheduledPlants_listview.setCellFactory(param -> new ListCell<>() {
            @Override
            protected void updateItem(Crop crop, boolean empty) {
                super.updateItem(crop, empty);

                if (empty || crop == null) {
                    setText(null);
                } else {
                    try {
                        String text = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId())
                                .map(Plant::name)
                                .orElse("");
                        setText(text);
                    } catch (HardinessZoneNotSetException | IOException e) {
                        LOG.log(Level.WARNING, "Could not get plant for Cell", e);
                    }
                }
            }
        });
    }

    private void loadTaskList() throws IOException {
        List<List<Task>> taskLists;
        if (selectedCrop != null) {
            taskLists = gardenSchedule.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get());
        } else {
            taskLists = gardenSchedule.getTasksUpcomingWeek();
        }
        if (!taskLists.isEmpty()) {
            viewTaskListOfDay(day1_pane, taskLists.get(0));
            viewTaskListOfDay(day2_pane, taskLists.get(1));
            viewTaskListOfDay(day3_pane, taskLists.get(2));
            viewTaskListOfDay(day4_pane, taskLists.get(3));
            viewTaskListOfDay(day5_pane, taskLists.get(4));
            viewTaskListOfDay(day6_pane, taskLists.get(5));
            viewTaskListOfDay(day7_pane, taskLists.get(6));
        }
    }

    private void viewTaskListOfDay(Pane pane, List<Task> tasks) {
        //ToDo update pane with task list
        VBox vBox = new VBox();
        for (Task task : tasks) {
            Label label = new Label(task.getDescription());
            vBox.getChildren().add(label);
        }
        pane.getChildren().add(vBox);
    }


}
