package seedu.jimi.logic.commands;

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

import seedu.jimi.commons.core.Messages;
import seedu.jimi.commons.core.UnmodifiableObservableList;
import seedu.jimi.commons.exceptions.IllegalValueException;
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; 

    /**
     * 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;
        
        //if new fields are to be edited, instantiate them
        if(name != null) {
            this.newName = new Name(name);
        }
        if(tagSet != null) {
            this.newTagList = new UniqueTagList(tagSet);
        }
    }
    
    @Override
    public CommandResult execute() {
        UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

        if (lastShownList.size() < taskIndex) {
            indicateAttemptToExecuteIncorrectCommand();
            return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

        ReadOnlyTask taskToEdit = lastShownList.get(taskIndex - 1);
        
        try {
            model.deleteTask(taskToEdit); //delete 
            
            UniqueTagList oldTagList = taskToEdit.getTags();
            Name oldName = taskToEdit.getName();
            
            if(newName != null && !oldName.equals(newName)){
                if(newTagList != null && !oldTagList.equals(newTagList)){
                    model.addFloatingTask(new FloatingTask(newName, newTagList)); //change both name and tags
                }
                model.addFloatingTask(new FloatingTask(newName, oldTagList)); //change only name
            }
            else {
                model.addFloatingTask(new FloatingTask(oldName, oldTagList)); //change nothing //TODO: reduce redundancy
            }
        } catch (TaskNotFoundException pnfe) {
            assert false : "The target task cannot be missing";
        } catch (UniqueTaskList.DuplicateTaskException e) {
            return new CommandResult(MESSAGE_DUPLICATE_TASK);
        }
        
        return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, lastShownList.get(lastShownList.size()-1)));
    }

}

@@ -78,16 +78,71 @@
 l.Set;%0A%0A
+import javafx.collections.transformation.FilteredList;%0A
 import s
@@ -285,32 +285,70 @@
 ValueException;%0A
+import seedu.jimi.model.ModelManager;%0A
 import seedu.jim
@@ -1954,16 +1954,46 @@
 ate them
+, else set them to the oldName
 %0A       
@@ -2056,32 +2056,34 @@
 name);%0A        %7D
+ %0A
 %0A        if(tagS
@@ -2237,43 +2237,29 @@
     
-UnmodifiableObservableList%3CReadOnly
+FilteredList%3CFloating
 Task
@@ -2279,26 +2279,45 @@
 st =
+ ((ModelManager)
  model
+)
 .get
-Filtered
+Modifiable
 Task
@@ -2533,24 +2533,24 @@
         
-ReadOnly
+Floating
 Task tas
@@ -2614,117 +2614,41 @@
     
-try %7B%0A            model.deleteTask(taskToEdit); //delete %0A            %0A            UniqueTagList oldTagList =
+if(newName != null) %7B%0A           
  tas
@@ -2659,65 +2659,23 @@
 dit.
-getTags();%0A            Name oldName = taskToEdit.get
+setName(new
 Name
-(
 );%0A 
@@ -2677,30 +2677,26 @@
 e);%0A        
-    %0A 
+%7D%0A
            i
@@ -2698,254 +2698,95 @@
     
-if(newName != null && !oldName.equals(newName))%7B%0A                if(newTagList != null && !oldTagList.equals(newTagList))%7B%0A                    model.addFloatingTask(new FloatingTask(newName, newTagList)); //change both name and tags%0A     
+ %0A        if(newTagList != null) %7B%0A            taskToEdit.setTags(newTagList);%0A
         
    %7D
@@ -2773,35 +2773,32 @@
 gList);%0A        
-   
 %7D%0A              
@@ -2787,32 +2787,33 @@
   %7D%0A        
+%0A
         
 model.addFlo
@@ -2804,499 +2804,75 @@
     
-m
+((M
 odel
-.addFloatingTask(new FloatingTask(newName, oldTagList)); //change only name%0A            %7D%0A            else %7B%0A                model.addFloatingTask(new FloatingTask(oldName, oldTagList)); //change nothing //TODO: reduce redundancy%0A            %7D%0A        %7D catch (TaskNotFoundException pnfe) %7B%0A            assert false : %22The target task cannot be missing%22;%0A        %7D catch (UniqueTaskList.DuplicateTaskException e) %7B%0A            return new CommandResult(MESSAGE_DUPLICATE_TASK);%0A        %7D
+Manager) model).editFloatingTask(taskToEdit, taskIndex - 1);
 %0A   
@@ -2955,49 +2955,18 @@
 SS, 
-lastShownList.get(lastShownList.size()-1)
+taskToEdit
 ));%0A

Modify EditCommand to increase cohesion