package com.aves.google_cast.fast

import com.google.android.gms.cast.MediaInfo
import com.google.android.gms.cast.MediaMetadata
import com.google.android.gms.cast.MediaQueueItem
import com.google.android.gms.cast.MediaStatus

object FastQueueBuilder {

    fun buildItems(items: List<Map<*, *>>): List<MediaQueueItem> {
        return items.map { map ->
            val url = map["url"] as String
            val isVideo = map["isVideo"] as Boolean
            val title = map["title"] as? String ?: ""

            val metadata = MediaMetadata(
                if (isVideo)
                    MediaMetadata.MEDIA_TYPE_MOVIE
                else
                    MediaMetadata.MEDIA_TYPE_PHOTO
            ).apply {
                putString(MediaMetadata.KEY_TITLE, title)
            }

            val info = MediaInfo.Builder(url)
                .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                .setContentType(if (isVideo) "video/mp4" else "image/jpeg")
                .setMetadata(metadata)
                .build()

            MediaQueueItem.Builder(info)
                .setAutoplay(true)
                .setPreloadTime(10.0) // prefetch CAF
                .build()
        }
    }

    fun repeatAll(): Int = MediaStatus.REPEAT_MODE_REPEAT_ALL
}
