package seedu.tasklist.ui;

import java.util.logging.Logger;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import seedu.tasklist.commons.core.LogsCenter;
import seedu.tasklist.commons.events.ui.TaskPanelSelectionChangedEvent;
import seedu.tasklist.model.task.ReadOnlyTask;

public class UpcomingTaskPanel extends UiPart<Region> {
    private final Logger logger = LogsCenter.getLogger(UpcomingTaskPanel.class);
    private static final String FXML = "UpcomingTaskPanel.fxml";

    @FXML
    private ListView<ReadOnlyTask> todayTaskListView;
    @FXML
    private ListView<ReadOnlyTask> tomorrowTaskListView;

    public UpcomingTaskPanel(AnchorPane upcomingTaskPlaceholder, ObservableList<ReadOnlyTask> todayTask,
            ObservableList<ReadOnlyTask> tomorrowTask) {
        super(FXML);
        setTodayListView(todayTask);
        setTomorrowListView(tomorrowTask);
        setEventHandlerForSelectionChangeEvent();
    }

    private void setTodayListView(ObservableList<ReadOnlyTask> todayList) {
        todayTaskListView.setItems(todayList);
    }

    private void setTomorrowListView(ObservableList<ReadOnlyTask> tomorrowList) {
        tomorrowTaskListView.setItems(tomorrowList);
    }

    private void setEventHandlerForSelectionChangeEvent() {
        setEventHandlerForTodaySelectionChangeEvent();
        setEventHandlerForTomorrowSelectionChangeEvent();
    }

    private void setEventHandlerForTodaySelectionChangeEvent() {
        todayTaskListView.getSelectionModel().selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> {
                    if (newValue != null) {
                        logger.fine("Selection in today list panel changed to : '" + newValue + "'");
                        raise(new TaskPanelSelectionChangedEvent(newValue));
                    }
                });
    }

    private void setEventHandlerForTomorrowSelectionChangeEvent() {
        tomorrowTaskListView.getSelectionModel().selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> {
                    if (newValue != null) {
                        logger.fine("Selection in tomorrow list panel changed to : '" + newValue + "'");
                        raise(new TaskPanelSelectionChangedEvent(newValue));
                    }
                });
    }

    public void todayScrollTo(int index) {
        Platform.runLater(() -> {
            todayTaskListView.scrollTo(index);
            todayTaskListView.getSelectionModel().clearAndSelect(index);
        });
    }

    public void tomorrowScrollTo(int index) {
        Platform.runLater(() -> {
            tomorrowTaskListView.scrollTo(index);
            tomorrowTaskListView.getSelectionModel().clearAndSelect(index);
        });
    }
}

@@ -158,16 +158,54 @@
 l.FXML;%0A
+import javafx.scene.control.ListCell;%0A
 import j
@@ -2976,11 +2976,448 @@
 );%0A    %7D
+%0A%0A    class UpcomingTaskViewCell extends ListCell%3CReadOnlyTask%3E %7B%0A%0A        @Override%0A        protected void updateItem(ReadOnlyTask task, boolean empty) %7B%0A            super.updateItem(task, empty);%0A%0A            if (empty %7C%7C task == null) %7B%0A                setGraphic(null);%0A                setText(null);%0A            %7D else %7B%0A                setGraphic(new UpcomingTaskCard(task, getIndex() + 1).getRoot());%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

add TaskListViewCell subclass to UpcomingTaskPanel