package seedu.address.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 seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.ui.TaskPanelSelectionChangedEvent;
import seedu.address.commons.util.FxViewUtil;
import seedu.address.model.task.ReadOnlyTask;

/**
 * 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<ReadOnlyTask> taskListView;

    public TaskListPanel(AnchorPane taskListPlaceholder, ObservableList<ReadOnlyTask> taskList) {
        super(FXML);
        setConnections(taskList);
        addToPlaceholder(taskListPlaceholder);
        setEventHandlerForSelectionChangeEvent();
    }

    public void setConnections(ObservableList<ReadOnlyTask> taskList) {
        taskListView.setItems(taskList);
        taskListView.setCellFactory(listView -> new TaskListViewCell());
    }

    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 TaskPanelSelectionChangedEvent(newValue));
                    }
                });
    }

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

    class TaskListViewCell extends ListCell<ReadOnlyTask> {

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

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

}

@@ -556,16 +556,37 @@
 yTask;%0A%0A
+//@@author A0162877N%0A
 /**%0A * P
@@ -818,68 +818,253 @@
 l%22;%0A
-%0A    @FXML%0A    private ListView%3CReadOnlyTask%3E taskListView;%0A
+    private static final double PERIMETER_DEFAULT_LENGTH = 0.0;%0A%0A    @FXML%0A    private ListView%3CReadOnlyTask%3E taskListView;%0A%0A    /**%0A     * Initialization of the task list panel and%0A     * add the task panel to the task list place holder%0A     */
 %0A   
@@ -1309,32 +1309,95 @@
 Event();%0A    %7D%0A%0A
+    /**%0A     * Set the connections for task list panel%0A     */%0A
     public void 
@@ -1766,90 +1766,321 @@
 t(),
- 0.0, 0.0, 0.0, 0.0);%0A        placeHolderPane.getChildren().add(getRoot());%0A    %7D%0A
+%0A                PERIMETER_DEFAULT_LENGTH,%0A                PERIMETER_DEFAULT_LENGTH,%0A                PERIMETER_DEFAULT_LENGTH,%0A                PERIMETER_DEFAULT_LENGTH);%0A        placeHolderPane.getChildren().add(getRoot());%0A    %7D%0A%0A    /**%0A     * Set the event handler for selection change of the task card%0A     */
 %0A   
@@ -2534,24 +2534,106 @@
  %7D);%0A    %7D%0A%0A
+    /**%0A     * Set the task list to scroll to the index in the task panel%0A     */%0A
     public v

Remove magic number and add comments to methods