package com.wisp.app.ui.component

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Checkbox
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.wisp.app.nostr.BookmarkSet

@Composable
fun AddNoteToListDialog(
    eventId: String,
    bookmarkSets: List<BookmarkSet>,
    isBookmarked: Boolean = false,
    onToggleBookmark: ((String) -> Unit)? = null,
    onAddToSet: (dTag: String, eventId: String) -> Unit,
    onRemoveFromSet: (dTag: String, eventId: String) -> Unit,
    onCreateSet: (name: String) -> Unit,
    onDismiss: () -> Unit
) {
    var newListName by remember { mutableStateOf("") }

    AlertDialog(
        onDismissRequest = onDismiss,
        title = { Text("Add to List") },
        text = {
            Column {
                // Global Bookmarks row
                if (onToggleBookmark != null) {
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .fillMaxWidth()
                            .clickable { onToggleBookmark(eventId) }
                            .padding(vertical = 8.dp)
                    ) {
                        Checkbox(
                            checked = isBookmarked,
                            onCheckedChange = { onToggleBookmark(eventId) }
                        )
                        Spacer(Modifier.width(8.dp))
                        Text(
                            "Bookmarks",
                            style = MaterialTheme.typography.bodyLarge,
                            modifier = Modifier.weight(1f)
                        )
                    }
                }

                bookmarkSets.forEach { set ->
                    val isInSet = eventId in set.eventIds
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        modifier = Modifier
                            .fillMaxWidth()
                            .clickable {
                                if (isInSet) onRemoveFromSet(set.dTag, eventId)
                                else onAddToSet(set.dTag, eventId)
                            }
                            .padding(vertical = 8.dp)
                    ) {
                        Checkbox(
                            checked = isInSet,
                            onCheckedChange = {
                                if (isInSet) onRemoveFromSet(set.dTag, eventId)
                                else onAddToSet(set.dTag, eventId)
                            }
                        )
                        Spacer(Modifier.width(8.dp))
                        Text(
                            set.name,
                            style = MaterialTheme.typography.bodyLarge,
                            modifier = Modifier.weight(1f)
                        )
                        Text(
                            "${set.eventIds.size}",
                            style = MaterialTheme.typography.bodySmall,
                            color = MaterialTheme.colorScheme.onSurfaceVariant
                        )
                    }
                }
                Spacer(Modifier.height(12.dp))
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    OutlinedTextField(
                        value = newListName,
                        onValueChange = { new -> if (!NsecPasteGuard.blockIfNsec(newListName, new)) newListName = new },
                        placeholder = { Text("New list name") },
                        singleLine = true,
                        modifier = Modifier.weight(1f)
                    )
                    Spacer(Modifier.width(8.dp))
                    TextButton(
                        onClick = {
                            if (newListName.isNotBlank()) {
                                onCreateSet(newListName.trim())
                                newListName = ""
                            }
                        },
                        enabled = newListName.isNotBlank()
                    ) {
                        Text("Create")
                    }
                }
            }
        },
        confirmButton = {
            TextButton(onClick = onDismiss) { Text("Done") }
        }
    )
}
