package seedu.address.ui;

import java.util.logging.Logger;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.util.FxViewUtil;
import seedu.address.logic.Logic;
import seedu.address.logic.commands.CommandException;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.parser.ParseException;

public class CommandBox extends UiPart<Pane> {

    private final Logger logger = LogsCenter.getLogger(CommandBox.class);

    private static final String FXML = "/view/CommandBox.fxml";

    private final ResultDisplay resultDisplay;
    String previousCommandTest;

    private final Logic logic;

    @FXML
    private TextField commandTextField;
    private CommandResult mostRecentResult;

    private OnCommandResultCallback onCommandResultCallback;

    public CommandBox(ResultDisplay resultDisplay, Logic logic) {
        super(FXML);
        this.resultDisplay = resultDisplay;
        this.logic = logic;
        FxViewUtil.applyAnchorBoundaryParameters(getRoot(), 0.0, 0.0, 0.0, 0.0);
        FxViewUtil.applyAnchorBoundaryParameters(commandTextField, 0.0, 0.0, 0.0, 0.0);
    }

    public void setOnCommandResult(OnCommandResultCallback callback) {
        this.onCommandResultCallback = callback;
    }

    /**
     * Focus on the command box text field.
     */
    public void requestFocus() {
        commandTextField.requestFocus();
    }

    @FXML
    private void handleCommandInputChanged() {
        //Take a copy of the command text
        previousCommandTest = commandTextField.getText();

        /* We assume the command is correct. If it is incorrect, the command box will be changed accordingly
         * in the event handling code {@link #handleIncorrectCommandAttempted}
         */
        setStyleToIndicateCorrectCommand();
        try {
            mostRecentResult = logic.execute(previousCommandTest);
            resultDisplay.postMessage(mostRecentResult.feedbackToUser);
            logger.info("Result: " + mostRecentResult.feedbackToUser);
            if (onCommandResultCallback != null) {
                onCommandResultCallback.call(mostRecentResult);
            }
        } catch (ParseException | CommandException e) {
            setStyleToIndicateIncorrectCommand();
            restoreCommandText();
            resultDisplay.postMessage(e.getMessage());
        }
    }

    /**
     * Sets the command box style to indicate a correct command.
     */
    private void setStyleToIndicateCorrectCommand() {
        commandTextField.getStyleClass().remove("error");
        commandTextField.setText("");
    }

    /**
     * Restores the command box text to the previously entered command
     */
    private void restoreCommandText() {
        commandTextField.setText(previousCommandTest);
    }

    /**
     * Sets the command box style to indicate an error
     */
    private void setStyleToIndicateIncorrectCommand() {
        commandTextField.getStyleClass().add("error");
    }

    public interface OnCommandResultCallback {
        void call(CommandResult result);
    }

}

@@ -480,17 +480,16 @@
 Pane%3E %7B%0A
-%0A
     priv
@@ -670,40 +670,8 @@
 lay;
-%0A    String previousCommandTest;
 %0A%0A  
@@ -753,52 +753,8 @@
 eld;
-%0A    private CommandResult mostRecentResult;
 %0A%0A  
@@ -1485,72 +1485,69 @@
     
-//Take a copy of the command text%0A        previous
+try %7B%0A            final 
 Command
-Test = 
+Result result = logic.execute(
 comm
@@ -1568,18 +1568,18 @@
 etText()
+)
 ;
-%0A
 %0A       
@@ -1583,322 +1583,58 @@
     
-/* We assume the command is correct. If it is incorrect, the command box will be changed accordingly%0A         * in the event handling code %7B@link #handleIncorrectCommandAttempted%7D%0A         */%0A        setStyleToIndicateCorrectCommand();%0A        try %7B%0A            mostRecentResult = logic.execute(previousCommandTest
+    logger.info(%22Result: %22 + result.feedbackToUser
 );%0A 
@@ -1670,27 +1670,17 @@
 Message(
-mostRecentR
+r
 esult.fe
@@ -1710,64 +1710,41 @@
     
-logger.info(%22Result: %22 + mostRecentResult.feedbackToUser
+setStyleToIndicateCorrectCommand(
 );%0A 
@@ -1842,19 +1842,9 @@
 all(
-mostRecentR
+r
 esul
@@ -1913,24 +1913,76 @@
 eption e) %7B%0A
+            logger.info(%22Result: %22 + e.toString());%0A
             
@@ -2023,42 +2023,8 @@
 ();%0A
-            restoreCommandText();%0A
     
@@ -2333,197 +2333,8 @@
  %7D%0A%0A
-    /**%0A     * Restores the command box text to the previously entered command%0A     */%0A    private void restoreCommandText() %7B%0A        commandTextField.setText(previousCommandTest);%0A    %7D%0A%0A
     

simplify code