package com.ponzischeme89.memby.ui.player

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
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

/**
 * Captures the real player XML at TV resolution. The restrained blue-grey field stands
 * in for a dark film frame and makes the cue's contrast and content-width easy to judge.
 */
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(sdk = [34], qualifiers = "w960dp-h540dp-television-xhdpi")
class TimeRemainingCueScreenshotTest {

    @Test
    fun `compact time remaining cue over playback`() {
        captureCue(
            name = "player-time-remaining",
            label = "TIME REMAINING",
            value = "9 mins",
        )
    }

    @Test
    fun `compact finishes in cue over playback`() {
        captureCue(
            name = "player-finishes-in",
            label = "FINISHES IN",
            value = "42 mins (9:48 PM)",
        )
    }

    @Test
    fun `compact resumed time left cue over playback`() {
        captureCue(
            name = "player-resume-time-left",
            label = "TIME LEFT",
            value = "36 mins",
        )
    }

    private fun captureCue(name: String, label: String, value: String) {
        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 cue = LayoutInflater.from(activity).inflate(
            R.layout.player_time_remaining,
            root,
            false,
        )
        cue.visibility = View.VISIBLE
        cue.findViewById<TextView>(R.id.player_time_remaining_label).text = label
        cue.findViewById<TextView>(R.id.player_time_remaining_value).text = value
        root.addView(cue)
        activity.setContentView(root)

        root.captureRoboImage("build/screenshots/time-remaining-cue/$name.png")
    }
}
