package xyz.cottongin.radio247.data.importing

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

class PlsParserTest {

    @Test
    fun parseBasicPlsWithNumberedEntries() {
        val content = """
            [playlist]
            File1=http://stream1.example.com/radio
            Title1=Station One
            File2=http://stream2.example.com/radio
            Title2=Station Two
            NumberOfEntries=2
        """.trimIndent()

        val result = PlsParser.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 handleMissingTitlesUrlFallback() {
        val content = """
            [playlist]
            File1=http://stream.example.com/live
            NumberOfEntries=1
        """.trimIndent()

        val result = PlsParser.parse(content)

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

    @Test
    fun handleCaseInsensitiveKeys() {
        val content = """
            [playlist]
            file1=http://stream.example.com/radio
            title1=Case Insensitive
            numberOfEntries=1
        """.trimIndent()

        val result = PlsParser.parse(content)

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

    @Test
    fun skipEntriesWithEmptyUrls() {
        val content = """
            [playlist]
            File1=http://stream1.example.com/radio
            Title1=Valid Station
            File2=
            Title2=Empty URL Station
            File3=http://stream3.example.com/radio
            Title3=Another Valid
            NumberOfEntries=3
        """.trimIndent()

        val result = PlsParser.parse(content)

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

    @Test
    fun handleOutOfOrderEntries() {
        val content = """
            [playlist]
            File2=http://stream2.example.com/radio
            Title2=Second
            File1=http://stream1.example.com/radio
            Title1=First
            NumberOfEntries=2
        """.trimIndent()

        val result = PlsParser.parse(content)

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

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

        val exported = PlaylistExporter.toPls(originalStations)
        val parsed = PlsParser.parse(exported)

        assertEquals(2, parsed.size)
        assertEquals("PLS Station One", parsed[0].name)
        assertEquals("http://first.example.com/stream", parsed[0].url)
        assertEquals("PLS Station Two", parsed[1].name)
        assertEquals("https://second.example.com/live", parsed[1].url)
    }
}
