package seedu.address.ui;

import java.util.HashMap;
import java.util.logging.Logger;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Group;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Region;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.FxViewUtil;
import seedu.address.model.task.ReadOnlyTask;
import seedu.address.model.task.Status;

/**
 * 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";

    private final String groupNames[] = {
        Status.FLOATING, Status.OVERDUE, Status.TODAY, Status.TOMORROW,
        Status.THIS_WEEK, Status.IN_FUTURE
    };

    private HashMap<String, ObservableList<ReadOnlyTask>> taskListMap;

    @FXML
    private VBox taskListView;

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

    /**
     * Groups tasks based on task status.
     *
     * A hashMap maps from groupName to corresponding task lists.
     */
    private void initTaskListsByStatus(ObservableList<ReadOnlyTask> taskList) {
        taskListMap = new HashMap<String, ObservableList<ReadOnlyTask>>();
        for (String groupName : groupNames) {
            taskListMap.put(groupName, FXCollections.observableArrayList());
        }
        for (ReadOnlyTask task : taskList) {
            String taskStatus = task.getStatus().toString();
            for (String groupName : groupNames) {
                if (groupName.equals(taskStatus)) {
                    taskListMap.get(groupName).add(task);
                    break;
                }
            }
        }
    }

    /**
     * Instantiate TaskGroupPanel objects and add them to taskListView.
     */
    private void initTaskGroups() {
        for (String groupName : groupNames) {
            TaskGroupPanel taskGroupPanel = new TaskGroupPanel(groupName, taskListMap.get(groupName));
            taskListView.getChildren().add(taskGroupPanel.getRoot());
        }
    }

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

}

@@ -84,44 +84,8 @@
 r;%0A%0A
-import javafx.application.Platform;%0A
 impo
@@ -147,30 +147,34 @@
 ections.
-Observabl
+ListChang
 eList
+ener
 ;%0Aimport
@@ -185,120 +185,112 @@
 afx.
-fxml.FXML;%0Aimport javafx.scene.Group;%0Aimport javafx.scene.control.ListCell;%0Aimport javafx.scene.control.ListView
+collections.ListChangeListener.Change;%0Aimport javafx.collections.ObservableList;%0Aimport javafx.fxml.FXML
 ;%0Aim
@@ -369,41 +369,8 @@
 ne;%0A
-import javafx.scene.layout.VBox;%0A
 impo
@@ -400,16 +400,49 @@
 Region;%0A
+import javafx.scene.layout.VBox;%0A
 import s
@@ -1361,16 +1361,324 @@
 holder);
+%0A%0A        // When taskList changes, update everything%0A        taskList.addListener(new ListChangeListener%3CReadOnlyTask%3E() %7B%0A%0A            public void onChanged(Change%3C? extends ReadOnlyTask%3E change) %7B%0A                initTaskListsByStatus(taskList);%0A                initTaskGroups();%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A
@@ -2558,24 +2558,68 @@
 kGroups() %7B%0A
+        taskListView.getChildren().clear();%0A
         for 

Fix task list not updated in UI