package xyz.cottongin.radio247.data.importing

import xyz.cottongin.radio247.data.model.Station

object PlaylistExporter {
    fun toM3u(stations: List<Station>): String {
        val sb = StringBuilder()
        sb.append("#EXTM3U\n")
        for (station in stations) {
            val safeName = station.name.replace("\n", " ").replace("\r", " ")
            sb.append("#EXTINF:-1,").append(safeName).append("\n")
            if (station.defaultArtworkUrl != null) {
                sb.append("#EXTIMG:").append(station.defaultArtworkUrl).append("\n")
            }
            sb.append(station.url).append("\n")
        }
        return sb.toString()
    }

    fun toPls(stations: List<Station>): String {
        val sb = StringBuilder()
        sb.append("[playlist]\n")
        stations.forEachIndexed { index, station ->
            val safeName = station.name.replace("\n", " ").replace("\r", " ")
            sb.append("File").append(index + 1).append("=").append(station.url).append("\n")
            sb.append("Title").append(index + 1).append("=").append(safeName).append("\n")
        }
        sb.append("NumberOfEntries=").append(stations.size).append("\n")
        sb.append("Version=2\n")
        return sb.toString()
    }
}
