package seedu.todo.ui.components;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import seedu.todo.commons.util.FxViewUtil;
import seedu.todo.ui.InputHandler;
import seedu.todo.ui.UiPartLoader;

public class ConsoleInput extends Component {

    private static final String FXML_PATH = "components/ConsoleInput.fxml";
    private static final String INVALID_COMMAND_RESPONSE = "Invalid command!";

    // Props
    public String consoleOutput;
    private String lastCommandEntered;
    
    // Input handler
    private InputHandler inputHandler = new InputHandler();
    
    // Components
    Console console;
    
    // FXML
    @FXML
    private TextField consoleInputTextField;
    @FXML
    private Pane consolePlaceholder;

    public static ConsoleInput load(Stage primaryStage, Pane placeholderPane) {
        return UiPartLoader.loadUiPart(primaryStage, placeholderPane, new ConsoleInput());
    }

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

    @Override
    public void componentDidMount() {
        // Makes ConsoleInput full width wrt parent container.
        FxViewUtil.makeFullWidth(this.mainNode);
        
        // Load Console.
        loadConsole();
    }
    
    private void loadConsole() {
        console = Console.load(primaryStage, consolePlaceholder);
        console.consoleOutput = consoleOutput;
        console.render();
    }

    /** ================ ACTION HANDLERS ================== **/
    @FXML
    public void handleConsoleInputChanged() {
        lastCommandEntered = consoleInputTextField.getText();
        boolean isValidCommand = inputHandler.processInput(lastCommandEntered);
        
        if (!isValidCommand) {
            console.consoleOutput = INVALID_COMMAND_RESPONSE;
            console.render();
        } else {
            consoleInputTextField.clear();
        }
    }
}

@@ -1772,24 +1772,72 @@
 dCommand) %7B%0A
+            // Show invalid response in Console%0A
             
@@ -1933,16 +1933,168 @@
  else %7B%0A
+            // Remove console output%0A            console.consoleOutput = %22%22;%0A            console.render();%0A            %0A            // Clear input text%0A
         

Clear invalid response if command is valid