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

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.task.ReadOnlyRecurringTask;
import seedu.address.model.task.ReadOnlyTask;
import seedu.address.model.task.ReadOnlyTask.RecurringProperty;
import seedu.address.model.task.Task;
import seedu.address.model.task.UniqueTaskList.DuplicateTaskException;
import seedu.address.model.task.UniqueTaskList.TaskNotFoundException;

/**
 * Edits the details of an existing recurring task in Dueue.
 */
public class EditNextCommand extends EditCommand {

  ///@@author A0143409J
    private ReadOnlyTask finishedOnceTask;

    /**
     * Create editNext command using an index for specific task and description for the edited task
     */
    public EditNextCommand(int filteredTaskListIndex, EditTaskDescriptor editTaskDescriptor) {
        super(filteredTaskListIndex, editTaskDescriptor);
        finishedOnceTask = null;
    }

    public EditNextCommand(ReadOnlyTask task, Task oldTask, ReadOnlyTask finishedOnceTask) {
        super(task, oldTask);
        this.finishedOnceTask = finishedOnceTask;
    }

    @Override
    public CommandResult execute() throws CommandException {
        List<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

        if (filteredTaskListIndex >= lastShownList.size()) {
            throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
        }

        ReadOnlyTask taskToEdit = lastShownList.get(filteredTaskListIndex);
        if (!(taskToEdit instanceof ReadOnlyRecurringTask)) {
            throw new CommandException("The task to edit once should be a recurring task.");
        }
        oldTask = new Task(lastShownList.get(filteredTaskListIndex));

        try {
            Task newTask = createEditedTask(taskToEdit, editTaskDescriptor);
            newTask.setRecurringProperty(RecurringProperty.NON_RECURRING);
            task = newTask;
            model.addTask(newTask);
            model.finishTaskOnce(taskToEdit);
            finishedOnceTask = taskToEdit;
        } catch (IllegalValueException e) {
            throw new CommandException(e.getMessage());
        }

        model.updateFilteredListToShowAllUnfinishedTasks();
        return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, taskToEdit.getName()));
    }

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

    @Override
    public CommandResult executeUndo(String message) throws CommandException {
        assert model != null;
        try {
            model.deleteTask(task);
            model.addTask(oldTask);
            model.deleteTask(finishedOnceTask);
        } catch (TaskNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DuplicateTaskException e) {
            e.printStackTrace();
        }
        model.updateFilteredListToShowAllUnfinishedTasks();
        isSuccess = true;
        return new CommandResult(CommandFormatter.undoMessageFormatter(message, getUndoCommandWord()));
    }

    @Override
    public Command getUndoCommand() {
        if (isSuccess) {
            //TODO
            return new EditNextCommand(task, oldTask, finishedOnceTask);
        } else {
            return new IncorrectCommand(null);
        }
    }

    @Override
    public String getUndoCommandWord() {
        return COMMAND_WORD + COMMAND_SUFFIX;
    }

}

@@ -415,16 +415,63 @@
 operty;%0A
+import seedu.address.model.task.RecurringTask;%0A
 import s
@@ -496,24 +496,24 @@
 .task.Task;%0A
-
 import seedu
@@ -1890,16 +1890,25 @@
 k = new 
+Recurring
 Task(las
@@ -2136,20 +2136,30 @@
 sk = new
+ 
 Task
+(newTask)
 ;%0A      
@@ -2265,26 +2265,80 @@
 eTask = 
-taskToEdit
+new RecurringTask(taskToEdit);%0A            this.isSuccess = true
 ;%0A      
@@ -2812,32 +2812,80 @@
 ;%0A        try %7B%0A
+            if (finishedOnceTask != null) %7B%0A    
             mode
@@ -2908,32 +2908,36 @@
 k);%0A            
+    
 model.addTask(ol
@@ -2936,32 +2936,36 @@
 dTask(oldTask);%0A
+    
             mode
@@ -2988,32 +2988,580 @@
 ishedOnceTask);%0A
+                Task temp = new Task(task);%0A                this.task = new RecurringTask(oldTask);%0A                this.oldTask = temp;%0A                this.finishedOnceTask = null;%0A                this.isSuccess = true;%0A            %7D else %7B%0A                model.addTask(oldTask);%0A                this.oldTask = new RecurringTask(task);%0A                model.finishTaskOnce(task);%0A                this.task = new Task(oldTask);%0A                this.finishedOnceTask = new RecurringTask(task);%0A                this.isSuccess = true;%0A            %7D%0A
         %7D catch 

support undo for editNextCommand tried to support redo but unsuccessful