package xyz.cottongin.radio247.data.importing

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

class M3uParserTest {

    @Test
    fun parseBasicM3uWithExtinfAndUrls() {
        val content = """
            #EXTM3U
            #EXTINF:-1,Station One
            http://stream1.example.com/radio
            #EXTINF:-1,Station Two
            http://stream2.example.com/radio
        """.trimIndent()

        val result = M3uParser.parse(content)

        assertEquals(2, result.size)
        assertEquals("Station One", result[0].name)
        assertEquals("http://stream1.example.com/radio", result[0].url)
        assertEquals(null, result[0].artworkUrl)
        assertEquals("Station Two", result[1].name)
        assertEquals("http://stream2.example.com/radio", result[1].url)
    }

    @Test
    fun parseM3uWithExtimgArtworkUrls() {
        val content = """
            #EXTM3U
            #EXTINF:-1,Radio Station
            #EXTIMG:http://art.example.com/logo.png
            http://stream.example.com/radio
        """.trimIndent()

        val result = M3uParser.parse(content)

        assertEquals(1, result.size)
        assertEquals("Radio Station", result[0].name)
        assertEquals("http://stream.example.com/radio", result[0].url)
        assertEquals("http://art.example.com/logo.png", result[0].artworkUrl)
    }

    @Test
    fun parseUrlOnlyLinesNoExtinf() {
        val content = """
            #EXTM3U
            http://stream.example.com/live
            https://another.example.com/stream
        """.trimIndent()

        val result = M3uParser.parse(content)

        assertEquals(2, result.size)
        assertEquals("live", result[0].name)
        assertEquals("http://stream.example.com/live", result[0].url)
        assertEquals("stream", result[1].name)
        assertEquals("https://another.example.com/stream", result[1].url)
    }

    @Test
    fun handleBlankLinesAndComments() {
        val content = """
            #EXTM3U

            #EXTINF:-1,My Station

            # some comment
            http://stream.example.com/radio

        """.trimIndent()

        val result = M3uParser.parse(content)

        assertEquals(1, result.size)
        assertEquals("My Station", result[0].name)
        assertEquals("http://stream.example.com/radio", result[0].url)
    }

    @Test
    fun skipEntriesWithBlankUrls() {
        val content = """
            #EXTM3U
            #EXTINF:-1,Station One

            http://stream1.example.com/radio
            #EXTINF:-1,Station Two
               
            http://stream2.example.com/radio
        """.trimIndent()

        val result = M3uParser.parse(content)

        assertEquals(2, result.size)
        assertEquals("Station One", result[0].name)
        assertEquals("http://stream1.example.com/radio", result[0].url)
        assertEquals("Station Two", result[1].name)
        assertEquals("http://stream2.example.com/radio", result[1].url)
    }

    @Test
    fun handleCrlfLineEndings() {
        val content = "#EXTM3U\r\n#EXTINF:-1,CRLF Station\r\nhttp://stream.example.com/radio\r\n"

        val result = M3uParser.parse(content)

        assertEquals(1, result.size)
        assertEquals("CRLF Station", result[0].name)
        assertEquals("http://stream.example.com/radio", result[0].url)
    }

    @Test
    fun roundTripExportParseCompare() {
        val originalStations = listOf(
            xyz.cottongin.radio247.data.model.Station(
                id = 1,
                name = "First Station",
                url = "http://first.example.com/stream",
                playlistId = null,
                sortOrder = 0,
                starred = false,
                defaultArtworkUrl = "http://art.example.com/first.png"
            ),
            xyz.cottongin.radio247.data.model.Station(
                id = 2,
                name = "Second Station",
                url = "https://second.example.com/live",
                playlistId = null,
                sortOrder = 1,
                starred = false,
                defaultArtworkUrl = null
            )
        )

        val exported = PlaylistExporter.toM3u(originalStations)
        val parsed = M3uParser.parse(exported)

        assertEquals(2, parsed.size)
        assertEquals("First Station", parsed[0].name)
        assertEquals("http://first.example.com/stream", parsed[0].url)
        assertEquals("http://art.example.com/first.png", parsed[0].artworkUrl)
        assertEquals("Second Station", parsed[1].name)
        assertEquals("https://second.example.com/live", parsed[1].url)
        assertEquals(null, parsed[1].artworkUrl)
    }
}
