package bakatxt.core;

import java.util.ArrayList;
import java.util.LinkedList;

import bakatxt.gui.BakaUI;

public class BakaProcessor {

    private static Database _database;
    private static BakaParser _parser;
    private static LinkedList<Task> _displayTasks;

    enum CommandType {
        ADD, DELETE, SHOW, DISPLAY, CLEAR, DEFAULT, REMOVE, EDIT, EXIT
    }

    public BakaProcessor() {
        _database = Database.getInstance();
        _parser = new BakaParser();
        _displayTasks = _database.getAllTasks();
    }

    public String addTask(String input) {
        Task toAdd = _parser.add(input);
        _database.add(toAdd);
        String output = toAdd.toDisplayString();
        _displayTasks = _database.getAllTasks();

        return output;
    }

    public void deleteTask(String input) {
        String content = _parser.getString(input).trim();
        ArrayList<Integer> listOfIndex = _parser.getIndexList(content);
        for (int i = 0; i < listOfIndex.size(); i++) {
            int trueIndex = listOfIndex.get(i);
            Task target = _displayTasks.get(trueIndex - 1);
            _database.delete(target);
        }

        _displayTasks = _database.getAllTasks();

    }

    public void displayTask() {
        _displayTasks = _database.getAllTasks();
    }

    public LinkedList<Task> getAllTasks() {
        return _displayTasks;
    }

    public void clearTask() {
        _database.clear();
        _displayTasks = _database.getAllTasks();
    }

    public void editTask(String input) {
        String index = _parser.getString(input).trim();
        int trueIndex = Integer.valueOf(index.trim());
        _displayTasks = _database.getAllTasks();
        Task target = _displayTasks.get(trueIndex - 1);
        _database.delete(target);
        Task toAdd = _parser.add("add" + BakaUI.getInputText());
        _database.add(toAdd);
        _displayTasks = _database.getAllTasks();
    }

    public void exitProg() {
        _database.close();
        System.exit(0);
    }

    public String executeCommand(String input) {

        String command = _parser.getCommand(input);
        CommandType commandType;

        try {
            commandType = CommandType.valueOf(command);
        } catch (IllegalArgumentException e) {
            commandType = CommandType.DEFAULT;
        }

        UserInput inputCmd = null;
        Task task = null;
        switch (commandType) {

            case ADD :
                // output = addTask(input);
                task = _parser.add(input);
                inputCmd = new UserInput(command, task);
                break;

            case REMOVE :
            case DELETE :
                // deleteTask(input);
                String content = _parser.getString(input).trim();
                ArrayList<Integer> listOfIndex = _parser.getIndexList(content);
                for (int i = 0; i < listOfIndex.size(); i++) {
                    int trueIndex = listOfIndex.get(i);
                    task = _displayTasks.get(trueIndex - 1);
                }
                inputCmd = new UserInput(command, task);
                break;

            case SHOW :
            case DISPLAY :
                displayTask();
                break;

            case CLEAR :
                clearTask();
                break;

            case EDIT :
                // editTask(input);
                String index = _parser.getString(input).trim();
                int trueIndex = Integer.valueOf(index.trim());
                _displayTasks = _database.getAllTasks();
                task = _displayTasks.get(trueIndex - 1);
                Task toEdit = _parser.add("add" + BakaUI.getInputText());
                inputCmd = new UserInput(command, task, toEdit);
                break;

            case EXIT :
                break;

            case DEFAULT :
                // addTask(input);
                task = _parser.add("add " + input);
                inputCmd = new UserInput("add", task);
                inputCmd.execute();
                break;

            default :
                break;
        }
        if (inputCmd != null) {
            inputCmd.execute();
            _displayTasks = inputCmd.getDisplayTasks();
            return inputCmd.commandString();
        }
        _displayTasks = _database.getAllTasks();
        return null;
    }
}

@@ -78,36 +78,8 @@
 t;%0A%0A
-import bakatxt.gui.BakaUI;%0A%0A
 publ
@@ -232,16 +232,75 @@
 ayTasks;
+%0A    private static ReverseAction ra = new ReverseAction();
 %0A%0A    en
@@ -1858,32 +1858,8 @@
 add%22
- + BakaUI.getInputText()
 );%0A 
@@ -2593,32 +2593,111 @@
 command, task);%0A
+                ra.execute(inputCmd);%0A                _database.getAllTasks();%0A
                 
@@ -3128,22 +3128,8 @@
     
-            %7D%0A
     
@@ -3173,32 +3173,93 @@
 command, task);%0A
+                    ra.execute(inputCmd);%0A                %7D%0A%0A
                 
@@ -3743,32 +3743,120 @@
 trueIndex - 1);%0A
+                // temporarily set it to null%0A                String newContent = null;%0A
                 
@@ -3893,29 +3893,18 @@
 %22 + 
-BakaUI.getInputText()
+newContent
 );%0A 
@@ -3963,24 +3963,62 @@
 k, toEdit);%0A
+                ra.execute(inputCmd);%0A
             
@@ -4258,32 +4258,26 @@
             
-inputCmd
+ra
 .execute();%0A
@@ -4265,32 +4265,40 @@
      ra.execute(
+inputCmd
 );%0A             
@@ -4367,182 +4367,8 @@
   %7D%0A
-        if (inputCmd != null) %7B%0A            inputCmd.execute();%0A            _displayTasks = inputCmd.getDisplayTasks();%0A            return inputCmd.commandString();%0A        %7D
 %0A   

Update case ADD, DELETE and EDIT with implementation of ReverseAction