package seedu.address.model.task;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.exceptions.DuplicateDataException;

import java.util.*;

/**
 * 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> {

    /**
     * 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 {}

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

    /**
     * Constructs empty TaskList.
     */
    public UniqueTaskList() {}

    /**
     * Returns true if the list contains an equivalent task as the given argument.
     */
    public boolean contains(ReadOnlyTask toCheck) {
        assert toCheck != null;
        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 a task to the list.
     * 
     * @throws DuplicateTaskException if the task to add is a duplicate of an existing task in the list.
     */
    public void update(ReadOnlyTask taskToUpdate, Task toUpdate) throws DuplicateTaskException {
        assert toUpdate != null;

        if(contains(toUpdate))
        {
            throw new DuplicateTaskException();
        }
        
        int index = internalList.indexOf(taskToUpdate);
        
        assert index >= 0;
        
        internalList.set(index, toUpdate);
    }
    
    /**
     * 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 ObservableList<Task> getInternalList() {
        return internalList;
    }

    @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();
    }
}

@@ -2074,20 +2074,16 @@
 lyTask t
-askT
 oUpdate,
@@ -2088,24 +2088,27 @@
 e, Task 
-toU
+u
 pdate
+dTask
 ) throws
@@ -2165,16 +2165,52 @@
 != null;
+%0A        assert updatedTask != null;
 %0A%0A      
@@ -2223,24 +2223,27 @@
 ontains(
-toU
+u
 pdate
+dTask
 ))%0A     
@@ -2360,12 +2360,8 @@
 Of(t
-askT
 oUpd
@@ -2443,24 +2443,27 @@
 (index, 
-toU
+u
 pdate
+dTask
 );%0A    %7D

Refactor update method variable naming