package com.ponzischeme89.memby.data

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

class DevicePlaybackCapabilitiesTest {
    @Test
    fun detailedH264AndHevcDecoderSupportIsReportedToTheGateway() {
        val capabilities = 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,
                hdr10 = true,
            ),
        )
        val tokens = capabilities.gatewayCapabilityTokens()

        assertTrue("video_h264_profile_high" in tokens)
        assertTrue("video_h264_level_52" in tokens)
        assertTrue("video_h264_max_3840x2160" in tokens)
        assertTrue("video_hevc_decode" in tokens)
        assertTrue("video_hevc_profile_main10" in tokens)
        assertTrue("video_hevc_main10_level_153" in tokens)
        assertTrue("video_hevc_hdr10" in tokens)
    }

    @Test
    fun h264OnlyDeviceDoesNotClaimHevc() {
        val tokens = DevicePlaybackCapabilities().gatewayCapabilityTokens()

        assertTrue("video_h264_decode" in tokens)
        assertTrue("video_hevc_decode" !in tokens)
    }

    @Test
    fun decoderFallbackCannotReturnHevcStreamCopy() {
        val fallback = DeviceProfile.embyAndroidTv(supportsHevc = true)
            .h264TranscodeFallback()

        assertTrue(fallback.directPlayProfiles.isEmpty())
        assertEquals(listOf("h264"), fallback.transcodingProfiles.map { it.videoCodec })
        assertTrue(fallback.codecProfiles.none { it.codec.equals("hevc", ignoreCase = true) })
    }
}
