package chat.vojo.proxy.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
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.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import chat.vojo.proxy.ui.theme.Vojo

@Composable
fun VojoTextField(
    value: String,
    onValueChange: (String) -> Unit,
    label: String,
    placeholder: String = "",
    modifier: Modifier = Modifier,
    keyboardType: KeyboardType = KeyboardType.Text,
    mono: Boolean = false,
    isPassword: Boolean = false,
    error: String? = null,
) {
    var reveal by rememberSaveable { mutableStateOf(false) }
    Column(modifier) {
        SectionLabel(label)
        Spacer(Modifier.height(6.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .clip(RoundedCornerShape(10.dp))
                .background(Vojo.bgApp)
                .border(1.dp, if (error != null) Vojo.danger.copy(alpha = 0.55f) else Vojo.hairline, RoundedCornerShape(10.dp))
                .padding(horizontal = 12.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            BasicTextField(
                value = value,
                onValueChange = onValueChange,
                singleLine = true,
                modifier = Modifier.weight(1f),
                textStyle = TextStyle(
                    color = Vojo.text,
                    fontSize = 14.sp,
                    fontFamily = if (mono) Vojo.mono else FontFamily.Default,
                ),
                cursorBrush = SolidColor(Vojo.fleet),
                keyboardOptions = KeyboardOptions(
                    keyboardType = if (isPassword) KeyboardType.Password else keyboardType,
                ),
                visualTransformation = if (isPassword && !reveal) PasswordVisualTransformation() else VisualTransformation.None,
                decorationBox = { inner ->
                    Box(Modifier.padding(vertical = 15.dp)) {
                        if (value.isEmpty()) {
                            Text(
                                placeholder,
                                color = Vojo.faint,
                                fontSize = 14.sp,
                                fontFamily = if (mono) Vojo.mono else FontFamily.Default,
                            )
                        }
                        inner()
                    }
                },
            )
            if (isPassword) {
                Icon(
                    if (reveal) VojoIcons.EyeOff else VojoIcons.Eye,
                    contentDescription = if (reveal) "Скрыть пароль" else "Показать пароль",
                    tint = Vojo.faint,
                    modifier = Modifier
                        .clip(RoundedCornerShape(6.dp))
                        .clickable { reveal = !reveal }
                        .padding(6.dp)
                        .size(16.dp),
                )
            }
        }
        if (error != null) {
            Spacer(Modifier.height(4.dp))
            Text(error, color = Vojo.danger, fontSize = 11.sp)
        }
    }
}

@Composable
fun <T> VojoDropdown(
    label: String,
    options: List<T>,
    selected: T,
    optionLabel: (T) -> String,
    onSelect: (T) -> Unit,
    modifier: Modifier = Modifier,
) {
    var expanded by remember { mutableStateOf(false) }
    Column(modifier) {
        SectionLabel(label)
        Spacer(Modifier.height(6.dp))
        Box {
            Row(
                Modifier
                    .fillMaxWidth()
                    .height(52.dp)
                    .clip(RoundedCornerShape(10.dp))
                    .background(Vojo.bgApp)
                    .border(1.dp, Vojo.hairline, RoundedCornerShape(10.dp))
                    .clickable { expanded = true }
                    .padding(horizontal = 12.dp),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Text(optionLabel(selected), color = Vojo.text, fontSize = 15.sp, modifier = Modifier.weight(1f))
                Icon(VojoIcons.ChevronDown, contentDescription = null, tint = Vojo.muted, modifier = Modifier.size(14.dp))
            }
            DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
                options.forEach { opt ->
                    DropdownMenuItem(
                        text = { Text(optionLabel(opt), color = Vojo.text, fontSize = 14.sp) },
                        onClick = { onSelect(opt); expanded = false },
                    )
                }
            }
        }
    }
}
