package seedu.taskitty.model.task;

import java.time.DateTimeException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import seedu.taskitty.commons.exceptions.IllegalValueException;
import seedu.taskitty.commons.util.TimeUtil;

/**
 * Represents a Task's name in the task manager.
 * Guarantees: immutable; is valid as declared in {@link #isValidName(String)}
 */
public class TaskTime {

    //TODO add more ways to type the time. eg. am/pm
    public static final String MESSAGE_TIME_CONSTRAINTS =
            "Task times should be in the format hh:mm"; //or hhmm
    public static final String MESSAGE_TIME_INVALID =
            "Task time provided is invalid!";
    public static final String MESSAGE_TIME_MISSING =
            "Task time must be provided";
    public static final String TIME_FORMAT_STRING = "HH:mm";
    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_FORMAT_STRING);
    
    //format: hh:mm
    private static final String TIME_VALIDATION_FORMAT = "[\\p{Digit}]{1,2}:[\\p{Digit}]{2}";

    public final LocalTime time;

    /**
     * Validates given name.
     *
     * @throws IllegalValueException if given name string is invalid.
     */
    public TaskTime(String time) throws IllegalValueException {
        if (time == null) {
            throw new IllegalValueException(MESSAGE_TIME_MISSING);
        }
        
        time = time.trim();
        if (!isValidName(time)) {
            throw new IllegalValueException(MESSAGE_TIME_CONSTRAINTS);
        }
        
        this.time = LocalTime.parse(time, TIME_FORMATTER);
    }

    /**
     * Returns true if a given string is a valid person name.
     */
    public static boolean isValidName(String test) {
        return test.matches(TIME_VALIDATION_FORMAT);
    }


    @Override
    public String toString() {
        return time.format(TIME_FORMATTER);
    }

    @Override
    public boolean equals(Object other) {
        return other == this // short circuit if same object
                || (other instanceof TaskTime // instanceof handles nulls
                && this.time.equals(((TaskTime) other).time)); // state check
    }

    @Override
    public int hashCode() {
        return time.hashCode();
    }

}

@@ -135,16 +135,64 @@
 rmatter;
+%0Aimport java.time.format.DateTimeParseException;
 %0A%0Aimport
@@ -409,26 +409,26 @@
 ink #isValid
-Na
+Ti
 me(String)%7D%0A
@@ -460,61 +460,8 @@
  %7B%0A%0A
-    //TODO add more ways to type the time. eg. am/pm%0A
     
@@ -536,17 +536,16 @@
 ask time
-s
  should 
@@ -572,18 +572,8 @@
 mm%22;
- //or hhmm
 %0A   
@@ -1119,21 +1119,116 @@
 s given 
-na
+ti
 me.
+ The time should be parsed by Natty and%0A     * be in the format according to TIME_FORMAT_STRING
 %0A     *%0A
@@ -1277,20 +1277,13 @@
 ven 
-na
+ti
 me 
-string 
 is i
@@ -1288,16 +1288,24 @@
  invalid
+ or null
 .%0A     *
@@ -1508,24 +1508,117 @@
 ime.trim();%0A
+        //This is not an assert because user can change the database and input wrong formats%0A
         if (
@@ -1625,20 +1625,26 @@
 !isValid
-Name
+TimeFormat
 (time)) 
@@ -1727,32 +1727,50 @@
      %7D%0A        %0A
+        try %7B%0A    
         this.tim
@@ -1804,32 +1804,156 @@
 IME_FORMATTER);%0A
+        %7D catch (DateTimeParseException dtpe)%7B%0A            throw new IllegalValueException(MESSAGE_TIME_INVALID);%0A        %7D%0A
     %7D%0A%0A    /**%0A 
@@ -2058,12 +2058,18 @@
 alid
-Name
+TimeFormat
 (Str

Improve model for TaskTime