//@@author A0138474X
package seedu.address.logic.commands;

import java.util.Iterator;
import java.util.Set;

/**
 * Lists all persons in the address book to the user.
 */
public class ListCommand extends Command {

    public static final String COMMAND_WORD = "list";

    public static final String LIST_ALL = "all";
    public static final String LIST_FINISHED = "finished";
    public static final String LIST_FAVORITE = "favorite";

    public static final String MESSAGE_LIST_SUCCESS = "Listed unfinished tasks";
    public static final String MESSAGE_LIST_ALL_SUCCESS = "Listed all tasks";
    public static final String MESSAGE_LIST_ALL_LIST_SUCCESS = "Listed all tasks in the list";
    public static final String MESSAGE_LIST_FINISHED_SUCCESS = "Listed all finished tasks";
    public static final String MESSAGE_LIST_FAVORITE_SUCCESS = "Listed all favorite tasks";
    public static final String MESSAGE_LIST_DOES_NOT_EXIST = "Given list name does not exist";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": List tasks as per the parameters\n"
            + "the specified listname and displays them as a list with index numbers.\n"
            + "Parameters: [all/favorite/finished] [LIST_NAME]\n"
            + "Example: " + COMMAND_WORD + "\n"
            + COMMAND_WORD + " all study\n"
            + COMMAND_WORD + " finished\n"
            + COMMAND_WORD + "study work\n";

    private static final String LIST_SEPARATOR = ", ";

    private final Set<String> keywords;

    public ListCommand(Set<String> keywords) {
        this.keywords = keywords;
    }

    public ListCommand() {
        this.keywords = null;
    }

    @Override
    public CommandResult execute() {
        if (keywords == null) {
            model.updateFilteredListToShowAllUnfinishedTasks();
            return new CommandResult(MESSAGE_LIST_SUCCESS);
        } else if (keywords.contains(LIST_ALL)) {
            keywords.remove(LIST_ALL);
            if (model.isListExist(keywords)) {
                model.updateFilteredTaskListAll(keywords);
                return new CommandResult(formatter(MESSAGE_LIST_ALL_SUCCESS, keywords));
            } else {
                model.updateFilteredListToShowAllTasks();
                return new CommandResult(MESSAGE_LIST_ALL_SUCCESS);
            }
        } else if (keywords.contains(LIST_FINISHED)) {
            keywords.remove(LIST_FINISHED);
            if (model.isListExist(keywords)) {
                model.updateFilteredTaskListFinished(keywords);
                return new CommandResult(formatter(MESSAGE_LIST_FINISHED_SUCCESS, keywords));
            } else {
                model.updateFilteredListToShowAllFinishedTasks();
                return new CommandResult(MESSAGE_LIST_FINISHED_SUCCESS);
            }
        } else if (keywords.contains(LIST_FAVORITE)) {
            keywords.remove(LIST_FAVORITE);
            if (model.isListExist(keywords)) {
                model.updateFilteredTaskListFavorite(keywords);
                return new CommandResult(formatter(MESSAGE_LIST_FAVORITE_SUCCESS, keywords));
            } else {
                model.updateFilteredListToShowAllFavoriteTasks();
                return new CommandResult(MESSAGE_LIST_FAVORITE_SUCCESS);
            }
        } else if (!model.isListExist(keywords)) {
            return new CommandResult(MESSAGE_LIST_DOES_NOT_EXIST);
        } else {
            model.updateFilteredTaskListGivenListName(keywords);
            return new CommandResult(formatter(MESSAGE_LIST_SUCCESS, keywords));
        }
    }

//@@author

    private String formatter(String message, Set<String> keywords) {
        String formatted = message + " in list ";
        for (Iterator<String> it = keywords.iterator(); it.hasNext(); ) {
            formatted += it.next();
            if (it.hasNext()) {
                formatted += LIST_SEPARATOR;
            }
        }
        return formatted;
    }

    @Override
    public boolean isUndoable() {
        return false;
    }
}

@@ -1968,34 +1968,25 @@
 if (
-model.isListExist(keywords
+keywords.isEmpty(
 )) %7B
@@ -2026,28 +2026,27 @@
 ered
-TaskListAll(keywords
+ListToShowAllTasks(
 );%0A 
@@ -2081,34 +2081,24 @@
 mmandResult(
-formatter(
 MESSAGE_LIST
@@ -2105,35 +2105,24 @@
 _ALL_SUCCESS
-, keywords)
 );%0A         
@@ -2122,32 +2122,65 @@
           %7D else
+ if (model.isListExist(keywords))
  %7B%0A             
@@ -2202,35 +2202,49 @@
 Filtered
-ListToShowAllTasks(
+TaskListGivenListNameAll(keywords
 );%0A     
@@ -2271,32 +2271,42 @@
 w CommandResult(
+formatter(
 MESSAGE_LIST_ALL
@@ -2305,32 +2305,135 @@
 LIST_ALL_SUCCESS
+, keywords));%0A            %7D else %7B%0A                return new CommandResult(MESSAGE_LIST_DOES_NOT_EXIST
 );%0A            %7D
@@ -2552,34 +2552,25 @@
 if (
-model.isListExist(keywords
+keywords.isEmpty(
 )) %7B
@@ -2610,33 +2610,35 @@
 ered
-TaskListFinished(keywords
+ListToShowAllFinishedTasks(
 );%0A 
@@ -2673,34 +2673,24 @@
 mmandResult(
-formatter(
 MESSAGE_LIST
@@ -2702,35 +2702,24 @@
 SHED_SUCCESS
-, keywords)
 );%0A         
@@ -2719,32 +2719,65 @@
           %7D else
+ if (model.isListExist(keywords))
  %7B%0A             
@@ -2803,35 +2803,33 @@
 ered
-ListToShowAllFinishedTasks(
+TaskListFinished(keywords
 );%0A 
@@ -2860,32 +2860,42 @@
 w CommandResult(
+formatter(
 MESSAGE_LIST_FIN
@@ -2899,32 +2899,135 @@
 FINISHED_SUCCESS
+, keywords));%0A            %7D else %7B%0A                return new CommandResult(MESSAGE_LIST_DOES_NOT_EXIST
 );%0A            %7D
@@ -3137,24 +3137,208 @@
          if 
+(keywords.isEmpty()) %7B%0A                model.updateFilteredListToShowAllFavoriteTasks();%0A                return new CommandResult(MESSAGE_LIST_FAVORITE_SUCCESS);%0A            %7D else if 
 (model.isLis
@@ -3540,74 +3540,8 @@
 e %7B%0A
-                model.updateFilteredListToShowAllFavoriteTasks();%0A
     
@@ -3590,32 +3590,30 @@
 GE_LIST_
-FAVORITE_SUCCESS
+DOES_NOT_EXIST
 );%0A     

Fix listcommand bug