package com.ponzischeme89.memby.ui.player

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

class PlayerTimingTest {
    @Test
    fun formatsRemainingTimeForViewerFriendlyDisplay() {
        assertEquals("Less than a minute remaining", PlayerActivity.formatRemaining(30_000L))
        assertEquals("42 min remaining", PlayerActivity.formatRemaining(42L * 60_000L))
        assertEquals("1 hr remaining", PlayerActivity.formatRemaining(60L * 60_000L))
        assertEquals("2 hr 5 min remaining", PlayerActivity.formatRemaining(125L * 60_000L))
    }

    @Test
    fun timeRemainingCueStartsStrictlyBelowTenMinutes() {
        assertNull(PlayerActivity.timeRemainingCueMinutes(10L * 60_000L))
        assertEquals(10, PlayerActivity.timeRemainingCueMinutes(10L * 60_000L - 1L))
        assertEquals(2, PlayerActivity.timeRemainingCueMinutes(61_000L))
        assertEquals(1, PlayerActivity.timeRemainingCueMinutes(1L))
        assertNull(PlayerActivity.timeRemainingCueMinutes(0L))
    }

    @Test
    fun playbackStartCueWaitsForTenSecondsOfPlaying() {
        assertEquals(false, PlayerActivity.playbackStartCueReady(9_999L))
        assertEquals(true, PlayerActivity.playbackStartCueReady(10_000L))
        assertEquals("1 min", PlayerActivity.formatCueDuration(1L))
        assertEquals("42 mins", PlayerActivity.formatCueDuration(42L * 60_000L))
    }

    @Test
    fun resumedPlaybackShowsTimeLeftAfterTwoSecondsOfPlaying() {
        assertEquals(false, PlayerActivity.playbackStartCueReady(1_999L, resumePositionMs = 1L))
        assertEquals(true, PlayerActivity.playbackStartCueReady(2_000L, resumePositionMs = 1L))
    }

    @Test
    fun prerollRuntimeUsesCompactEpisodeFacts() {
        assertNull(PlayerActivity.formatPrerollRuntime(0L))
        assertEquals("48 mins", PlayerActivity.formatPrerollRuntime(48L * 60_000L))
        assertEquals("1h 42m", PlayerActivity.formatPrerollRuntime(102L * 60_000L))
    }

    @Test
    fun completedPlaybackReturnsHomeUnlessAutoplayCanAdvance() {
        assertEquals(
            PlaybackCompletionAction.RETURN_HOME,
            playbackCompletionAction(hasNextEpisode = false, nextUpDismissed = false),
        )
        assertEquals(
            PlaybackCompletionAction.RETURN_HOME,
            playbackCompletionAction(hasNextEpisode = true, nextUpDismissed = true),
        )
        assertEquals(
            PlaybackCompletionAction.PLAY_NEXT,
            playbackCompletionAction(hasNextEpisode = true, nextUpDismissed = false),
        )
    }
}
