package seedu.taskman.ui;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import seedu.taskman.commons.core.LogsCenter;
import seedu.taskman.logic.commands.AddCommand;
import seedu.taskman.logic.commands.AddECommand;
import seedu.taskman.logic.commands.ClearCommand;
import seedu.taskman.logic.commands.CompleteCommand;
import seedu.taskman.logic.commands.DeleteCommand;
import seedu.taskman.logic.commands.EditCommand;
import seedu.taskman.logic.commands.ExitCommand;
import seedu.taskman.logic.commands.HelpCommand;
import seedu.taskman.logic.commands.HistoryCommand;
import seedu.taskman.logic.commands.ListCommand;
import seedu.taskman.logic.commands.SelectCommand;
import seedu.taskman.logic.commands.StoragelocCommand;
import seedu.taskman.logic.commands.TagsCommand;
import seedu.taskman.logic.commands.UndoCommand;

import java.util.logging.Logger;

/**
 * Controller for a help page
 */
public class HelpWindow extends UiPart {

    private static final Logger logger = LogsCenter.getLogger(HelpWindow.class);
    private static final String FXML = "HelpWindow.fxml";
    final ObservableList<HelpTableViewRow> data = FXCollections.observableArrayList(
                                                  new HelpTableViewRow(AddCommand.COMMAND_WORD, AddCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(AddECommand.COMMAND_WORD, AddECommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(ClearCommand.COMMAND_WORD, ClearCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(CompleteCommand.COMMAND_WORD, CompleteCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(DeleteCommand.COMMAND_WORD, DeleteCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(EditCommand.COMMAND_WORD, EditCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(ExitCommand.COMMAND_WORD, ExitCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(HelpCommand.COMMAND_WORD, HelpCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(HistoryCommand.COMMAND_WORD, HistoryCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(ListCommand.COMMAND_WORD, ListCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(SelectCommand.COMMAND_WORD, SelectCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(StoragelocCommand.COMMAND_WORD, StoragelocCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(TagsCommand.COMMAND_WORD, TagsCommand.MESSAGE_USAGE),
                                                  new HelpTableViewRow(UndoCommand.COMMAND_WORD, UndoCommand.MESSAGE_USAGE));


    private VBox mainPane;
    private Scene previousScene;
    private Scene helpScene;
    
    @FXML
    private TableView<HelpTableViewRow> helpTableView;
    
    @FXML
    private TableColumn<HelpTableViewRow, String> commandColumn;
    
    @FXML
    private TableColumn<HelpTableViewRow, String> formatColumn;

    public static HelpWindow load(Stage primaryStage, Scene mainScene) {
        logger.fine("Showing help page about the application.");
        HelpWindow helpWindow = UiPartLoader.loadUiPart(primaryStage, new HelpWindow());
        helpWindow.configure(mainScene);
        return helpWindow;
    }

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

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

    private void configure(Scene mainScene) {
        previousScene = mainScene;
        helpScene = new Scene(mainPane);
        primaryStage.setScene(helpScene);
        addKeyPressedFilters();
        initTable();
    }
    
    private void addKeyPressedFilters() {
        helpScene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
            KeyCode code = event.getCode();
            if (code.equals(KeyCode.ESCAPE)) {
                handleBack();
            }
        });
    }
    
    private void initTable() {
        commandColumn.setCellValueFactory(
                      new PropertyValueFactory<HelpTableViewRow,String>("command"));
        formatColumn.setCellValueFactory(
                     new PropertyValueFactory<HelpTableViewRow,String>("format"));
        helpTableView.setItems(data);
    }

    public Scene getScene() {
        return helpScene;
    }
    
    public void handleBack() {
        primaryStage.setScene(previousScene);
    }
}
@@ -1231,16 +1231,37 @@
 ogger;%0A%0A
+//@@author A0140136W%0A
 /**%0A * C
@@ -1281,20 +1281,22 @@
  a help 
-page
+window
 %0A */%0Apub
@@ -1473,16 +1473,84 @@
 .fxml%22;%0A
+    %0A    // A list of every command word and its respective format.%0A
     fina
@@ -4196,24 +4196,187 @@
 XML;%0A    %7D%0A%0A
+    /* Creates a new scene that contains layout for help window.%0A    * Stores the previous scene to go back to.%0A    * Adds listener for keyboard shortcuts.%0A    */%0A
     private 
@@ -4401,32 +4401,32 @@
 ne mainScene) %7B%0A
-
         previous
@@ -4583,32 +4583,83 @@
 e();%0A    %7D%0A    %0A
+    // Listener and filter for keyboard shortcuts.%0A
     private void
@@ -4904,24 +4904,105 @@
 %0A    %7D%0A    %0A
+    // Initialize the help table with every command and its respective formats. %0A
     private 
@@ -5314,24 +5314,52 @@
 ta);%0A    %7D%0A%0A
+    // Gets the help scene.%0A
     public S
@@ -5404,29 +5404,78 @@
 cene;%0A    %7D%0A
-
     %0A
+    // Handles going back to the previous scene.%0A
     public v

Refactor help window