package com.github.nullptroma.wallenc.ui.screens.main.screens.storage.twofa

import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.github.nullptroma.wallenc.domain.datatypes.TwoFaTokenRecord
import com.github.nullptroma.wallenc.ui.R
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class TwoFaTokensScreenContentTest {

    @get:Rule
    val composeRule = createComposeRule()

    private val context get() = InstrumentationRegistry.getInstrumentation().targetContext

    @Test
    fun showsEmptyStateWhenNoTokens() {
        composeRule.setContent {
            MaterialTheme {
                TwoFaTokensScreenContent(
                    uiState = TwoFaTokensScreenState(
                        isLoading = false,
                        isAvailable = true,
                        items = emptyList(),
                    ),
                )
            }
        }
        composeRule.onNodeWithText(context.getString(R.string.two_fa_empty_state)).assertIsDisplayed()
    }

    @Test
    fun showsIssuerAndAccountForToken() {
        val token = TwoFaTokenRecord(
            id = "1",
            issuer = "GitHub",
            account = "user@example.com",
            secret = "SECRET",
        )
        composeRule.setContent {
            MaterialTheme {
                TwoFaTokensScreenContent(
                    uiState = TwoFaTokensScreenState(
                        isLoading = false,
                        isAvailable = true,
                        items = listOf(token),
                    ),
                ) {
                    TwoFaTokenListHeader(
                        issuer = token.issuer,
                        account = token.account,
                        modifier = Modifier.padding(8.dp),
                    )
                }
            }
        }
        composeRule.onNodeWithText("GitHub").assertIsDisplayed()
        composeRule.onNodeWithText("user@example.com").assertIsDisplayed()
    }
}
