package com.ponzischeme89.memby.ui

import android.graphics.BitmapFactory
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onRoot
import androidx.test.core.app.ApplicationProvider
import com.github.takahirom.roborazzi.captureRoboImage
import com.ponzischeme89.memby.ServiceLocator
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
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 RecommendationOnboardingScreenshotTest {
    @get:Rule
    val compose = createComposeRule()

    @Before
    fun locator() {
        ServiceLocator.init(ApplicationProvider.getApplicationContext())
    }

    @Test
    fun `movie taste selection`() = capture(
        stageIndex = 0,
        heading = "Choose movies you love",
        fileName = "recommendation-onboarding-movies",
    )

    @Test
    fun `actor taste selection`() = capture(
        stageIndex = 2,
        heading = "Pick actors you enjoy watching",
        fileName = "recommendation-onboarding-actors",
    )

    private fun capture(stageIndex: Int, heading: String, fileName: String) {
        val previewFocus = FocusRequester()
        val artwork = requireNotNull(javaClass.getResourceAsStream("/home_hero_preview_art.png"))
            .use(BitmapFactory::decodeStream)
            .asImageBitmap()
        compose.setContent {
            RecommendationOnboardingPreview(
                initialStageIndex = stageIndex,
                previewArtwork = artwork,
                previewFocusRequester = previewFocus,
            )
        }
        compose.runOnIdle { previewFocus.requestFocus() }
        compose.onNodeWithText(heading).fetchSemanticsNode()
        compose.onRoot().captureRoboImage(
            "build/screenshots/recommendation-onboarding/$fileName.png",
        )
    }
}
