package seedu.geekeep.logic.commands;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import seedu.geekeep.commons.exceptions.IllegalValueException;
import seedu.geekeep.logic.commands.exceptions.CommandException;
import seedu.geekeep.model.tag.Tag;
import seedu.geekeep.model.tag.UniqueTagList;
import seedu.geekeep.model.task.DateTime;
import seedu.geekeep.model.task.Location;
import seedu.geekeep.model.task.Task;
import seedu.geekeep.model.task.Title;
import seedu.geekeep.model.task.UniqueTaskList;

/**
 * Adds a task to the Task Manager.
 */
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 Task Manager. "
            + "Parameters: TITLE s/STARTING_DATETIME e/ENDING_DATETIME l/LOCATION [t/TAG]...\n"
            + "Example: " + COMMAND_WORD
            + " Meeting 1 s/2017-04-01T10:16:30 e/2017-04-01T10:17:30 l/311, Clementi Ave 2, #02-25 t/friends";

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

    private final Task toAdd;

    /**
     * Creates an AddCommand using raw values.
     *
     * @throws IllegalValueException if any of the raw values are invalid
     */
    public AddCommand(String title, Optional<String> startDateTime, Optional<String> endDateTime,
            Optional<String> location, Set<String> tags)
            throws IllegalValueException {
        final Set<Tag> tagSet = new HashSet<>();
        for (String tagName : tags) {
            tagSet.add(new Tag(tagName));
        }

        this.toAdd = new Task(
                new Title(title),
                new DateTime(startDateTime.orElse(null)),
                new DateTime(endDateTime.orElse(null)),
                new Location(location.orElse(null)),
                new UniqueTagList(tagSet)
        );
    }

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

    }

}

@@ -1722,30 +1722,71 @@
     
-this.toAdd = new Task(
+DateTime start = null, end = null;%0A        Location loc = null;
 %0A   
@@ -1786,32 +1786,33 @@
 ll;%0A        
+%0A
         
 new Title(ti
@@ -1803,25 +1803,40 @@
     
-new Title(title),
+if (startDateTime.isPresent()) %7B
 %0A   
@@ -1836,35 +1836,39 @@
 ) %7B%0A            
-   
+start =
  new DateTime(st
@@ -1875,38 +1875,31 @@
 artDateTime.
-orElse(null)),
+get());
 %0A           
@@ -1891,32 +1891,34 @@
 ));%0A        
+%7D%0A
         
 new DateTime
@@ -1909,47 +1909,135 @@
     
-new DateTime(endDateTime.orElse(null)),
+if (endDateTime.isPresent()) %7B%0A            end = new DateTime(endDateTime.get());%0A        %7D%0A        if (location.isPresent()) %7B
 %0A   
@@ -2041,27 +2041,29 @@
             
-   
+loc =
  new Locatio
@@ -2077,21 +2077,155 @@
 ion.
-orElse(null))
+get());%0A        %7D%0A%0A        this.toAdd = new Task(%0A                new Title(title),%0A                start,%0A                end,%0A                loc
 ,%0A  

Fix AddCommand model API call