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 persons.
 */
public class TaskListPanel extends UiPart<Region> {
    private final Logger logger = LogsCenter.getLogger(TaskListPanel.class);
    private static final String FXML = "PersonListPanel.fxml";

    @FXML
    private ListView<ReadOnlyEvent> personListView;

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

    private void setConnections(ObservableList<ReadOnlyEvent> observableList) {
        personListView.setItems(observableList);
        personListView.setCellFactory(listView -> new PersonListViewCell());
        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() {
        personListView.getSelectionModel().selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> {
                    if (newValue != null) {
                        logger.fine("Selection in person list panel changed to : '" + newValue + "'");
                        raise(new EventPanelSelectionChangedEvent(newValue));
                    }
                });
    }

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

    class PersonListViewCell extends ListCell<ReadOnlyEvent> {

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

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

}

@@ -579,22 +579,20 @@
 list of 
-person
+task
 s.%0A */%0Ap
@@ -759,22 +759,20 @@
 FXML = %22
-Person
+Task
 ListPane
@@ -827,22 +827,20 @@
 yEvent%3E 
-person
+task
 ListView
@@ -878,22 +878,20 @@
 horPane 
-person
+task
 ListPlac
@@ -1033,22 +1033,20 @@
 eholder(
-person
+task
 ListPlac
@@ -1142,38 +1142,36 @@
 List) %7B%0A        
-person
+task
 ListView.setItem
@@ -1189,38 +1189,36 @@
 eList);%0A        
-person
+task
 ListView.setCell
@@ -1241,22 +1241,20 @@
  -%3E new 
-Person
+Task
 ListView
@@ -1651,38 +1651,36 @@
 ent() %7B%0A        
-person
+task
 ListView.getSele
@@ -1876,22 +1876,20 @@
 tion in 
-person
+task
  list pa
@@ -2130,38 +2130,36 @@
 %3E %7B%0A            
-person
+task
 ListView.scrollT
@@ -2180,22 +2180,20 @@
         
-person
+task
 ListView
@@ -2265,22 +2265,20 @@
   class 
-Person
+Task
 ListView
@@ -2383,22 +2383,20 @@
 lyEvent 
-person
+task
 , boolea
@@ -2435,22 +2435,20 @@
 ateItem(
-person
+task
 , empty)
@@ -2475,22 +2475,20 @@
 mpty %7C%7C 
-person
+task
  == null
@@ -2623,14 +2623,12 @@
 ard(
-person
+task
 , ge

Update TaskListPanel variable to reflect previous change