package com.ponzischeme89.memby.ui.player

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Shader
import android.graphics.drawable.GradientDrawable
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 com.ponzischeme89.memby.data.Trickplay
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

/**
 * The seek chip with and without a preview frame, captured from the real player XML at TV
 * resolution → `build/screenshots/seek-indicator/`.
 *
 * The case worth having is the *empty* one. A preview is a decoration on an indicator that
 * has always worked without it — a title with no thumbnails, a gateway that will not answer
 * and the moment before the first frame arrives all have to leave the chip looking exactly
 * as it did before this existed, and that is a property no assertion can check.
 */
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(sdk = [34], qualifiers = "w960dp-h540dp-television-xhdpi")
class SeekIndicatorScreenshotTest {

    @Test
    fun `skipping forward with a preview frame`() {
        capture(
            name = "seek-forward-with-preview",
            amount = "Forward 1 min 30 secs",
            position = "1:12:40 / 2:04:00",
            forward = true,
            withPreview = true,
        )
    }

    @Test
    fun `skipping back with a preview frame`() {
        capture(
            name = "seek-back-with-preview",
            amount = "Back 30 seconds",
            position = "0:41:10 / 2:04:00",
            forward = false,
            withPreview = true,
        )
    }

    @Test
    fun `a title with no previews keeps the chip it always had`() {
        capture(
            name = "seek-forward-no-preview",
            amount = "Forward 30 seconds",
            position = "1:12:40 / 2:04:00",
            forward = true,
            withPreview = false,
        )
    }

    private fun capture(
        name: String,
        amount: String,
        position: String,
        forward: Boolean,
        withPreview: Boolean,
    ) {
        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
        val root = FrameLayout(activity).apply {
            background = GradientDrawable(
                GradientDrawable.Orientation.TL_BR,
                intArrayOf(Color.rgb(42, 57, 70), Color.rgb(16, 25, 33), Color.rgb(4, 8, 12)),
            )
        }
        val chip = LayoutInflater.from(activity).inflate(R.layout.player_seek_indicator, root, false)
        chip.visibility = View.VISIBLE
        chip.findViewById<ImageView>(R.id.player_seek_glyph).setImageResource(
            if (forward) R.drawable.ic_player_forward else R.drawable.ic_player_rewind,
        )
        chip.findViewById<TextView>(R.id.player_seek_amount).text = amount
        chip.findViewById<TextView>(R.id.player_seek_position).text = position

        if (withPreview) {
            val preview = chip.findViewById<ImageView>(R.id.player_seek_preview)
            // Sized the way the real one is: from the frames' own shape rather than the
            // 16:9 placeholder the layout carries, which is what stops a 320x172 thumbnail
            // being letterboxed inside its own box.
            val track = Trickplay(itemId = "1", intervalMs = 10_000L, count = 700, width = 320, height = 172)
            preview.layoutParams = preview.layoutParams.apply {
                width = track.widthFor(height)
            }
            preview.setImageBitmap(thumbnail())
            preview.visibility = View.VISIBLE
        }

        root.addView(chip)
        activity.setContentView(root)
        root.captureRoboImage("build/screenshots/seek-indicator/$name.png")
    }

    /** Stands in for a frame of a film: the point is the chip around it, not the picture. */
    private fun thumbnail(): Bitmap {
        val bitmap = Bitmap.createBitmap(320, 172, Bitmap.Config.ARGB_8888)
        Canvas(bitmap).drawPaint(
            Paint().apply {
                shader = LinearGradient(
                    0f, 0f, 320f, 172f,
                    intArrayOf(Color.rgb(96, 84, 62), Color.rgb(38, 44, 58), Color.rgb(10, 12, 18)),
                    null,
                    Shader.TileMode.CLAMP,
                )
            },
        )
        return bitmap
    }
}
