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

import android.content.ClipData
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboard
import androidx.compose.ui.platform.toClipEntry
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.ui.R
import com.github.nullptroma.wallenc.ui.elements.WallencScreenContentPadding
import com.github.nullptroma.wallenc.ui.elements.WallencScreenScaffold
import com.github.nullptroma.wallenc.ui.resources.resolveText
import kotlinx.coroutines.launch

@Composable
fun TextSecretDetailsScreen(
    modifier: Modifier = Modifier,
    viewModel: TextSecretDetailsViewModel = hiltViewModel(),
    onEdit: (String) -> Unit,
    onDeleted: () -> Unit,
) {
    val uiState by viewModel.state.collectAsStateWithLifecycle()
    val secret = uiState.secret
    val clipboard = LocalClipboard.current
    val scope = rememberCoroutineScope()

    WallencScreenScaffold(modifier = modifier) { innerPadding ->
        WallencScreenContentPadding(innerPadding) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.spacedBy(10.dp),
        ) {
            uiState.errorNotification?.let { notification ->
                Text(
                    text = notification.resolveText(),
                    color = MaterialTheme.colorScheme.error,
                )
            }
            if (uiState.isMutating) {
                LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
            }
            if (secret == null) return@Column

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
            ) {
                Text(
                    text = secret.title,
                    style = MaterialTheme.typography.headlineSmall,
                )
                TextButton(
                    onClick = { onEdit(secret.id) },
                    enabled = uiState.isAvailable && !uiState.isMutating,
                ) {
                    Text(stringResource(R.string.edit))
                }
            }

            LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) {
                items(secret.items) { item ->
                    val clipboardFallbackLabel = stringResource(R.string.text_secret_clipboard_fallback_label)
                    Card(
                        modifier = Modifier.fillMaxWidth(),
                        colors = CardDefaults.elevatedCardColors(
                            containerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
                        ),
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(12.dp),
                            verticalArrangement = Arrangement.spacedBy(4.dp),
                        ) {
                            Text(
                                text = item.label ?: stringResource(R.string.text_secret_item_without_label),
                                style = MaterialTheme.typography.labelMedium,
                                color = MaterialTheme.colorScheme.onSurfaceVariant,
                            )
                            Row(
                                modifier = Modifier.fillMaxWidth(),
                                horizontalArrangement = Arrangement.spacedBy(8.dp),
                            ) {
                                Text(
                                    text = item.value,
                                    style = MaterialTheme.typography.bodyLarge,
                                    modifier = Modifier.weight(1f),
                                )
                                IconButton(
                                    onClick = {
                                        scope.launch {
                                            val clipData = ClipData.newPlainText(
                                                item.label ?: clipboardFallbackLabel,
                                                item.value,
                                            )
                                            clipboard.setClipEntry(clipData.toClipEntry())
                                        }
                                    },
                                ) {
                                    Icon(
                                        imageVector = Icons.Default.ContentCopy,
                                        contentDescription = stringResource(R.string.text_secret_copy_value),
                                    )
                                }
                            }
                        }
                    }
                }
            }

            Button(
                onClick = { viewModel.delete(onDeleted) },
                enabled = uiState.isAvailable && !uiState.isMutating,
            ) {
                Text(stringResource(R.string.remove))
            }
        }
        }
    }
}
