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

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.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight
import androidx.compose.material.icons.automirrored.outlined.Notes
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.github.nullptroma.wallenc.domain.datatypes.TextSecretRecord
import com.github.nullptroma.wallenc.ui.R
import com.github.nullptroma.wallenc.ui.resources.resolveText

@Composable
fun TextSecretsScreenContent(
    uiState: TextSecretsScreenState,
    modifier: Modifier = Modifier,
    secretList: @Composable () -> Unit = {},
) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        uiState.errorNotification?.let { notification ->
            Text(
                text = notification.resolveText(),
                color = MaterialTheme.colorScheme.error,
            )
        }

        if (uiState.items.isEmpty()) {
            Text(stringResource(R.string.text_secret_empty_state))
        } else {
            secretList()
        }
    }
}

@Composable
fun TextSecretListCard(
    secret: TextSecretRecord,
    onClick: () -> Unit,
    enabled: Boolean,
    modifier: Modifier = Modifier,
) {
    Card(
        modifier = modifier.fillMaxWidth(),
        onClick = onClick,
        enabled = enabled,
        colors = CardDefaults.elevatedCardColors(
            containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
        ),
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(14.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.Top,
        ) {
            Row(
                modifier = Modifier.weight(1f),
                horizontalArrangement = Arrangement.spacedBy(12.dp),
            ) {
                Icon(
                    imageVector = Icons.AutoMirrored.Outlined.Notes,
                    contentDescription = null,
                    modifier = Modifier
                        .size(22.dp)
                        .padding(top = 2.dp),
                    tint = MaterialTheme.colorScheme.primary,
                )
                Column(modifier = Modifier.padding(end = 12.dp)) {
                    Text(
                        text = secret.title,
                        style = MaterialTheme.typography.titleSmall,
                    )
                    Text(
                        text = stringResource(R.string.text_secret_items_count, secret.items.size),
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                    )
                    val fieldNames = secret.items
                        .map { item ->
                            item.label
                                ?.trim()
                                .takeUnless { it.isNullOrBlank() }
                                ?: stringResource(R.string.text_secret_item_without_label)
                        }
                    if (fieldNames.isNotEmpty()) {
                        fieldNames.take(3).forEach { fieldName ->
                            Text(
                                text = "\u2022 $fieldName",
                                style = MaterialTheme.typography.bodySmall,
                                color = MaterialTheme.colorScheme.onSurfaceVariant,
                            )
                        }
                        val hiddenCount = fieldNames.size - 3
                        if (hiddenCount > 0) {
                            Text(
                                text = stringResource(
                                    R.string.text_secret_more_fields,
                                    hiddenCount,
                                ),
                                style = MaterialTheme.typography.bodySmall,
                                color = MaterialTheme.colorScheme.onSurfaceVariant,
                            )
                        }
                    }
                }
            }
            Icon(
                imageVector = Icons.AutoMirrored.Filled.KeyboardArrowRight,
                contentDescription = null,
            )
        }
    }
}
