package seedu.task.logic.commands;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;

import seedu.task.commons.exceptions.IllegalValueException;
import seedu.task.logic.GlobalStack;
import seedu.task.logic.commands.exceptions.CommandException;
import seedu.task.model.tag.Tag;
import seedu.task.model.tag.UniqueTagList;
import seedu.task.model.task.Deadline;
import seedu.task.model.task.Information;
import seedu.task.model.task.PriorityLevel;
import seedu.task.model.task.Task;
import seedu.task.model.task.TaskName;
import seedu.task.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: TASK_NAME [d/DEADLINE] [p/PRIORITY_LEVEL] [i/ANY_INFO] [t/TAG]...\n"
            + "Example: " + COMMAND_WORD
            + "Buy Milk d/17-Mar-2017 p/4 i/For home t/chore";

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

    private final Task toAdd;

    /**
     * Creates an AddCommand using raw values.
     *
     * @throws IllegalValueException if any of the raw values are invalid
     */
    public AddCommand(String taskName, String deadline, String priorityLevel, String info, Set<String> tags)
            throws IllegalValueException {
        final Set<Tag> tagSet = new HashSet<>();
        for (String tagName : tags) {
            tagSet.add(new Tag(tagName));
        }
        //@@author A0139161J
        //NLP, Natty implementation
        Parser parser = new Parser();
        List <DateGroup> groups = parser.parse(deadline);
        List dates = null;
        int line;
        int column;
        String matchingValue;
        String syntaxTree;
        Map parseMap;
        boolean isRecurring;
        Date recursUntil;

        for (DateGroup group: groups) {
            dates = group.getDates();
            line = group.getLine();
            column = group.getPosition();
            matchingValue = group.getText();
            syntaxTree = group.getSyntaxTree().toStringTree();
            parseMap = group.getParseLocations();
            isRecurring = group.isRecurring();
            recursUntil = group.getRecursUntil();
        }

        if (dates != null) {
            deadline = dates.get(0).toString();
        }
        StringTokenizer st = new StringTokenizer(deadline);
        List<String> listDeadline = new ArrayList<String>();
        while (st.hasMoreTokens()) {
            listDeadline.add(st.nextToken());
        }
        StringBuilder deadlineString = new StringBuilder();
        deadlineString.append(listDeadline.get(2) + "-" + listDeadline.get(1)
             + "-" + listDeadline.get(5)); // Extracting the dates.toString() format to DD-MMM-YYYY
        deadline = deadlineString.toString();
        this.toAdd = new Task(
                new TaskName(taskName),
                new Deadline(deadline),
                new PriorityLevel(priorityLevel),
                new Information(info),
                new UniqueTagList(tagSet)
        );
        toAdd.setParserInfo("add");
    }

    @Override
    public CommandResult execute() throws CommandException {
        assert model != null;
        try {
            model.addTask(toAdd);
            GlobalStack gStack = GlobalStack.getInstance();
            gStack.getUndoStack().push(toAdd);
            /**Debugging purpose
             * gStack.printStack();
             */
            //@@author
            return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
        } catch (UniqueTaskList.DuplicateTaskException e) {
            throw new CommandException(MESSAGE_DUPLICATE_TASK);
        }
    }
}

@@ -1917,16 +1917,56 @@
 ntation%0A
+        if (!deadline.equals(%22%22)) %7B%0A    
         
@@ -1991,24 +1991,28 @@
 w Parser();%0A
+    
         List
@@ -2061,24 +2061,28 @@
 e);%0A        
+    
 List dates =
@@ -2092,24 +2092,28 @@
 ll;%0A        
+    
 int line;%0A  
@@ -2114,24 +2114,28 @@
 ne;%0A        
+   
+ 
 int column;%0A
@@ -2134,16 +2134,20 @@
 column;%0A
+    
         
@@ -2172,24 +2172,28 @@
 ue;%0A        
+ 
+   
 String synta
@@ -2207,16 +2207,20 @@
         
+  
+  
 Map pars
@@ -2225,16 +2225,20 @@
 rseMap;%0A
+    
         
@@ -2266,16 +2266,20 @@
         
+    
 Date rec
@@ -2293,24 +2293,28 @@
 l;%0A%0A        
+ 
+   
 for (DateGro
@@ -2345,16 +2345,20 @@
         
+    
 dates = 
@@ -2387,16 +2387,20 @@
         
+   
+ 
 line = g
@@ -2411,24 +2411,28 @@
 .getLine();%0A
+    
             
@@ -2473,16 +2473,20 @@
         
+ 
+   
 matching
@@ -2522,16 +2522,20 @@
         
+  
+  
 syntaxTr
@@ -2573,24 +2573,28 @@
 ringTree();%0A
+    
             
@@ -2643,16 +2643,20 @@
         
+    
 isRecurr
@@ -2694,16 +2694,20 @@
         
+ 
+   
 recursUn
@@ -2740,27 +2740,35 @@
 ();%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         if (
@@ -2788,32 +2788,36 @@
 ) %7B%0A            
+    
 deadline = dates
@@ -2840,34 +2840,40 @@
 ();%0A        
-%7D%0A
+    %7D%0A  
         
 StringTokeni
@@ -2856,24 +2856,26 @@
 %7D%0A          
+  
 StringTokeni
@@ -2906,32 +2906,36 @@
 izer(deadline);%0A
+    
         List%3CStr
@@ -2987,16 +2987,20 @@
         
+    
 while (s
@@ -3032,16 +3032,20 @@
         
+ 
+   
 listDead
@@ -3070,34 +3070,42 @@
 ken());%0A        
-%7D%0A
+    %7D%0A    
         StringBu
@@ -3148,32 +3148,36 @@
 lder();%0A        
+    
 deadlineString.a
@@ -3230,16 +3230,19 @@
 .get(1)%0A
+   
         
@@ -3333,16 +3333,20 @@
 MM-YYYY%0A
+    
         
@@ -3375,32 +3375,42 @@
 ing.toString();%0A
+        %7D%0A
         this.toA

Fix bug in add Command