package seedu.jimi.logic.commands;

import java.util.HashSet;
import java.util.Set;

import javafx.collections.transformation.FilteredList;
import seedu.jimi.commons.core.Messages;
import seedu.jimi.commons.core.UnmodifiableObservableList;
import seedu.jimi.commons.exceptions.IllegalValueException;
import seedu.jimi.model.ModelManager;
import seedu.jimi.model.tag.Tag;
import seedu.jimi.model.tag.UniqueTagList;
import seedu.jimi.model.task.FloatingTask;
import seedu.jimi.model.task.Name;
import seedu.jimi.model.task.ReadOnlyTask;
import seedu.jimi.model.task.UniqueTaskList;
import seedu.jimi.model.task.UniqueTaskList.DuplicateTaskException;
import seedu.jimi.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * 
 * @author zexuan
 *
 * Edits an existing task/event in Jimi.
 */
public class EditCommand extends Command{

    public static final String COMMAND_WORD = "edit";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits an existing task/event in Jimi. \n"
            + "Example: " + COMMAND_WORD
            + " 2 by 10th July at 12 pm";

    public static final String MESSAGE_EDIT_TASK_SUCCESS = "Updated task details: %1$s";
    public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in Jimi";

    private final int taskIndex; //index of task/event to be edited
    private UniqueTagList newTagList;
    private Name newName; 
    private UnmodifiableObservableList<ReadOnlyTask> lastShownList;

    /**
     * Convenience constructor using raw values.
     * //TODO: change to support FloatingTask, Task and Events types as well
     * 
     * @throws IllegalValueException if any of the raw values are invalid
     */
    public EditCommand(String name, Set<String> tags, int taskIndex) throws IllegalValueException {
        final Set<Tag> tagSet = new HashSet<>();
        for (String tagName : tags) {
            tagSet.add(new Tag(tagName));
        }
        this.taskIndex = taskIndex;
        
        lastShownList = model.getFilteredTaskList();
        ReadOnlyTask taskToEdit = lastShownList.get(taskIndex - 1);
        
        //if new fields are to be edited, instantiate them, else set them to the oldName
        if(name.length() != 0) {
            this.newName = new Name(name);
        }
        else {
            this.newName = taskToEdit.getName(); //assigns old task name
        }
        
        if(!tagSet.isEmpty()) {
            this.newTagList = new UniqueTagList(tagSet);
        }
        else {
            this.newTagList = taskToEdit.getTags(); //assigns old tag list
        }
    }
    @Override
    public CommandResult execute() {
        if (lastShownList.size() < taskIndex) {
            indicateAttemptToExecuteIncorrectCommand();
            return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }
        
        ReadOnlyTask taskToReplace = new FloatingTask(newName, newTagList);
        
        ((ModelManager) model).editFloatingTask(new FloatingTask(taskToReplace), taskIndex - 1);
        
        return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, taskToReplace));
    }
}

@@ -1390,76 +1390,8 @@
 me; 
-%0A    private UnmodifiableObservableList%3CReadOnlyTask%3E lastShownList;
 %0A%0A  
@@ -1900,138 +1900,8 @@
    %0A
-        lastShownList = model.getFilteredTaskList();%0A        ReadOnlyTask taskToEdit = lastShownList.get(taskIndex - 1);%0A        %0A
     
@@ -1958,38 +1958,8 @@
 them
-, else set them to the oldName
 %0A   
@@ -2045,106 +2045,8 @@
   %7D%0A
-        else %7B%0A            this.newName = taskToEdit.getName(); //assigns old task name%0A        %7D%0A
     
@@ -2153,114 +2153,19 @@
   %7D%0A
-        else %7B%0A            this.newTagList = taskToEdit.getTags(); //assigns old tag list%0A        %7D%0A
     %7D%0A
+    %0A
     
@@ -2207,24 +2207,195 @@
 execute() %7B%0A
+        UnmodifiableObservableList%3CReadOnlyTask%3E lastShownList = model.getFilteredTaskList();%0A        ReadOnlyTask taskToEdit = lastShownList.get(taskIndex - 1);%0A        %0A
         if (
@@ -2651,17 +2651,151 @@
 (newName
-,
+ == null ? taskToEdit.getName() : newName%0A                                                , newTagList == null ? taskToEdit.getTags() :
  newTagL

Fix EditCommand not retrieving TaskList