package com.ponzischeme89.memby.ui.player

import android.app.Activity
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
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.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
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 PlaybackIdentityScreenshotTest {
    @Test
    fun `colour artwork keeps its original treatment`() {
        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
        val colourLogo = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888).apply {
            eraseColor(Color.rgb(82, 181, 75))
        }

        assertFalse(
            makeLogoVisibleOnDarkBackground(
                ImageView(activity),
                BitmapDrawable(activity.resources, colourLogo),
            ),
        )
    }

    @Test
    fun `black artwork is lightened for dark player surfaces`() {
        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
        val blackLogo = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888).apply {
            eraseColor(Color.BLACK)
        }

        assertTrue(
            makeLogoVisibleOnDarkBackground(
                ImageView(activity),
                BitmapDrawable(activity.resources, blackLogo),
            ),
        )
    }

    @Test
    fun `plain title and emby mark appear over playback for five seconds`() {
        val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
        val root = FrameLayout(activity)
        val backdrop = ImageView(activity).apply {
            scaleType = ImageView.ScaleType.CENTER_CROP
            setImageBitmap(
                javaClass.classLoader
                    ?.getResourceAsStream("home_hero_preview_art.png")
                    ?.use(BitmapFactory::decodeStream),
            )
        }
        root.addView(
            backdrop,
            FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT,
            ),
        )

        val identity = LayoutInflater.from(activity)
            .inflate(R.layout.player_playback_identity, root, false)
            .apply {
                visibility = View.VISIBLE
                alpha = 1f
            }
        identity.findViewById<TextView>(R.id.player_playback_identity_title).text = "Dark Matter"
        root.addView(identity)
        activity.setContentView(root)

        assertEquals(5_000L, PlayerActivity.PLAYBACK_IDENTITY_VISIBLE_MS)
        root.captureRoboImage(
            "build/screenshots/playback-identity/player-playback-identity.png",
        )
    }
}
