package seedu.address.commons.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;

import org.junit.Test;

import seedu.todo.commons.util.DateUtil;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class DateUtilTest {
	
	@Test
	public void toDate_equalTimestamps() {
		long currTimeMs = java.lang.System.currentTimeMillis();
		Date testDate = new Date(currTimeMs);
		LocalDateTime testDateTime = fromEpoch(currTimeMs);
		Date actualDate = DateUtil.toDate(testDateTime);
		
		assertEquals(testDate.getTime(), actualDate.getTime());
	}
	
	@Test
	public void toDate_differentTimestamps() {
		long currTimeMs = java.lang.System.currentTimeMillis();
		Date testDate = new Date(currTimeMs);
		LocalDateTime testDateTime = fromEpoch(currTimeMs + 1);
		Date actualDate = DateUtil.toDate(testDateTime);
		
		assertNotEquals(testDate.getTime(), actualDate.getTime());
	}
	
	@Test
	public void floorDate_sameDate() {
		long testEpoch1 = 1476099300000l; // 10/10/2016 @ 11:35am (UTC)
		long testEpoch2 = 1476099360000l; // 10/10/2016 @ 11:36am (UTC)
		LocalDateTime testDateTime1 = fromEpoch(testEpoch1);
		LocalDateTime testDateTime2 = fromEpoch(testEpoch2);
		
		assertEquals(DateUtil.floorDate(testDateTime1), DateUtil.floorDate(testDateTime2));
	}
	
	@Test
	public void floorDate_differentDate() {
		long testEpoch1 = 1476099300000l; // 10/10/2016 @ 11:35am (UTC)
		long testEpoch2 = 1476185700000l; // 11/10/2016 @ 11:35am (UTC)
		LocalDateTime testDateTime1 = fromEpoch(testEpoch1);
		LocalDateTime testDateTime2 = fromEpoch(testEpoch2);
		
		assertNotEquals(DateUtil.floorDate(testDateTime1), DateUtil.floorDate(testDateTime2));
	}
	
	@Test
	public void formatDayTests() {
	    LocalDateTime now = LocalDateTime.now();
	    assertEquals(DateUtil.formatDay(now), "Today");
	    assertEquals(DateUtil.formatDay(now.plus(1, ChronoUnit.DAYS)), "Tomorrow");
	    
	    // Show day of week for d = {n+2,...,n+6} where n = today
	    for (int i = 2; i <= 6; i++) {
	        String expected = now.plus(i, ChronoUnit.DAYS).getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.US);
	        assertEquals(DateUtil.formatDay(now.plus(i, ChronoUnit.DAYS)), expected);
	    }
	    
	    assertEquals(DateUtil.formatDay(now.minus(1, ChronoUnit.DAYS)), "1 day ago");
        assertEquals(DateUtil.formatDay(now.minus(6, ChronoUnit.DAYS)), "6 days ago");
        assertEquals(DateUtil.formatDay(now.minus(14, ChronoUnit.DAYS)), "14 days ago");
	}
	
	private static LocalDateTime fromEpoch(long epoch) {
		return LocalDateTime.ofInstant(Instant.ofEpochMilli(epoch), ZoneId.systemDefault());
	}
	
}

@@ -113,16 +113,59 @@
 ZoneId;%0A
+import java.time.format.DateTimeFormatter;%0A
 import j
@@ -2486,36 +2486,117 @@
 , %221 day ago%22);%0A
+%09
     
+assertEquals(DateUtil.formatDay(now.minus(6, ChronoUnit.DAYS)), %226 days ago%22);%0A%09
     assertEquals
@@ -2617,33 +2617,34 @@
 atDay(now.minus(
-6
+14
 , ChronoUnit.DAY
@@ -2641,33 +2641,34 @@
 noUnit.DAYS)), %22
-6
+14
  days ago%22);%0A   
@@ -2668,60 +2668,425 @@
 %22);%0A
-        assertEquals(DateUtil.formatDay(now.min
+%09%7D%0A%09%0A%09@Test%0A%09public void formatShortDateTests() %7B%0A%09    LocalDateTime now = LocalDateTime.now();%0A%09    %0A%09    String expectedToday = now.format(DateTimeFormatter.ofPattern(%22E dd MMM%22));%0A%09    assertEquals(DateUtil.formatShortDate(now), expectedToday);%0A%09    %0A%09    String expectedTomorrow = now.plus(1,  ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern(%22E dd MMM%22));%0A%09    assertEquals(DateUtil.formatShortDate(now.pl
 us(1
-4
 , Ch
@@ -3098,36 +3098,443 @@
 nit.DAYS)), 
-%2214 days ago
+expectedTomorrow);%0A%09    %0A%09    for (int i = 2; i %3C= 6; i++) %7B%0A%09        String expected = now.plus(i,  ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern(%22dd MMM%22));%0A%09        assertEquals(DateUtil.formatShortDate(now.plus(i, ChronoUnit.DAYS)), expected);%0A%09    %7D%0A%09    %0A%09    // Test dates in the past%0A%09    LocalDateTime pastDate = fromEpoch(946656000000l);%0A%09    assertEquals(DateUtil.formatShortDate(pastDate), %22Sat 01 Jan
 %22);%0A%09%7D%0A%09%0A%09pr

Write tests for formatShortDate