package seedu.doit.ui;

import java.util.logging.Logger;

import javafx.fxml.FXML;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import seedu.doit.commons.core.LogsCenter;
import seedu.doit.commons.events.ui.NewResultAvailableEvent;
import seedu.doit.commons.util.FxViewUtil;
import seedu.doit.logic.Logic;
import seedu.doit.logic.commands.CommandResult;
import seedu.doit.logic.commands.exceptions.CommandException;

public class CommandBox extends UiPart<Region> {
    public static final String ERROR_STYLE_CLASS = "error";
    private static final String FXML = "CommandBox.fxml";
    private final Logger logger = LogsCenter.getLogger(CommandBox.class);
    private final Logic logic;

    @FXML
    private TextField commandTextField;

    public CommandBox(AnchorPane commandBoxPlaceholder, Logic logic) {
        super(FXML);
        this.logic = logic;
        addToPlaceholder(commandBoxPlaceholder);
    }

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

    @FXML
    private void handleCommandInputChanged() {
        try {
            CommandResult commandResult = this.logic.execute(this.commandTextField.getText());

            // process result of the command
            setStyleToIndicateCommandSuccess();
            this.commandTextField.setText("");
            this.logger.info("Result: " + commandResult.feedbackToUser);
            raise(new NewResultAvailableEvent(commandResult.feedbackToUser));

        } catch (CommandException e) {
            // handle command failure
            setStyleToIndicateCommandFailure();
            this.logger.info("Invalid command: " + this.commandTextField.getText());
            raise(new NewResultAvailableEvent(e.getMessage()));
        }
    }


    /**
     * Sets the command box style to indicate a successful command.
     */
    private void setStyleToIndicateCommandSuccess() {
        this.commandTextField.getStyleClass().remove(ERROR_STYLE_CLASS);
    }

    /**
     * Sets the command box style to indicate a failed command.
     */
    private void setStyleToIndicateCommandFailure() {
        this.commandTextField.getStyleClass().add(ERROR_STYLE_CLASS);
    }

}

@@ -51,16 +51,62 @@
 ogger;%0A%0A
+import com.google.common.eventbus.Subscribe;%0A%0A
 import j
@@ -122,16 +122,16 @@
 l.FXML;%0A
-
 import j
@@ -200,16 +200,52 @@
 tField;%0A
+import javafx.scene.input.KeyEvent;%0A
 import j
@@ -1752,33 +1752,21 @@
     
-this.commandTextField.set
+setCommandBox
 Text
@@ -2214,17 +2214,16 @@
 %0A    %7D%0A%0A
-%0A
     /**%0A
@@ -2636,17 +2636,560 @@
 CLASS);%0A
-
     %7D%0A%0A
+    // @@A0138909R%0A    /**%0A     * sets the text in the command box%0A     */%0A    public void setCommandBoxText(String text) %7B%0A        this.commandTextField.setText(text);%0A    %7D%0A%0A    @Subscribe%0A    /**%0A     * Listens to keyEvents when command Box is focused%0A     */%0A    public void handleKeyPress(KeyEvent event) %7B%0A        switch (event.getCode()) %7B%0A        case UP:%0A            // up arrow%0A%0A            break;%0A        case DOWN:%0A            // down arrow%0A%0A            break;%0A        default:%0A            break;%0A        %7D%0A    %7D%0A%0A    // @@author%0A%0A
 %7D%0A

Add KeyEvent for up and down keys