package t16b4.yats.ui;

import java.util.logging.Logger;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import t16b4.yats.commons.core.LogsCenter;
import t16b4.yats.commons.events.ui.EventPanelSelectionChangedEvent;
import t16b4.yats.commons.util.FxViewUtil;
import t16b4.yats.model.item.ReadOnlyEvent;

/**
 * Panel containing the list of tasks.
 */
public class TaskListPanel extends UiPart<Region> {
    private final Logger logger = LogsCenter.getLogger(TaskListPanel.class);
    private static final String FXML = "TaskListPanel.fxml";

    @FXML
    private ListView<ReadOnlyEvent> taskListView;

    public TaskListPanel(AnchorPane taskListPlaceholder, ObservableList<ReadOnlyEvent> observableList) {
        super(FXML);
        setConnections(observableList);
        addToPlaceholder(taskListPlaceholder);
    }

    private void setConnections(ObservableList<ReadOnlyEvent> observableList) {
        taskListView.setItems(observableList);
        taskListView.setCellFactory(listView -> new TaskListViewCell());
        setEventHandlerForSelectionChangeEvent();
    }

    private void addToPlaceholder(AnchorPane placeHolderPane) {
        SplitPane.setResizableWithParent(placeHolderPane, false);
        FxViewUtil.applyAnchorBoundaryParameters(getRoot(), 0.0, 0.0, 0.0, 0.0);
        placeHolderPane.getChildren().add(getRoot());
    }

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

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

    class TaskListViewCell extends ListCell<ReadOnlyEvent> {

        @Override
        protected void updateItem(ReadOnlyEvent task, boolean empty) {
            super.updateItem(task, empty);

            if (empty || task == null) {
                setGraphic(null);
                setText(null);
            } else {
                setGraphic(new PersonCard(task, getIndex() + 1).getRoot());
            }
        }
    }

}

@@ -579,20 +579,22 @@
 list of 
-task
+person
 s.%0A */%0Ap
@@ -761,20 +761,22 @@
 FXML = %22
-Task
+Person
 ListPane
@@ -831,20 +831,22 @@
 yEvent%3E 
-task
+person
 ListView
@@ -884,20 +884,22 @@
 horPane 
-task
+person
 ListPlac
@@ -1041,20 +1041,22 @@
 eholder(
-task
+person
 ListPlac
@@ -1152,36 +1152,38 @@
 List) %7B%0A        
-task
+person
 ListView.setItem
@@ -1201,36 +1201,38 @@
 eList);%0A        
-task
+person
 ListView.setCell
@@ -1255,20 +1255,22 @@
  -%3E new 
-Task
+Person
 ListView
@@ -1667,36 +1667,38 @@
 ent() %7B%0A        
-task
+person
 ListView.getSele
@@ -1894,20 +1894,22 @@
 tion in 
-task
+person
  list pa
@@ -2150,36 +2150,38 @@
 %3E %7B%0A            
-task
+person
 ListView.scrollT
@@ -2202,20 +2202,22 @@
         
-task
+person
 ListView
@@ -2285,28 +2285,30 @@
 %0A%0A    class 
-Task
+Person
 ListViewCell
@@ -2409,20 +2409,22 @@
 lyEvent 
-task
+person
 , boolea
@@ -2463,20 +2463,22 @@
 ateItem(
-task
+person
 , empty)
@@ -2505,20 +2505,22 @@
 mpty %7C%7C 
-task
+person
  == null
@@ -2655,12 +2655,14 @@
 ard(
-task
+person
 , ge

Revert "Update TaskListPanel variable to reflect previous change"