package eu.friedcutlet.yokan.services.media.cast import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.StateFlow data class CastDevice(val id: String, val name: String, val host: String = "", val port: Int = 8009) data class CastSubtitle(val url: String, val label: String, val language: String? = null) data class CastTextStyle( val foregroundArgb: Int = 0xFFFFFFFF.toInt(), val edgeArgb: Int = 0xFF000000.toInt(), val backgroundArgb: Int = 0, val fontScale: Double = 1.0, val bold: Boolean = false, val italic: Boolean = false, ) data class CastMedia( val url: String, val title: String, val subtitle: String? = null, val contentType: String? = null, val posterUrl: String? = null, val subtitles: List = emptyList(), val itemKey: String = url, ) data class CastStatus( val device: CastDevice, val playing: Boolean = false, val buffering: Boolean = false, val positionMs: Long = 0, val durationMs: Long = 0, val queue: List = emptyList(), /** Index into [queue] of the receiver's current item, -1 when idle/unknown. */ val currentIndex: Int = -1, /** Receiver went IDLE with reason FINISHED after the last item. */ val finished: Boolean = false, val error: String? = null, ) /** * Casts to Chromecast's Default Media Receiver. The receiver fetches media URLs itself, so streams that need * request headers (referer, cookies, auth) or are only reachable from this device cannot play there. */ interface CastController { /** False where the platform has no casting path at runtime (e.g. non-Chromium browser). */ val isSupported: StateFlow /** True when the platform shows its own device picker (web Cast SDK); connect(null) opens it. */ val usesSystemPicker: Boolean val devices: StateFlow> /** Null when no session. */ val status: StateFlow fun startDiscovery() fun stopDiscovery() /** Throws on failure; launches the Default Media Receiver (app id CC1AD845). */ suspend fun connect(device: CastDevice?) suspend fun loadQueue( items: List, startIndex: Int, startPositionMs: Long, style: CastTextStyle, activeSubtitleIndex: Int?, ) /** Queue insert at end. */ suspend fun append(items: List) fun play() fun pause() fun seekTo(positionMs: Long) fun next() fun previous() fun jumpTo(index: Int) /** Stops the receiver app and closes the transport. */ suspend fun disconnect() /** Owner shutdown: cancels jobs and closes sockets; idempotent. */ fun close() } expect fun createCastController(scope: CoroutineScope): CastController