package com.ponzischeme89.memby.ui

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.test.core.app.ApplicationProvider
import androidx.tv.material3.Text
import com.github.takahirom.roborazzi.captureRoboImage
import com.ponzischeme89.memby.ServiceLocator
import com.ponzischeme89.memby.data.model.BaseItem
import com.ponzischeme89.memby.data.model.UserItemData
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 WatchedVisibilityScreenshotTest {
    @get:Rule
    val compose = createComposeRule()

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

    @Test
    fun `browse cards hide watched movies and count watched episodes`() {
        val visible = applyWatchedVisibility(listOf(row), enabled = true).single()

        compose.setContent {
            PreviewSurface {
                Column(Modifier.fillMaxSize().padding(48.dp)) {
                    Text("Because you like great stories", color = Color.White, fontSize = 22.sp)
                    Row(
                        modifier = Modifier.padding(top = 18.dp),
                        horizontalArrangement = Arrangement.spacedBy(18.dp),
                    ) {
                        visible.items.forEach { item ->
                            PortraitMediaCard(
                                item = item,
                                availableWidth = 820.dp,
                                showSecondaryMetadata = true,
                                onFocused = {},
                                onClick = {},
                                onLongClick = {},
                                density = "large",
                                showWatchedEpisodeCount = visible.showWatchedEpisodeCount,
                            )
                        }
                    }
                }
            }
        }

        compose.onRoot().captureRoboImage(
            "build/screenshots/watched-visibility/watched-visibility-row.png",
        )
    }

    private val row = HomeBrowseRow(
        id = "stories",
        title = "Stories",
        kind = MediaRowKind.MOVIES,
        emptyMessage = "Nothing here",
        items = listOf(
            BaseItem(
                id = "seen",
                name = "Already Seen",
                type = "Movie",
                userData = UserItemData(played = true),
            ),
            BaseItem(
                id = "signal",
                name = "Signal House",
                type = "Series",
                recursiveItemCount = 10,
                userData = UserItemData(unplayedItemCount = 2),
            ),
            BaseItem(
                id = "winter",
                name = "A Long Winter",
                type = "Series",
                recursiveItemCount = 24,
                userData = UserItemData(unplayedItemCount = 19),
            ),
            BaseItem(id = "new", name = "Not Watched Yet", type = "Movie", productionYear = 2026),
        ),
    )
}
