package com.ponzischeme89.memby.ui.player

import android.app.Activity
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.GridLayout
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 PrerollScreenshotTest {
    @Test
    fun `sonarr preroll uses hero video and artwork stack`() {
        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
        val root = FrameLayout(activity)
        val preroll = LayoutInflater.from(activity).inflate(R.layout.player_preroll, root, false)
        preroll.visibility = View.VISIBLE
        root.addView(
            preroll,
            FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,
            ),
        )

        val artwork = javaClass.classLoader
            ?.getResourceAsStream("home_hero_preview_art.png")
            ?.use(BitmapFactory::decodeStream)
        preroll.findViewById<FrameLayout>(R.id.player_preroll_video_host).addView(
            ImageView(activity).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
                setImageBitmap(artwork)
            },
            0,
            FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,
            ),
        )

        val samples = listOf(
            Triple("TODAY · 8:30 PM", "Northbound", "S02E04 · The Crossing"),
            Triple("TODAY · 9:00 PM", "Harbour", "S03E01 · Home Water"),
            Triple("TODAY · 9:30 PM", "The Bear", "S04E03 · Bridges"),
            Triple("TODAY · 10:00 PM", "Slow Horses", "S05E02 · Signals"),
            Triple("WED · 7:30 PM", "Foundation", "S03E06 · The Mule"),
            Triple("THU · 8:00 PM", "Severance", "S03E01 · Cold Harbour"),
            Triple("FRI · 8:30 PM", "Silo", "S03E04 · Legacy"),
            Triple("SAT · 7:00 PM", "North Shore", "S01E07 · The Dive"),
        )
        preroll.findViewById<TextView>(R.id.player_preroll_now_title).text =
            "Northbound – The Crossing"
        preroll.findViewById<TextView>(R.id.player_preroll_now_metadata).text =
            "EPISODE  ·  S02E04  ·  48 mins"
        preroll.findViewById<TextView>(R.id.player_preroll_now_overview).text =
            "The crew follows a signal beyond the last charted crossing and discovers " +
                "a settlement that has been waiting for their arrival."
        val calendar = preroll.findViewById<GridLayout>(R.id.player_preroll_calendar)
        samples.forEachIndexed { index, (label, title, detail) ->
            val card = LayoutInflater.from(activity)
                .inflate(R.layout.player_preroll_schedule_card, calendar, false)
            card.findViewById<ImageView>(R.id.player_preroll_card_artwork).setImageBitmap(artwork)
            card.findViewById<TextView>(R.id.player_preroll_card_label).text = label
            card.findViewById<TextView>(R.id.player_preroll_card_title).text = title
            card.findViewById<TextView>(R.id.player_preroll_card_detail).text = detail
            calendar.addView(
                card,
                GridLayout.LayoutParams().apply {
                    width = 0
                    height = dp(activity, 80)
                    columnSpec = GridLayout.spec(index % 4, 1f)
                    rowSpec = GridLayout.spec(index / 4)
                    setMargins(dp(activity, 4), dp(activity, 3), dp(activity, 4), dp(activity, 3))
                },
            )
        }
        val countdown = preroll.findViewById<PrerollCountdownView>(R.id.player_preroll_countdown)
        countdown.setCountdown(
            seconds = 7,
            progress = 1f,
            description = "Starting in 7 seconds",
        )

        activity.setContentView(root)
        root.captureRoboImage("build/screenshots/sonarr-preroll/player-sonarr-preroll.png")

        countdown.setCountdown(seconds = 3, progress = 3f / 6.5f, description = "Starting in 3 seconds")
        root.captureRoboImage(
            "build/screenshots/sonarr-preroll/player-sonarr-preroll-mid-countdown.png",
        )
    }

    private fun dp(activity: Activity, value: Int): Int =
        (value * activity.resources.displayMetrics.density).toInt()
}
