// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later

package space.rcmd.android.sizzle.playback

import android.os.Looper
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player
import androidx.media3.common.SimpleBasePlayer
import androidx.media3.common.util.UnstableApi
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import space.rcmd.android.sizzle.audio.AudioEngine

/**
 * Adapts our custom [AudioEngine] to the media3 [Player] interface via
 * [SimpleBasePlayer], so a [androidx.media3.session.MediaSession] can expose the
 * transport to the system: lock-screen controls, Bluetooth/headset media buttons,
 * and the media notification. We only implement play/pause/stop — the tracker
 * itself is the real transport; this is the "remote control" surface.
 *
 * Call [refresh] whenever the engine's transport changes so the reported state
 * (and therefore the notification) stays in sync with on-screen play/pause.
 */
@UnstableApi
class EnginePlayer(private val engine: AudioEngine) : SimpleBasePlayer(Looper.getMainLooper()) {

    private val mediaItem = MediaItem.Builder()
        .setMediaId("sizzletracker-song")
        .setMediaMetadata(
            MediaMetadata.Builder()
                .setTitle("Sizzletracker")
                .setArtist("Song")
                .build(),
        )
        .build()

    override fun getState(): State {
        val playing = engine.transport.value.isPlaying
        return State.Builder()
            .setAvailableCommands(
                Player.Commands.Builder()
                    .addAll(
                        Player.COMMAND_PLAY_PAUSE,
                        Player.COMMAND_STOP,
                        Player.COMMAND_PREPARE,
                        Player.COMMAND_GET_CURRENT_MEDIA_ITEM,
                        Player.COMMAND_GET_METADATA,
                        Player.COMMAND_GET_TIMELINE,
                    )
                    .build(),
            )
            .setPlaybackState(Player.STATE_READY)
            .setPlayWhenReady(playing, Player.PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST)
            .setPlaylist(
                listOf(
                    MediaItemData.Builder("sizzletracker-song")
                        .setMediaItem(mediaItem)
                        .build(),
                ),
            )
            .build()
    }

    /** Publish the latest engine state to any connected media controllers. */
    fun refresh() = invalidateState()

    override fun handleSetPlayWhenReady(playWhenReady: Boolean): ListenableFuture<*> {
        if (playWhenReady) engine.play() else engine.pause()
        invalidateState()
        return Futures.immediateVoidFuture()
    }

    override fun handlePrepare(): ListenableFuture<*> = Futures.immediateVoidFuture()

    override fun handleStop(): ListenableFuture<*> {
        engine.stop()
        invalidateState()
        return Futures.immediateVoidFuture()
    }

    override fun handleRelease(): ListenableFuture<*> = Futures.immediateVoidFuture()
}
