package ch.zhaw.gartenverwaltung;

import ch.zhaw.gartenverwaltung.gardenplan.Gardenplanmodel;
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
import ch.zhaw.gartenverwaltung.plantList.PlantListModel;
import ch.zhaw.gartenverwaltung.taskList.TaskListModel;
import ch.zhaw.gartenverwaltung.types.Crop;
import ch.zhaw.gartenverwaltung.types.Task;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
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.net.URL;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

public class MyScheduleController implements Initializable {
    private Crop selectedCrop = null;
    private final TaskListModel taskListModel = new TaskListModel();
    private final Gardenplanmodel gardenplanmodel = new Gardenplanmodel(taskListModel);
    private final PlantListModel plantListModel = new PlantListModel();

    private final ListProperty<Crop> cropListProperty = new SimpleListProperty<>(FXCollections.observableArrayList());

    @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;

    public MyScheduleController() throws IOException {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<Crop> cropList;
        try {
            cropList = gardenplanmodel.getCrops();
            cropListProperty.addAll(cropList);
        } catch (IOException e) {
            e.printStackTrace();
        }
        setCellFactoryListView();
        scheduledPlants_listview.itemsProperty().bind(cropListProperty);
        lookForSelectedListEntries();
        setDayLabels();
        information_label.setText("");
        try {
            loadTaskList();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void lookForSelectedListEntries() {
        scheduledPlants_listview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Crop>() {
            @Override
            public void changed(ObservableValue<? extends Crop> observable, Crop oldValue, Crop 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<Crop>() {
            @Override
            protected void updateItem(Crop crop, boolean empty) {
                super.updateItem(crop, empty);

                if (empty || crop == null) {
                    setText(null);
                } else {
                    try {
                        setText(plantListModel.getFilteredPlantListById(Config.getCurrentHardinessZone(), crop.getPlantId()).get(0).name());
                    } catch (HardinessZoneNotSetException | IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void loadTaskList() throws IOException {
        List<List<Task>> taskLists = new LinkedList<>();
        if (selectedCrop != null) {
            taskLists = taskListModel.getTasksUpcomingWeekForCrop(selectedCrop.getCropId().get());
        } else {
            taskLists = taskListModel.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);
    }


}
