package seedu.task.model.task;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.task.commons.core.UnmodifiableObservableList;
import seedu.task.commons.exceptions.DuplicateDataException;
import seedu.task.commons.util.CollectionUtil;

/**
 * A list of tasks that enforces uniqueness between its elements and does not allow nulls.
 *
 * Supports a minimal set of list operations.
 *
 * @see Task#equals(Object)
 * @see CollectionUtil#elementsAreUnique(Collection)
 */
public class UniqueTaskList implements Iterable<Task> {

    private final ObservableList<Task> internalList = FXCollections.observableArrayList();

    /**
     * Returns true if the list contains an equivalent task as the given argument.
     */
    public boolean contains(ReadOnlyTask toCheck) {
        assert toCheck != null;
        assert internalList instanceof Collection;
        return internalList.contains(toCheck);
    }

    /**
     * Adds a task to the list.
     *
     * @throws DuplicateTaskException if the task to add is a duplicate of an existing task in the list.
     */
    public void add(Task toAdd) throws DuplicateTaskException {
        assert toAdd != null;
        if (contains(toAdd)) {
            throw new DuplicateTaskException();
        }
        internalList.add(toAdd);
    }

    /**
     * Updates the task in the list at position {@code index} with {@code editedTask}.
     *
     * @throws DuplicateTaskException if updating the task's details causes the task to be equivalent to
     *      another existing task in the list.
     * @throws IndexOutOfBoundsException if {@code index} < 0 or >= the size of the list.
     */
    public void updateTask(int index, ReadOnlyTask editedTask) throws DuplicateTaskException {
        assert editedTask != null;

        Task taskToUpdate = internalList.get(index);
        if (!taskToUpdate.equals(editedTask) && internalList.contains(editedTask)) {
            throw new DuplicateTaskException();
        }

        taskToUpdate.resetData(editedTask);
        // TODO: The code below is just a workaround to notify observers of the updated task.
        // The right way is to implement observable properties in the Task class.
        // Then, TaskCard should then bind its text labels to those observable properties.
        internalList.set(index, taskToUpdate);
    }

    /**
     * Removes the equivalent task from the list.
     *
     * @throws TaskNotFoundException if no such task could be found in the list.
     */
    public boolean remove(ReadOnlyTask toRemove) throws TaskNotFoundException {
        assert toRemove != null;
        final boolean taskFoundAndDeleted = internalList.remove(toRemove);
        if (!taskFoundAndDeleted) {
            throw new TaskNotFoundException();
        }
        return taskFoundAndDeleted;
    }

    public void setTasks(UniqueTaskList replacement) {
        this.internalList.setAll(replacement.internalList);
    }

    public void setTasks(List<? extends ReadOnlyTask> tasks) throws DuplicateTaskException {
        final UniqueTaskList replacement = new UniqueTaskList();
        for (final ReadOnlyTask task : tasks) {
            replacement.add(new Task(task));
        }
        setTasks(replacement);
    }

    public UnmodifiableObservableList<Task> asObservableList() {
        return new UnmodifiableObservableList<>(internalList);
    }

    //@@author A0139938L
    public Task[] asArray() {
        Task[] tasks = new Task[internalList.size()];
        for (int i = 0; i < internalList.size(); i++) {
            tasks[i] = internalList.get(i);
        }
        return tasks;
    }
    //@@author

    @Override
    public Iterator<Task> iterator() {
        return internalList.iterator();
    }

    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof UniqueTaskList // instanceof handles nulls
                && this.internalList.equals(
                ((UniqueTaskList) other).internalList));
    }

    @Override
    public int hashCode() {
        return internalList.hashCode();
    }

    /**
     * Signals that an operation would have violated the 'no duplicates' property of the list.
     */
    public static class DuplicateTaskException extends DuplicateDataException {
        protected DuplicateTaskException() {
            super("Operation would result in duplicate tasks");
        }
    }

    /**
     * Signals that an operation targeting a specified task in the list would fail because
     * there is no such matching task in the list.
     */
    public static class TaskNotFoundException extends Exception {}

    public int getTaskID(Task task) {
        return internalList.indexOf(task);
    }

}


@@ -50,24 +50,76 @@
 Collection;%0A
+import java.util.Comparator;%0Aimport java.util.Date;%0A
 import java.
@@ -129,24 +129,24 @@
 l.Iterator;%0A
-
 import java.
@@ -693,16 +693,34 @@
 le%3CTask%3E
+, Comparator%3CTask%3E
  %7B%0A%0A    
@@ -1471,16 +1471,80 @@
 toAdd);%0A
+        FXCollections.sort(internalList, new UniqueTaskList());%0A
     %7D%0A%0A 
@@ -2576,24 +2576,88 @@
 kToUpdate);%0A
+        FXCollections.sort(internalList, new UniqueTaskList());%0A
     %7D%0A%0A    /
@@ -3078,32 +3078,96 @@
 on();%0A        %7D%0A
+        FXCollections.sort(internalList, new UniqueTaskList());%0A
         return t
@@ -5131,18 +5131,457 @@
 (task);%0A
-
     %7D%0A%0A
+    //@@author A0138664W%0A%09@Override%0A%09public int compare(Task task1, Task task2) %7B%0A%09%09Date task1Date = task1.getEndTime().getValue();%0A%09%09Date task2Date = task2.getEndTime().getValue();%0A%09%09if (task1Date==null %7C%7C task2Date==null)%7B%0A%09%09%09return -1;%0A%09%09%7D%0A%09%09if (task1Date.before(task2Date)) %7B%0A            return -1;%0A        %7D else if (task1Date.after(task2Date)) %7B%0A            return 1;%0A        %7D else %7B%0A            return 0;%0A        %7D%0A%09%7D%0A%09//@@author%0A%0A
 %7D%0A%0A

Implement default Sorting