package seedu.unburden.logic.commands;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javafx.collections.ObservableList;
import seedu.unburden.commons.exceptions.IllegalValueException;
import seedu.unburden.model.tag.Tag;
import seedu.unburden.model.tag.UniqueTagList;
import seedu.unburden.model.tag.UniqueTagList.DuplicateTagException;
import seedu.unburden.model.task.*;

/**
 * Adds a person to the address book.
 * @@author A0139678J 
 */

//@@Nathanael Chan A0139678J
public class AddCommand extends Command {

	public static final String COMMAND_WORD = "add";

	public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a task to the list of tasks. \n" 
			+ "Parameters: NAME i/TASKDESCRIPTIONS d/DATE \n" + "s/STARTTIME e/ENDTIME [t/TAG]\n" + "Example: \n"
			+ COMMAND_WORD + " meeting with boss i/ prepare for minutes \n" + "d/23-04-2003 s/1200 e/1300 t/important t/work";

	public static final String MESSAGE_SUCCESS = "New task added: %1$s";
	public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the list of tasks";

	private Task toAdd;

	/**
	 * Convenience constructor using raw values.
	 *
	 * @throws IllegalValueException
	 *             if any of the raw values are invalid
	 */


	public AddCommand(String mode, ArrayList<String> details, Set<String> tags) throws IllegalValueException {
		final Set<Tag> tagSet = new HashSet<>();
		for (String tagName : tags) {
			tagSet.add(new Tag(tagName));
		}

		switch (mode) {
		case "event with everything":
			this.toAdd = new Task(new Name(details.get(0)), new TaskDescription(details.get(1)), new Date(details.get(2)),
					new Time(details.get(3)), new Time(details.get(4)), new UniqueTagList(tagSet));
			break;

		case "event without description":
			this.toAdd = new Task(new Name(details.get(0)), new Date(details.get(1)), new Time(details.get(2)),
					new Time(details.get(3)), new UniqueTagList(tagSet));
			break;

		case "deadline":
			this.toAdd = new Task(new Name(details.get(0)), new TaskDescription(details.get(1)), new Date(details.get(2)),
					new Time(details.get(3)), new UniqueTagList(tagSet));
			break;

		case "deadline without task description":
			this.toAdd = new Task(new Name(details.get(0)), new Date(details.get(1)), new Time(details.get(2)),
					new UniqueTagList(tagSet));
			break;

		case "deadline without task description and time":
			this.toAdd = new Task(new Name(details.get(0)), new Date(details.get(1)), new UniqueTagList(tagSet));
			break;

		case "deadline without task description and date":
			this.toAdd = new Task(new Name(details.get(0)), new Time(details.get(1)), new UniqueTagList(tagSet));
			break;

		case "deadline without date":
			this.toAdd = new Task(new Name(details.get(0)), new TaskDescription(details.get(1)),
					new Time(details.get(2)), new UniqueTagList(tagSet));
			break;

		case "deadline without time":
			this.toAdd = new Task(new Name(details.get(0)), new TaskDescription(details.get(1)),
					new Date(details.get(2)), new UniqueTagList(tagSet));
			break;

		case "floating task":
			this.toAdd = new Task(new Name(details.get(0)), new TaskDescription(details.get(1)),
					new UniqueTagList(tagSet));
			break;

		default:
			this.toAdd = new Task(new Name(details.get(0)), new UniqueTagList(tagSet));
		}
	}

	@Override
	public CommandResult execute() throws IllegalValueException {
		assert model != null;
		try {
			model.saveToPrevLists();
			model.addTask(toAdd);
			overdueOrNot();
			return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
		} catch (UniqueTaskList.DuplicateTaskException e) {
			return new CommandResult(MESSAGE_DUPLICATE_TASK);
		}

	}

	/**
	 * @throws IllegalValueException
	 * @throws DuplicateTagException
	 */
    //This method checks the entire list to check for overdue tasks
	private void overdueOrNot() throws IllegalValueException, DuplicateTagException {
		List<ReadOnlyTask> currentTaskList= model.getListOfTask().getTaskList();
		for(ReadOnlyTask task : currentTaskList){
			if(((Task) task).checkOverDue()){
				((Task) task).setOverdue();
			}
			else{
				((Task) task).setNotOverdue();
			}
		}
	}

}

@@ -3510,27 +3510,8 @@
 d);%0A
-%09%09%09overdueOrNot();%0A
 %09%09%09r
@@ -3694,485 +3694,6 @@
 %09%7D%0A%0A
-%09/**%0A%09 * @throws IllegalValueException%0A%09 * @throws DuplicateTagException%0A%09 */%0A    //This method checks the entire list to check for overdue tasks%0A%09private void overdueOrNot() throws IllegalValueException, DuplicateTagException %7B%0A%09%09List%3CReadOnlyTask%3E currentTaskList= model.getListOfTask().getTaskList();%0A%09%09for(ReadOnlyTask task : currentTaskList)%7B%0A%09%09%09if(((Task) task).checkOverDue())%7B%0A%09%09%09%09((Task) task).setOverdue();%0A%09%09%09%7D%0A%09%09%09else%7B%0A%09%09%09%09((Task) task).setNotOverdue();%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Update Add command for overdue tasks