package tars.ui;

import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
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.stage.Stage;
import tars.model.task.rsv.RsvTask;

/**
 * UI Controller for panel containing the list of reserved tasks.
 * 
 * @@author A0121533W
 */
public class RsvTaskListPanel extends UiPart {
    private static final String FXML = "RsvTaskListPanel.fxml";
    private VBox panel;
    private AnchorPane placeHolderPane;
    private static final int START_INDEX = 1;

    @FXML
    private ListView<RsvTask> rsvTaskListView;

    public RsvTaskListPanel() {
        super();
    }

    @Override
    public void setNode(Node node) {
        panel = (VBox) node;
    }

    @Override
    public String getFxmlPath() {
        return FXML;
    }

    @Override
    public void setPlaceholder(AnchorPane pane) {
        this.placeHolderPane = pane;
    }

    public static RsvTaskListPanel load(Stage primaryStage,
            AnchorPane rsvTaskListPlaceholder,
            ObservableList<RsvTask> rsvTaskList) {
        RsvTaskListPanel rsvTaskListPanel = UiPartLoader.loadUiPart(
                primaryStage, rsvTaskListPlaceholder, new RsvTaskListPanel());
        rsvTaskListPanel.configure(rsvTaskList);
        return rsvTaskListPanel;
    }

    private void configure(ObservableList<RsvTask> rsvTaskList) {
        setConnections(rsvTaskList);
        addToPlaceholder();
    }

    private void setConnections(ObservableList<RsvTask> rsvTaskList) {
        rsvTaskListView.setItems(rsvTaskList);
        rsvTaskListView.setCellFactory(listView -> new RsvTaskListViewCell());
    }

    private void addToPlaceholder() {
        SplitPane.setResizableWithParent(placeHolderPane, false);
        placeHolderPane.getChildren().add(panel);
    }

    public void scrollTo(int index) {
        Platform.runLater(() -> {
            rsvTaskListView.scrollTo(index);
        });
    }

    class RsvTaskListViewCell extends ListCell<RsvTask> {

        public RsvTaskListViewCell() {}

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

            if (empty || task == null) {
                setGraphic(null);
                setText(null);
            } else {
                setGraphic(RsvTaskCard.load(task, getIndex() + START_INDEX)
                        .getLayout());
            }
        }
    }

}

@@ -11,16 +11,96 @@
 rs.ui;%0A%0A
+import java.util.logging.Logger;%0A%0Aimport com.google.common.eventbus.Subscribe;%0A%0A
 import j
@@ -127,16 +127,16 @@
 atform;%0A
-
 import j
@@ -370,24 +370,57 @@
 AnchorPane;%0A
+import javafx.scene.layout.HBox;%0A
 import javaf
@@ -467,16 +467,148 @@
 .Stage;%0A
+import tars.commons.core.LogsCenter;%0Aimport tars.commons.events.ui.RsvTaskAddedEvent;%0Aimport tars.commons.events.ui.TaskAddedEvent;%0A
 import t
@@ -783,16 +783,96 @@
 iPart %7B%0A
+    private static final Logger logger = LogsCenter.getLogger(UiManager.class);%0A
     priv
@@ -2519,47 +2519,151 @@
 %3E %7B%0A
-%0A        public RsvTaskListViewCell() %7B
+        private RsvTask newlyAddedRsvTask;%0A        %0A        public RsvTaskListViewCell() %7B%0A            registerAsAnEventHandler(this);%0A        
 %7D%0A%0A 
@@ -2931,27 +2931,35 @@
         
-setGraphic(
+RsvTaskCard card = 
 RsvTaskC
@@ -2998,16 +2998,17 @@
 T_INDEX)
+;
 %0A       
@@ -3020,44 +3020,700 @@
     
-        .getLayout());%0A            %7D
+HBox layout = card.getLayout();%0A                if (this.newlyAddedRsvTask != null &&%0A                        this.newlyAddedRsvTask.isSameStateAs(task)) %7B%0A                    layout.setStyle(UiColor.TASK_CARD_NEWLY_ADDED_BORDER);%0A                %7D else %7B%0A                    layout.setStyle(UiColor.TASK_CARD_DEFAULT_BORDER);%0A                %7D%0A                setGraphic(layout);%0A            %7D%0A        %7D%0A        %0A        @Subscribe%0A        private void handleRsvTaskAddedEvent(RsvTaskAddedEvent event) %7B%0A            logger.info(LogsCenter.getEventHandlingLogMessage(event,%0A                    %22Updating layout for %22 + event.task.toString()));%0A            this.newlyAddedRsvTask = event.task;
 %0A   

Update UI for rsvTaskAddedEvent