package tars.logic.commands;

import tars.commons.exceptions.IllegalValueException;
import tars.model.task.*;
import tars.model.tag.Tag;
import tars.model.tag.UniqueTagList;

import java.time.DateTimeException;
import java.util.HashSet;
import java.util.Set;

/**
 * Adds a task to tars.
 */
public class AddCommand extends Command {

	public static final String COMMAND_WORD = "add";

    public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a task to tars. "
            + "Parameters: NAME -dt DATETIME -p PRIORITY [-t TAG]...\n"
            + "Example: " + COMMAND_WORD
            + " cs2103 project meeting -dt 05/09/2016 1400 to 06/09/2016 2200 -p h -t project";

    public static final String MESSAGE_SUCCESS = "New task added: %1$s";
    public static final String MESSAGE_DUPLICATE_TASK= "This task already exists in tars";
    
    private static final int DATETIME_INDEX_OF_ENDDATE = 1;
	private static final int DATETIME_INDEX_OF_STARTDATE = 0;
	private static final int DATETIME_INDEX_OF_DEADLINE = 0;

    private final Task toAdd;
    
    /**
     * Convenience constructor using raw values.
     *
     * @throws IllegalValueException if any of the raw values are invalid
     * @throws DateTimeException if given dateTime string is invalid.
     */
    public AddCommand(String name, String[] dateTime, String priority, Set<String> tags)
            throws IllegalValueException, DateTimeException {
        final Set<Tag> tagSet = new HashSet<>();
        for (String tagName : tags) {
            tagSet.add(new Tag(tagName));
        }
        if (dateTime.length == 1) {
            this.toAdd = new Task(
                    new Name(name),
                    new DateTime(null, dateTime[DATETIME_INDEX_OF_DEADLINE]),
                    new Priority(priority),
                    new Status(),
                    new UniqueTagList(tagSet)
            );
        } else {
            this.toAdd = new Task(
                    new Name(name),
                    new DateTime(dateTime[DATETIME_INDEX_OF_STARTDATE], dateTime[DATETIME_INDEX_OF_ENDDATE]),
                    new Priority(priority),
                    new Status(),
                    new UniqueTagList(tagSet)
            );
        }
    }

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

    }

}

@@ -230,16 +230,43 @@
 ashSet;%0A
+import java.util.Optional;%0A
 import j
@@ -524,14 +524,18 @@
 ers:
+ -n
  NAME 
+%5B
 -dt 
@@ -542,17 +542,19 @@
 DATETIME
- 
+%5D %5B
 -p PRIOR
@@ -556,16 +556,17 @@
 PRIORITY
+%5D
  %5B-t TAG
@@ -629,16 +629,19 @@
      + %22
+ -n
  cs2103 
@@ -1463,16 +1463,17 @@
 ption %7B%0A
+%0A
         
@@ -1615,126 +1615,54 @@
     
-if (dateTime.length == 1) %7B%0A            this.toAdd = new Task(%0A                    new Name(name),%0A                   
+%0A        this.toAdd = new Task(new Name(name),
  new
@@ -1675,14 +1675,8 @@
 ime(
-null, 
 date
@@ -1684,394 +1684,25 @@
 ime%5B
-DATETIME_INDEX_OF_DEADLINE%5D),%0A                    new Priority(priority),%0A                    new Status(),%0A                    new UniqueTagList(tagSet)%0A            );%0A        %7D else %7B%0A            this.toAdd = new Task(%0A                    new Name(name),%0A                    new DateTime(dateTime%5BDATETIME_INDEX_OF_STARTDATE%5D, dateTime%5BDATETIME_INDEX_OF_ENDDATE%5D),%0A                   
+0%5D, dateTime%5B1%5D),
  new
@@ -1734,28 +1734,24 @@
             
-    
 new Status()
@@ -1755,28 +1755,8 @@
 s(),
-%0A                   
  new
@@ -1781,33 +1781,11 @@
 Set)
-%0A            );%0A        %7D
+);%0A
 %0A   

Update AddCommand