package com.battlelancer.seriesguide.test;

import com.battlelancer.seriesguide.util.TimeTools;
import java.util.Date;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import static org.assertj.core.api.Assertions.assertThat;

public class TimeToolsTest extends TestCase {

    public static final String AMERICA_NEW_YORK = "America/New_York";
    public static final String AMERICA_LOS_ANGELES = "America/Los_Angeles";
    public static final String EUROPE_BERLIN = "Europe/Berlin";
    public static final String GERMANY = "de";
    public static final String UNITED_STATES = "us";

    public static Test suite() {
        return new TestSuite(TimeToolsTest.class);
    }

    public void test_parseEpisodeReleaseTime() {
        // ensure a US show has its local release time correctly converted to UTC time
        // (we can be sure that in May there is always DST in effect in America/New_York
        // so this test will likely not break if DST rules change)
        String showTimeZone = AMERICA_NEW_YORK;
        String deviceTimeZone = AMERICA_LOS_ANGELES;
        long episodeReleaseTime = TimeTools.parseEpisodeReleaseDate(
                TimeTools.getTvdbDateFormatter(showTimeZone),
                "2013-05-31",
                2000, // 20:00
                UNITED_STATES,
                deviceTimeZone);
        System.out.println(
                "Release time: " + episodeReleaseTime + " " + new Date(episodeReleaseTime));
        assertThat(episodeReleaseTime).isEqualTo(1370055600000L);
    }

    public void test_parseEpisodeReleaseTime_Country() {
        // ensure a German show has its local release time correctly converted to UTC time
        // (we can be sure that in May there is always DST in effect in Europe/Berlin
        // so this test will likely not break if DST rules change)
        String showTimeZone = EUROPE_BERLIN;
        String deviceTimeZone = AMERICA_LOS_ANGELES;
        long episodeReleaseTime = TimeTools.parseEpisodeReleaseDate(
                TimeTools.getTvdbDateFormatter(showTimeZone),
                "2013-05-31",
                2000, // 20:00
                GERMANY,
                deviceTimeZone);
        System.out.println(
                "Release time: " + episodeReleaseTime + " " + new Date(episodeReleaseTime));
        assertThat(episodeReleaseTime).isEqualTo(1370023200000L);
    }

    public void test_parseEpisodeReleaseTime_HourPastMidnight() {
        // ensure episodes releasing in the hour past midnight are moved to the next day
        // e.g. if 00:35, the episode date is typically (wrongly) that of the previous day
        // this is common for late night shows, e.g. "Monday night" is technically "early Tuesday"
        String showTimeZone = AMERICA_NEW_YORK;
        String deviceTimeZone = AMERICA_LOS_ANGELES;
        long episodeReleaseTime = TimeTools.parseEpisodeReleaseDate(
                TimeTools.getTvdbDateFormatter(showTimeZone),
                "2013-05-31",
                35, // 00:35
                UNITED_STATES,
                deviceTimeZone);
        System.out.println(
                "Release time: " + episodeReleaseTime + " " + new Date(episodeReleaseTime));
        assertThat(episodeReleaseTime).isEqualTo(1370072100000L);
    }
}
@@ -207,16 +207,83 @@
 stSuite;
+%0Aimport org.joda.time.DateTimeZone;%0Aimport org.joda.time.LocalTime;
 %0A%0Aimport
@@ -1076,38 +1076,44 @@
 change)%0A        
-String
+DateTimeZone
  showTimeZone = 
@@ -1104,32 +1104,51 @@
  showTimeZone = 
+DateTimeZone.forID(
 AMERICA_NEW_YORK
@@ -1139,32 +1139,33 @@
 AMERICA_NEW_YORK
+)
 ;%0A        String
@@ -1292,39 +1292,8 @@
     
-TimeTools.getTvdbDateFormatter(
 show
@@ -1292,33 +1292,32 @@
     showTimeZone
-)
 ,%0A              
@@ -1340,36 +1340,52 @@
                 
-2000
+new LocalTime(20, 0)
 , // 20:00%0A     
@@ -1938,38 +1938,44 @@
 change)%0A        
-String
+DateTimeZone
  showTimeZone = 
@@ -1974,16 +1974,35 @@
 eZone = 
+DateTimeZone.forID(
 EUROPE_B
@@ -2006,16 +2006,17 @@
 E_BERLIN
+)
 ;%0A      
@@ -2151,39 +2151,8 @@
     
-TimeTools.getTvdbDateFormatter(
 show
@@ -2151,33 +2151,32 @@
     showTimeZone
-)
 ,%0A              
@@ -2207,20 +2207,36 @@
         
-2000
+new LocalTime(20, 0)
 , // 20:
@@ -2843,22 +2843,28 @@
         
-String
+DateTimeZone
  showTim
@@ -2863,32 +2863,51 @@
  showTimeZone = 
+DateTimeZone.forID(
 AMERICA_NEW_YORK
@@ -2906,16 +2906,17 @@
 NEW_YORK
+)
 ;%0A      
@@ -3051,39 +3051,8 @@
     
-TimeTools.getTvdbDateFormatter(
 show
@@ -3059,17 +3059,16 @@
 TimeZone
-)
 ,%0A      
@@ -3110,11 +3110,29 @@
     
+ new LocalTime(0,
  35
+)
 , //

Fix time tools test.