package harmony.mastermind.ui;

import javafx.scene.Node;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Popup;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

//@@author A0139194X
public class HelpPopup extends UiPart {

    private static final String FXML = "HelpPopup.fxml";
    private final int DEFAULT_X_POS = 200;
    private final int DEFAULT_Y_POS = 100;
    private final int DEFAULT_HEIGHT = 800;
    private final int DEFAULT_WIDTH = 1000;
    private Popup popup;
    private TextArea content;
    private boolean isFirstKey;
    private TableView<HelpPopupEntry> table;
    
    TableColumn<HelpPopupEntry, String> commandCol;
    TableColumn<HelpPopupEntry, String> formatCol;
    TableColumn<HelpPopupEntry, String> usageCol;
    
    //@@author A0139194X
    public HelpPopup() {
        initTable();
        initPopup();
        isFirstKey = true;
    }

    //@@author A0139194X
    public void show(Node node) {
        
        popup.show(node, DEFAULT_X_POS, DEFAULT_Y_POS);
        popup.centerOnScreen();
    }

    //@@author A0139194X
    @Override
    public void setNode(Node node) {
    }

    //@@author A0139194X
    @Override
    public String getFxmlPath() {
        return FXML;
    }
    
    //@@author A0139194X
    @FXML
    private void initPopup() {
        popup = new Popup();
        content = new TextArea();
        //properties();

        //popup.getContent().add(content);
        popup.getContent().add(table);
        popup.addEventHandler(KeyEvent.KEY_RELEASED, keyEventHandler);

        //content.setEditable(false);
    }
    
    @FXML
    private void initTable() {
        
        table = new TableView<HelpPopupEntry>();
        table.setEditable(false);
        
        initCommandCol();
        initFormatCol();
        initUsageCol();
        
        table.setItems(getList());
        table.getColumns().setAll(commandCol, formatCol, usageCol);
    }
    
    private void initCommandCol() {
        commandCol = new TableColumn<HelpPopupEntry, String>("Command");
        commandCol.setMinWidth(100);
        commandCol.setCellValueFactory(new PropertyValueFactory<>("commandWord"));
    }
    
    private void initFormatCol() {
        formatCol = new TableColumn<HelpPopupEntry, String>("Format");
        formatCol.setMinWidth(200);
        formatCol.setCellValueFactory(new PropertyValueFactory<>("format"));
    }
    
    private void initUsageCol() {
        usageCol = new TableColumn<HelpPopupEntry, String>("Usage");
        usageCol.setMinWidth(200);
        usageCol.setCellValueFactory(new PropertyValueFactory<>("usage"));
    }
    
    private ObservableList<HelpPopupEntry> getList() {
        ObservableList<HelpPopupEntry> entries = FXCollections.observableArrayList();
        entries.add(new HelpPopupEntry("help", "help", "help"));
        entries.add(new HelpPopupEntry("exit", "exit", "exit"));
        
        return entries;
    }
    

    //@@author A0139194X
    @FXML
    EventHandler<KeyEvent> keyEventHandler = new EventHandler<KeyEvent>() {
        public void handle(KeyEvent event) {
            if (!isFirstKey && event.getCode() != null) {
                popup.hide();
            }
            isFirstKey = !isFirstKey;
        }
    };

    //@@author A0139194X
    public void setContent(String text) {
        content.setText(text);
    }
    
    //@@author A0143378Y
    public void properties() { 
        //Setting up the width and height
        content.setPrefHeight(DEFAULT_HEIGHT);
        content.setPrefWidth(DEFAULT_WIDTH);
        
        //Setting up wrapping of text in the content box 
        content.setWrapText(true);
        
        //Setting up the background, font and borders
        content.setStyle("-fx-background-color: #00BFFF;-fx-padding:10px;"
                + "-fx-text-fill: #000080;"+ "-fx-font-family: Consolas;"
                + "-fx-alignment: center"
                );

//        content.setStyle("-fx-font-family: sample; -fx-font-size: 20;");
    }
    
    //@@author A0139194X
    class HelpPopupEntry {
        private String commandWord;
        private String format;
        private String usage;
        
        public HelpPopupEntry(String commandWord, String format, String usage) {
            this.setCommandWord(commandWord);
            this.setFormat(format);
            this.setUsage(usage);
        }

        public String getFormat() {
            return format;
        }

        public void setFormat(String format) {
            this.format = format;
        }

        public String getCommandWord() {
            return commandWord;
        }

        public void setCommandWord(String commandWord) {
            this.commandWord = commandWord;
        }

        public String getUsage() {
            return usage;
        }

        public void setUsage(String usage) {
            this.usage = usage;
        }
    }
}

@@ -750,16 +750,341 @@
 .fxml%22;%0A
+    private final String COMMAND_COL_HEADER = %22Command%22;%0A    private final String FORMAT_COL_HEADER = %22Format%22;%0A    private final String USAGE_COL_HEADER = %22Usage%22;%0A    %0A    private final int COMMAND_COL_MIN_WIDTH = 100;%0A    private final int FORMAT_COL_MIN_WIDTH = 200;%0A    private final int USAGE_COL_MIN_WIDTH = 200;%0A    %0A
     priv
@@ -2815,25 +2815,34 @@
 String%3E(
-%22Command%22
+COMMAND_COL_HEADER
 );%0A     
@@ -2867,19 +2867,37 @@
 inWidth(
-100
+COMMAND_COL_MIN_WIDTH
 );%0A     
@@ -3080,24 +3080,33 @@
 String%3E(
-%22Format%22
+FORMAT_COL_HEADER
 );%0A     
@@ -3122,35 +3122,52 @@
 Col.setMinWidth(
-200
+FORMAT_COL_MIN_WIDTH
 );%0A        forma
@@ -3334,23 +3334,32 @@
 String%3E(
-%22Usage%22
+USAGE_COL_HEADER
 );%0A     
@@ -3382,19 +3382,35 @@
 inWidth(
-200
+USAGE_COL_MIN_WIDTH
 );%0A     

improve code quality