package jfdi.ui;

import java.util.ArrayList;

import dummy.LogicDummy;
import dummy.LogicInterfaceDummy;
import dummy.TaskDummy;
import jfdi.logic.ControlCenter;
import jfdi.storage.data.TaskAttributes;

public class UserInterface implements IUserInterface {

    private static final String UI_MESSAGE_INIT = "Initializing UI...";
    private static final String UI_MESSAGE_INITED = "Initialization Completed!";
    private static final String UI_MESSAGE_WELCOME = "J.F.D.I.: Hello! :) What can I do for you?";
    private static final String UI_MESSAGE_USERCMD = "You said: %1$s";
    private static final String UI_MESSAGE_RESPONSE = "J.F.D.I.: %1$s";
    private static final String UI_MESSAGE_WARNING = "Warning: %1$s";
    private static final String UI_MESSAGE_QUIT = "Bye Bye! See you next time! :)";

    private static final EventBus eventBus = ControlCenter.getEventBus();
    // PurchaseSubscriber purchaseSubscriber = new PurchaseSubscriber();
    //eventBus.register(purchaseSubscriber);

    LogicInterfaceDummy logic;
    private MainController controller;

    public UserInterface() {
        // what to do for constructor?
    }

    @Override
    public void init() {

        showToUser(UI_MESSAGE_INIT);

        // Initialize Logic
        logic = new LogicDummy();
        logic.init();

        // Error: if fail to get data (logic issue, cannot find storage), query user for filename

        showToUser(UI_MESSAGE_INITED);
    }

    @Override
    public void displayWelcome() {
        // Create and display a default view
        //controller.clearFb();
        showToUser(UI_MESSAGE_WELCOME);

        // display default list
    }

    @Override
    public void processInput(String input) {

        // Clear controller first
        controller.clearCmdArea();
        controller.clearFb();

        // Show user what the command recognized in the feedback area
        controller.displayFb(String.format(UI_MESSAGE_USERCMD, input));

        // Relay user input to logic and wait for reply
        logic.handleInput(input);

        /*// Update UI according to reply from logic
        switch (feedback) {
            case "QUIT":
                doQuit();
                break;
            default:
                doUserCmd();
                break;
        }*/
    }

    @Override
    public ArrayList<TaskDummy> getList() {
        // get list of tasks from storage if command is completed (ArrayList<Task>)
        return null;
    }

    @Override
    public void setController(MainController controller) {
        this.controller = controller;
    }

    /***************************
     *** LEVEL 1 Abstraction ***
     ***************************/

    private void showToUser(String string) {
        System.out.println(string);
    }

    private void doQuit() {
        showToUser(UI_MESSAGE_QUIT);
        System.exit(0);
    }

    private void doUserCmd() {

    }

    /***************************
     *** LEVEL 2 Abstraction ***
     ***************************/

    @Subscribe
    public void handleListDoneEvent(ListDoneEvent e) {
        for (TaskAttributes item : e.getItems()) {
            System.out.println(item.getDescription());
        }
    }

    @Subscribe
    public void handleListFailEvent(ListFailEvent e) {
        switch (e.getError()) {
            case NON_EXISTENT_TAG:
                System.out.println("Supplied tags do not exist in the database!");
                break;
            case UNKNOWN:
                System.out.println("Unknown error occurred.");
        }
    }

    @Subscribe
    public void handleExitCalledEvent(ExitCalledEvent e) {
        System.out.printf("\nMoriturus te saluto.\n");
    }

    @Subscribe
    public void handleInvalidCommandEvent(InvalidCommandEvent e) {
        System.out.printf("Sorry, I do not understand what you mean by \"%s\" :(\n", e.getInputString());
    }

    @Subscribe
    public void handleAddTaskDoneEvent(AddTaskDoneEvent e) {
        TaskAttributes task = e.getTask();
        System.out.printf("Task added: #%d - %s\n", task.getId(), task.getDescription());
    }

    @Subscribe
    public void handleAddTaskFailEvent(AddTaskFailEvent e) {
        switch (e.getError()) {
            case UNKNOWN:
                System.out.println("Some stupid error occurred.");
        }
    }
}

@@ -198,16 +198,18 @@
 butes;%0A%0A
+%0A%0A
 public c
@@ -888,71 +888,52 @@
     
-// PurchaseSubscriber purchaseSubscriber = new PurchaseSubscrib
+CommandHandler cmdHandler = new CommandHandl
 er()
@@ -938,18 +938,16 @@
 ();%0A    
-//
 eventBus
@@ -960,24 +960,16 @@
 ter(
-purchaseSubscrib
+cmdHandl
 er);
@@ -2885,1415 +2885,7 @@
    %7D
-%0A%0A    /***************************%0A     *** LEVEL 2 Abstraction ***%0A     ***************************/%0A%0A    @Subscribe%0A    public void handleListDoneEvent(ListDoneEvent e) %7B%0A        for (TaskAttributes item : e.getItems()) %7B%0A            System.out.println(item.getDescription());%0A        %7D%0A    %7D%0A%0A    @Subscribe%0A    public void handleListFailEvent(ListFailEvent e) %7B%0A        switch (e.getError()) %7B%0A            case NON_EXISTENT_TAG:%0A                System.out.println(%22Supplied tags do not exist in the database!%22);%0A                break;%0A            case UNKNOWN:%0A                System.out.println(%22Unknown error occurred.%22);%0A        %7D%0A    %7D%0A%0A    @Subscribe%0A    public void handleExitCalledEvent(ExitCalledEvent e) %7B%0A        System.out.printf(%22%5CnMoriturus te saluto.%5Cn%22);%0A    %7D%0A%0A    @Subscribe%0A    public void handleInvalidCommandEvent(InvalidCommandEvent e) %7B%0A        System.out.printf(%22Sorry, I do not understand what you mean by %5C%22%25s%5C%22 :(%5Cn%22, e.getInputString());%0A    %7D%0A%0A    @Subscribe%0A    public void handleAddTaskDoneEvent(AddTaskDoneEvent e) %7B%0A        TaskAttributes task = e.getTask();%0A        System.out.printf(%22Task added: #%25d - %25s%5Cn%22, task.getId(), task.getDescription());%0A    %7D%0A%0A    @Subscribe%0A    public void handleAddTaskFailEvent(AddTaskFailEvent e) %7B%0A        switch (e.getError()) %7B%0A            case UNKNOWN:%0A                System.out.println(%22Some stupid error occurred.%22);%0A        %7D%0A    %7D
 %0A%7D%0A

Change minor error