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();
        }
    }
}

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) {
            // Show invalid response in Console
            console.consoleOutput = INVALID_COMMAND_RESPONSE;
            console.render();
        } else {
            // Remove console output
            console.consoleOutput = "";
            console.render();
            
            // Clear input text
            consoleInputTextField.clear();
        }
    }
}

Clear invalid response if command is valid