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

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.nullptroma.wallenc.domain.datatypes.TextSecretRecord
import com.github.nullptroma.wallenc.ui.R
import com.github.nullptroma.wallenc.ui.elements.WallencScreenContentPadding
import com.github.nullptroma.wallenc.ui.elements.WallencScreenScaffold

@Composable
fun TextSecretsScreen(
    modifier: Modifier = Modifier,
    viewModel: TextSecretsViewModel = hiltViewModel(),
    onOpenSecret: (TextSecretRecord) -> Unit,
    onCreateSecret: () -> Unit,
) {
    val uiState by viewModel.state.collectAsStateWithLifecycle()

    WallencScreenScaffold(
        modifier = modifier,
        floatingActionButton = {
            FloatingActionButton(
                onClick = {
                    if (uiState.isAvailable) {
                        onCreateSecret()
                    }
                },
                modifier = Modifier.alpha(if (uiState.isAvailable) 1f else 0.5f),
            ) {
                Icon(Icons.Default.Add, contentDescription = stringResource(R.string.text_secret_create))
            }
        },
    ) { innerPadding ->
        WallencScreenContentPadding(innerPadding) {
        TextSecretsScreenContent(
            uiState = uiState,
            modifier = Modifier.fillMaxSize(),
        ) {
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                items(uiState.items) { secret ->
                    TextSecretListCard(
                        secret = secret,
                        onClick = { onOpenSecret(secret) },
                        enabled = uiState.isAvailable,
                    )
                }
            }
        }
        }
    }
}
