//@@author A0143409J
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.IncorrectCommand;
import seedu.address.logic.commands.ViewNextCommand;
import seedu.address.logic.commands.ViewOnCommand;
import seedu.address.model.task.TaskDate;

/**
 * Parses input arguments and creates a new ViewNextCommand/ViewOnCommand object
 */
public class ViewCommandParser {

    private static ViewCommandParser theOne;
    public static final String MESSAGE_NONNEGATIVE = "The number of days in the future cannot be negative.\n";

    private ViewCommandParser() {}

    public static ViewCommandParser getInstance() {
        if (theOne == null) {
            theOne = new ViewCommandParser();
        }
        return theOne;
    }

    /**
     * Parses the given {@code String} of arguments in the context of the ViewNextCommand
     * and returns an ViewNextCommand object for execution.
     * @throws IllegalValueException
     */
    public Command parse(String args) {
        if (args == null || args.equals("")) {
            return parseNoParamGiven();
        }

        String[] parameters = formatter(args);
        if (parameters[0].equals("next") || parameters[0].equals("by")) {
            return parseViewNext(parameters);
        } else if (parameters[0].equals("on")) {
            return parseViewOn(parameters);
        } else {
            return new IncorrectCommand(String.format(
                MESSAGE_INVALID_COMMAND_FORMAT, ViewNextCommand.MESSAGE_USAGE));
        }
    }

    private static Command parseNoParamGiven() {
        try {
            return new ViewNextCommand(0);
        } catch (IllegalValueException e) {
            return new IncorrectCommand(String.format(
                    MESSAGE_INVALID_COMMAND_FORMAT, ViewNextCommand.MESSAGE_USAGE));
        }
    }

    private static Command parseViewNext(String[] parameters) {
        try {
            if (Integer.valueOf(parameters[1]) < 0) {
                return new IncorrectCommand(String.format(
                        MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_NONNEGATIVE));
            } else {
                return new ViewNextCommand(Integer.valueOf(parameters[1]));
            }
        } catch (NumberFormatException nfe) {
            try {
                TaskDate date = new TaskDate(parameters[1]);
                return new ViewNextCommand(date);
            } catch (IllegalValueException e) {
                return new IncorrectCommand(String.format(
                    MESSAGE_INVALID_COMMAND_FORMAT, ViewNextCommand.MESSAGE_USAGE));
            }
        } catch (IllegalValueException e) {
            return new IncorrectCommand(String.format(
                    MESSAGE_INVALID_COMMAND_FORMAT, ViewNextCommand.MESSAGE_USAGE));
        }
    }

    private static Command parseViewOn(String[] parameters) {
        try {
            if (Integer.valueOf(parameters[1]) < 0) {
                return new IncorrectCommand(String.format(
                        MESSAGE_INVALID_COMMAND_FORMAT, MESSAGE_NONNEGATIVE));
            } else {
                return new ViewOnCommand(Integer.valueOf(parameters[1]));
            }
        } catch (NumberFormatException nfe) {
            try {
                TaskDate date = new TaskDate(parameters[1]);
                return new ViewOnCommand(date);
            } catch (IllegalValueException e) {
                return new IncorrectCommand(String.format(
                        MESSAGE_INVALID_COMMAND_FORMAT, ViewOnCommand.MESSAGE_USAGE));
            }
        } catch (IllegalValueException e) {
            return new IncorrectCommand(String.format(
                    MESSAGE_INVALID_COMMAND_FORMAT, ViewOnCommand.MESSAGE_USAGE));
        }
    }

    public static String[] formatter(String args) {
        String argument = args.trim();
        String[] parameters = argument.split("/", 2);
        return parameters;
    }
}

@@ -1008,16 +1008,23 @@
 t of the
+%0A     *
  ViewNex
@@ -1031,24 +1031,41 @@
 tCommand
+ or ViewOnCommand
 %0A     * 
 and retu
@@ -1060,22 +1060,17 @@
   * 
-and 
+@
 return
-s
  a
-n
  Vie
@@ -1077,24 +1077,38 @@
 wNextCommand
+/ViewOnCommand
  object for 
@@ -1122,45 +1122,8 @@
 on.%0A
-     * @throws IllegalValueException%0A
     
@@ -1702,89 +1702,127 @@
     
-private static Command parseNoParamGiven() %7B%0A        try %7B%0A    
+/**%0A     * Create a new ViewNextCommand with date today%0A     * when there is no parameter after %22view%22%0A
      
-   
+* @
 return 
-new
+A
  Vie
@@ -1837,61 +1837,78 @@
 mand
-(0);%0A        %7D catch (IllegalValueException e) %7B%0A    
+ dated today%0A     */%0A    private static Command parseNoParamGiven() %7B%0A
     
@@ -1918,32 +1918,31 @@
  return new 
-Incorrec
+ViewNex
 tCommand(Str
@@ -1942,124 +1942,192 @@
 and(
-String.format(%0A                    MESSAGE_INVALID_COMMAND_FORMAT, ViewNextCommand.MESSAGE_USAGE));%0A        %7D
+0);%0A    %7D%0A%0A    /**%0A     * Create a new ViewNextCommand%0A     * @param parameters A set of dates/number of days%0A     * @return A new ViewNextCommand with specific date associated
 %0A    
-%7D%0A
+ */
 %0A   
@@ -2898,199 +2898,185 @@
    %7D
- catch (IllegalValueException e) %7B%0A            return new IncorrectCommand(String.format(%0A                    MESSAGE_INVALID_COMMAND_FORMAT,
+%0A    %7D%0A%0A    /**%0A     * Create a new ViewOnCommand%0A     * @param parameters A set of dates/number of days%0A     * @return A new
  View
-Next
+On
 Command
-.MESSAGE_USAGE));%0A        %7D%0A    %7D%0A
+ with specific date associated%0A     */
 %0A   

Add javadoc for viewCommandParser