package xyz.cottongin.radio247.data.importing

import xyz.cottongin.radio247.data.model.Station
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class PlaylistExporterTest {

    @Test
    fun sanitizeNewlinesInStationNamesForM3u() {
        val stations = listOf(
            Station(
                id = 1,
                name = "Line One\nLine Two",
                url = "http://stream.example.com/radio",
                playlistId = null,
                sortOrder = 0,
                starred = false,
                defaultArtworkUrl = null
            )
        )

        val result = PlaylistExporter.toM3u(stations)

        assertFalse(result.contains("#EXTINF:-1,Line One\nLine Two"))
        assertTrue(result.contains("#EXTINF:-1,Line One Line Two"))
        val parsed = M3uParser.parse(result)
        assertEquals(1, parsed.size)
        assertEquals("Line One Line Two", parsed[0].name)
    }

    @Test
    fun exportStationsToM3uFormatWithArtwork() {
        val stations = listOf(
            Station(
                id = 1,
                name = "With Art",
                url = "http://stream.example.com/radio",
                playlistId = null,
                sortOrder = 0,
                starred = false,
                defaultArtworkUrl = "http://art.example.com/logo.png"
            )
        )

        val result = PlaylistExporter.toM3u(stations)

        assertTrue(result.contains("#EXTM3U"))
        assertTrue(result.contains("#EXTINF:-1,With Art"))
        assertTrue(result.contains("#EXTIMG:http://art.example.com/logo.png"))
        assertTrue(result.contains("http://stream.example.com/radio"))
    }

    @Test
    fun exportStationsToM3uFormatWithoutArtwork() {
        val stations = listOf(
            Station(
                id = 1,
                name = "No Art",
                url = "http://stream.example.com/radio",
                playlistId = null,
                sortOrder = 0,
                starred = false,
                defaultArtworkUrl = null
            )
        )

        val result = PlaylistExporter.toM3u(stations)

        assertTrue(result.contains("#EXTM3U"))
        assertTrue(result.contains("#EXTINF:-1,No Art"))
        assertFalse(result.contains("#EXTIMG:"))
        assertTrue(result.contains("http://stream.example.com/radio"))
    }

    @Test
    fun exportStationsToPlsFormat() {
        val stations = listOf(
            Station(
                id = 1,
                name = "PLS One",
                url = "http://stream1.example.com/radio",
                playlistId = null,
                sortOrder = 0,
                starred = false,
                defaultArtworkUrl = null
            ),
            Station(
                id = 2,
                name = "PLS Two",
                url = "http://stream2.example.com/radio",
                playlistId = null,
                sortOrder = 1,
                starred = false,
                defaultArtworkUrl = null
            )
        )

        val result = PlaylistExporter.toPls(stations)

        assertTrue(result.contains("[playlist]"))
        assertTrue(result.contains("File1=http://stream1.example.com/radio"))
        assertTrue(result.contains("Title1=PLS One"))
        assertTrue(result.contains("File2=http://stream2.example.com/radio"))
        assertTrue(result.contains("Title2=PLS Two"))
        assertTrue(result.contains("NumberOfEntries=2"))
        assertTrue(result.contains("Version=2"))
    }

    @Test
    fun roundTripCreateStationsExportParseVerify() {
        val stations = listOf(
            Station(
                id = 1,
                name = "Round Trip One",
                url = "https://one.example.com/stream",
                playlistId = null,
                sortOrder = 0,
                starred = false,
                defaultArtworkUrl = "http://art.example.com/one.png"
            ),
            Station(
                id = 2,
                name = "Round Trip Two",
                url = "https://two.example.com/live",
                playlistId = null,
                sortOrder = 1,
                starred = false,
                defaultArtworkUrl = null
            )
        )

        val m3uExported = PlaylistExporter.toM3u(stations)
        val m3uParsed = M3uParser.parse(m3uExported)

        assertEquals(2, m3uParsed.size)
        assertEquals("Round Trip One", m3uParsed[0].name)
        assertEquals("https://one.example.com/stream", m3uParsed[0].url)
        assertEquals("http://art.example.com/one.png", m3uParsed[0].artworkUrl)
        assertEquals("Round Trip Two", m3uParsed[1].name)
        assertEquals("https://two.example.com/live", m3uParsed[1].url)
        assertEquals(null, m3uParsed[1].artworkUrl)

        val plsExported = PlaylistExporter.toPls(stations)
        val plsParsed = PlsParser.parse(plsExported)

        assertEquals(2, plsParsed.size)
        assertEquals("Round Trip One", plsParsed[0].name)
        assertEquals("https://one.example.com/stream", plsParsed[0].url)
        assertEquals("Round Trip Two", plsParsed[1].name)
        assertEquals("https://two.example.com/live", plsParsed[1].url)
    }
}
