package com.ponzischeme89.memby.ui.player

import android.app.Activity
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.github.takahirom.roborazzi.captureRoboImage
import com.ponzischeme89.memby.R
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode

@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(sdk = [34], qualifiers = "w960dp-h540dp-television-xhdpi")
class PlayerPauseOverlayScreenshotTest {
    @Test
    fun `paused movie shows focused poster synopsis and resume controls`() {
        val (activity, root, controls) = playerSurface()

        controls.findViewById<View>(R.id.player_pause_overlay).visibility = View.VISIBLE
        controls.findViewById<View>(R.id.player_now_playing_group).visibility = View.GONE
        controls.findViewById<TextView>(R.id.player_pause_title).text = "The Last Horizon"
        controls.findViewById<TextView>(R.id.player_pause_overview).text =
            "A cartographer follows a signal beyond the edge of the known world, " +
                "where an abandoned observatory may hold the way home."
        controls.findViewById<ImageView>(R.id.player_pause_poster).setImageBitmap(previewArtwork())
        controls.findViewById<TextView>(androidx.media3.ui.R.id.exo_position).text = "42:18"
        controls.findViewById<TextView>(androidx.media3.ui.R.id.exo_duration).text = "1:54:02"
        controls.findViewById<TextView>(R.id.player_remaining).text = "1h 12m left"
        controls.findViewById<TextView>(R.id.player_finish_time).text = "Ends at 10:14 PM"

        root.captureRoboImage("build/screenshots/player-pause-overlay/player-paused-movie-overlay.png")
    }

    @Test
    fun `playing OSD keeps video visible behind controls without black container`() {
        val (_, root, controls) = playerSurface()

        controls.findViewById<View>(R.id.player_pause_overlay).visibility = View.GONE
        controls.findViewById<View>(R.id.player_title_logo).visibility = View.GONE
        controls.findViewById<TextView>(R.id.player_title).apply {
            text = "The Last Horizon"
            visibility = View.VISIBLE
        }
        controls.findViewById<TextView>(androidx.media3.ui.R.id.exo_position).text = "42:18"
        controls.findViewById<TextView>(androidx.media3.ui.R.id.exo_duration).text = "1:54:02"
        controls.findViewById<TextView>(R.id.player_remaining).text = "1h 12m left"
        controls.findViewById<TextView>(R.id.player_finish_time).text = "Ends at 10:14 PM"

        root.captureRoboImage("build/screenshots/player-pause-overlay/player-osd-no-black-container.png")
    }

    private fun playerSurface(): Triple<Activity, FrameLayout, View> {
        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
        val root = FrameLayout(activity)
        root.addView(
            ImageView(activity).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
                setImageBitmap(previewArtwork())
            },
            FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT,
            ),
        )
        val controls = FrameLayout(activity)
        root.addView(
            controls,
            FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT,
            ),
        )
        LayoutInflater.from(activity).inflate(R.layout.memby_player_controls, controls, true)
        activity.setContentView(root)
        return Triple(activity, root, controls)
    }

    private fun previewArtwork() =
        javaClass.classLoader
            ?.getResourceAsStream("home_hero_preview_art.png")
            ?.use(BitmapFactory::decodeStream)
}
