package seedu.tache.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.tache.commons.core.LogsCenter;
import seedu.tache.commons.events.ui.NewResultAvailableEvent;
import seedu.tache.commons.util.FxViewUtil;
import seedu.tache.logic.Logic;
import seedu.tache.logic.commands.CommandResult;
import seedu.tache.logic.commands.exceptions.CommandException;

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

    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(commandTextField);
        FxViewUtil.applyAnchorBoundaryParameters(getRoot(), 0.0, 0.0, 0.0, 0.0);
        FxViewUtil.applyAnchorBoundaryParameters(commandTextField, 0.0, 0.0, 0.0, 0.0);
    }

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

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

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


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

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

}

@@ -52,16 +52,132 @@
 ogger;%0A%0A
+import org.controlsfx.control.textfield.AutoCompletionBinding;%0Aimport org.controlsfx.control.textfield.TextFields;%0A%0A
 import j
@@ -193,16 +193,16 @@
 l.FXML;%0A
-
 import j
@@ -1130,24 +1130,51 @@
 aceholder);%0A
+        setAutocomplete();%0A
     %7D%0A%0A    p
@@ -2234,24 +2234,24 @@
 essage()));%0A
-
         %7D%0A  
@@ -2255,16 +2255,528 @@
 %0A    %7D%0A%0A
+    /**%0A     * Sets autocomplete functionality for user commands.%0A     * Uses ControlsFX Autocomplete TextField function.%0A     */%0A    private void setAutocomplete() %7B%0A        String%5B%5D possibleCommands = %7B%22add %22, %22clear%22, %22complete %22, %22delete %22, %22edit %22, %22exit%22, %22find %22,%0A                                        %22help%22, %22list%22, %22save %22, %22select %22, %22undo%22 %7D;%0A        AutoCompletionBinding%3CString%3E binding = TextFields.bindAutoCompletion(commandTextField, possibleCommands);%0A        binding.setMaxWidth(70);%0A    %7D%0A%0A
 %0A    /**

Implement autocomplete functionality for user commands