package fr.univpau.queezer.manager

import fr.univpau.queezer.data.Input
import fr.univpau.queezer.data.Playlist
import fr.univpau.queezer.data.Track
import fr.univpau.queezer.service.PlaylistResponse
import fr.univpau.queezer.service.createDeezerApiService
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

suspend fun fetchPlaylist(apiUrl: String) : Playlist? {
    val deezerApiService = createDeezerApiService()

    return withContext(Dispatchers.IO) {
        try {
            val playlistResponse : PlaylistResponse = deezerApiService.getPlaylist(apiUrl)

            Playlist(
                title = playlistResponse.title,
                tracks = playlistResponse.tracks.data.map { track ->
                    Track(
                        preview = track.preview,
                        album = track.album.cover,
                        title = Input(value = track.title),
                        artist = Input(value = track.artist.name)
                    )
                }.shuffled()
            )
        } catch (e: Exception) {
            e.printStackTrace()
            null;
        }
    }
}