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; 

    /**
     * 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.length() != 0) {
            this.newName = new Name(name);
        }
        
        if(!tagSet.isEmpty()) {
            this.newTagList = new UniqueTagList(tagSet);
        }
    }
    
    @Override
    public CommandResult execute() {
        UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();
        ReadOnlyTask taskToEdit = lastShownList.get(taskIndex - 1);
        
        if (lastShownList.size() < taskIndex) {
            indicateAttemptToExecuteIncorrectCommand();
            return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }
        
        ReadOnlyTask taskToReplace = new FloatingTask(newName == null ? taskToEdit.getName() : newName
                                                , newTagList == null ? taskToEdit.getTags() : newTagList);
        
        ((ModelManager) model).editFloatingTask(new FloatingTask(taskToReplace), taskIndex - 1);
        
        return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, taskToReplace));
    }
}

@@ -823,18 +823,23 @@
  Command
+ 
 %7B%0A
+    
 %0A    pub
@@ -885,16 +885,20 @@
 %22edit%22;%0A
+    
 %0A    pub
@@ -996,28 +996,16 @@
 imi. %5Cn%22
-%0A           
  + %22Exam
@@ -1010,16 +1010,28 @@
 ample: %22
+%0A           
  + COMMA
@@ -1037,28 +1037,16 @@
 AND_WORD
-%0A           
  + %22 2 b
@@ -1068,16 +1068,20 @@
 12 pm%22;%0A
+    
 %0A    pub
@@ -1254,16 +1254,20 @@
  Jimi%22;%0A
+    
 %0A    pri
@@ -1390,18 +1390,21 @@
 newName;
- 
 %0A
+    
 %0A    /**
@@ -1973,16 +1973,17 @@
       if
+ 
 (name.le
@@ -2046,33 +2046,24 @@
 ;%0A        %7D%0A
-        %0A
         if(!
@@ -2060,16 +2060,17 @@
       if
+ 
 (!tagSet
@@ -2645,16 +2645,33 @@
 ingTask(
+%0A                
 newName 
@@ -2710,16 +2710,17 @@
  newName
+,
 %0A       
@@ -2731,42 +2731,8 @@
     
-                                 ,
  new
@@ -2805,31 +2805,14 @@
     
- ((ModelManager)
  model
-)
 .edi
@@ -2825,16 +2825,31 @@
 ingTask(
+taskIndex - 1, 
 new Floa
@@ -2875,23 +2875,8 @@
 ace)
-, taskIndex - 1
 );%0A 

format and swap parameters