package com.ponzischeme89.memby.data

import com.ponzischeme89.memby.data.model.DeviceProfile
import com.ponzischeme89.memby.data.playback.DeviceAudioCapabilities
import com.ponzischeme89.memby.data.playback.DevicePlaybackCapabilities
import com.ponzischeme89.memby.data.playback.VideoDecoderCapabilities
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class PlaybackReportMathTest {
    @Test
    fun millisecondsAreConvertedToEmbyTicks() {
        assertEquals(12_340_000L, millisecondsToTicks(1_234L))
    }

    @Test
    fun negativePositionsAreClamped() {
        assertEquals(0L, millisecondsToTicks(-1L))
    }

    @Test
    fun deviceProfileExternalizesTextAndEncodesBitmapSubtitles() {
        val profiles = DeviceProfile.embyAndroidTv().subtitleProfiles.associate {
            it.format to it.method
        }
        assertEquals("External", profiles["srt"])
        assertEquals("External", profiles["ass"])
        assertEquals("External", profiles["mov_text"])
        assertEquals("Encode", profiles["pgssub"])
        assertEquals("Encode", profiles["dvdsub"])
    }

    @Test
    fun directPlayProfileAdvertisesHevcOnlyForCapableTvs() {
        val baseline = DeviceProfile.embyAndroidTv(
            audio = DeviceAudioCapabilities(),
        ).directPlayProfiles.single()
        val capable = DeviceProfile.embyAndroidTv(
            capabilities = DevicePlaybackCapabilities(
                hevc = VideoDecoderCapabilities(supported = true),
            ),
            audio = DeviceAudioCapabilities(),
        ).directPlayProfiles.single()

        assertEquals("h264", baseline.videoCodec)
        assertEquals("h264,hevc", capable.videoCodec)
        assertEquals("aac,mp3,flac,opus,vorbis,pcm_s16le,pcm_s24le", capable.audioCodec)
        assertFalse(capable.videoCodec.contains("av1"))
        assertFalse(capable.audioCodec.contains("eac3"))
    }

    @Test
    fun detailedProfileAllowsVideoStreamCopyWithinDecoderLimits() {
        val profile = DeviceProfile.embyAndroidTv(
            DevicePlaybackCapabilities(
                h264 = VideoDecoderCapabilities(
                    supported = true,
                    profiles = setOf("baseline", "main", "high"),
                    mainLevel = 52,
                    maxWidth = 3840,
                    maxHeight = 2160,
                ),
                hevc = VideoDecoderCapabilities(
                    supported = true,
                    profiles = setOf("main", "main10"),
                    mainLevel = 153,
                    tenBitLevel = 153,
                    maxWidth = 3840,
                    maxHeight = 2160,
                ),
            ),
        )

        assertEquals("h264,hevc", profile.directPlayProfiles.single().videoCodec)
        assertEquals("h264,hevc", profile.transcodingProfiles.single().videoCodec)
        assertTrue(profile.codecProfiles.any { it.codec == "h264" && it.conditions.any { c -> c.value == "52" } })
        assertTrue(profile.codecProfiles.any { it.codec == "hevc" && it.conditions.any { c -> c.value == "153" } })
        assertTrue(profile.codecProfiles.any { it.codec == "hevc" && it.conditions.any { c -> c.property == "Width" } })
    }
}
