package com.battlelancer.seriesguide.util;

import com.battlelancer.seriesguide.util.TimeTools;

import org.junit.Test;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import java.lang.System;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import static org.fest.assertions.api.Assertions.assertThat;

// TODO Can't run Robolectric until it supports Android 4.4
//@RunWith(RobolectricTestRunner.class)
public class TimeToolsTest {

    private static final SimpleDateFormat TIME_FORMAT_CUSTOM_TIMEZONE = new SimpleDateFormat(
            "hh:mm aa");

    static {
        TIME_FORMAT_CUSTOM_TIMEZONE.setTimeZone(TimeZone.getTimeZone(TimeTools.TIMEZONE_ID_CUSTOM));
    }

    @Test
    public void test_parseEpisodeReleaseTime() {
        long showReleaseTime = TimeTools.parseShowReleaseTime("8:00pm");
        long episodeReleaseTime = TimeTools
                .parseEpisodeReleaseTime("2013-05-31", showReleaseTime, "United States");
        usTimeZoneWarning();
        System.out.println(
                "Release time: " + episodeReleaseTime + " " + new Date(episodeReleaseTime));
        assertThat(episodeReleaseTime).isEqualTo(1370055600000L);
    }

    @Test
    public void test_parseEpisodeReleaseTime_HourPastMidnight() {
        long showReleaseTime = TimeTools.parseShowReleaseTime("12:35am");
        long episodeReleaseTime = TimeTools
                .parseEpisodeReleaseTime("2013-05-31", showReleaseTime, "United States");
        usTimeZoneWarning();
        System.out.println(
                "Release time: " + episodeReleaseTime + " " + new Date(episodeReleaseTime));
        assertThat(episodeReleaseTime).isEqualTo(1370072100000L);
    }

    private void usTimeZoneWarning() {
        System.out.println("WARNING: This test WILL fail on any US time zone not US Pacific");
    }

    @Test
    public void test_parseTimeToMillisecondsFormats() {
        parseAndCompare("8:00pm", "08:00 PM");
        parseAndCompare("8:00am", "08:00 AM");

        // test some variations to be sure
        parseAndCompare("8:00PM", "08:00 PM");
        parseAndCompare("08:00pm", "08:00 PM");
        parseAndCompare("08:00PM", "08:00 PM");
    }

    private void parseAndCompare(String time, String timeResult) {
        long timeMs = TimeTools.parseShowReleaseTime(time);
        String timeString = TIME_FORMAT_CUSTOM_TIMEZONE.format(new Date(timeMs));

        System.out.println(
                time + " is " + timeString + " or " + timeMs + "ms or " + new Date(timeMs));

        assertThat(timeString).isEqualTo(timeResult);
    }

//    @Test
//    public void test_formatShowReleaseTimeAndDay() {
//        long releaseTime = TimeTools.parseTimeToMilliseconds("12:35am");
//        Context context = Robolectric.getShadowApplication().getApplicationContext();
//        String[] timeAndDay = TimeTools
//                .formatShowReleaseTimeAndDay(context, releaseTime, "United States", "Monday");
//        System.out.println("Time: " + timeAndDay[0] + "and day: " + timeAndDay[1]);
//    }

}
@@ -1221,32 +1221,523 @@
 00000L);%0A    %7D%0A%0A
+    @Test%0A    public void test_parseEpisodeReleaseTime_Country() %7B%0A        long showReleaseTime = TimeTools.parseShowReleaseTime(%228:00pm%22);%0A        long episodeReleaseTime = TimeTools%0A                .parseEpisodeReleaseTime(%222013-05-31%22, showReleaseTime, %22Germany%22);%0A        usTimeZoneWarning();%0A        System.out.println(%0A                %22Release time: %22 + episodeReleaseTime + %22 %22 + new Date(episodeReleaseTime));%0A        assertThat(episodeReleaseTime).isEqualTo(1370023200000L);%0A    %7D%0A%0A
     @Test%0A    pu
@@ -1792,24 +1792,24 @@
 idnight() %7B%0A
-
         long
@@ -2404,34 +2404,32 @@
 st_parse
-TimeToMilliseconds
+ShowReleaseTime_
 Formats(
@@ -3004,19 +3004,16 @@
 ring + %22
- or
  %22 + tim
@@ -3023,13 +3023,8 @@
  + %22
-ms or
  %22 +

Add parseEpisodeReleaseTime for another country.