package com.peachpasswords.ui.vault import androidx.compose.animation.core.animateDpAsState import androidx.compose.animation.core.tween import androidx.compose.foundation.background import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement 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.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.text.BasicTextField import androidx.compose.foundation.verticalScroll import androidx.compose.material3.Slider import androidx.compose.material3.SliderDefaults 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.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight 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.em import androidx.compose.ui.unit.sp import androidx.compose.runtime.collectAsState import androidx.hilt.navigation.compose.hiltViewModel import com.peachpasswords.ui.components.CopyButton import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.peachColors import com.peachpasswords.ui.theme.PeachMotion import com.peachpasswords.ui.theme.PeachRadius import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground @Composable fun EntryEditScreen( id: String?, onBack: () -> Unit, vm: EntryEditViewModel = hiltViewModel(), ) { val f by vm.form.collectAsState() val usernameSuggestions by vm.usernameSuggestions.collectAsState() val accent = LocalPeachAccent.current ShellBackground { Column(Modifier.fillMaxSize().statusBarsPadding()) { Row( Modifier.fillMaxWidth().height(56.dp).padding(horizontal = 18.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceBetween, ) { Text("cancel", color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 12.sp, modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable(onClick = onBack).padding(8.dp)) Text(if (vm.isNew) "new entry" else "edit", color = peachColors.T1, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 16.sp) Text("save", color = accent, fontFamily = PeachType.Mono, fontSize = 13.sp, modifier = Modifier.clip(RoundedCornerShape(8.dp)).clickable { vm.save(onBack) }.padding(8.dp)) } Box(Modifier.fillMaxSize()) { // Ghost PeachScript watermark behind the form (matches the detail hero). if (f.title.isNotBlank()) { Text( text = f.title.first().uppercase(), style = TextStyle(fontFamily = PeachType.Script, fontSize = 140.sp), color = peachColors.T0.copy(alpha = 0.03f), modifier = Modifier.align(Alignment.TopEnd).offset(x = (-16).dp, y = 8.dp), ) } Column( Modifier.fillMaxSize().verticalScroll(rememberScrollState()).padding(horizontal = 22.dp), ) { Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { listOf("login", "note", "card").forEach { t -> EntryTypePill(t, f.type == t) { vm.setType(t) } } } Spacer(Modifier.height(18.dp)) Field("title", f.title, vm::setTitle) if (f.type == "login") { Field("url", f.url, vm::setUrl) AdditionalUrlsEditor( urls = f.additionalUrls, onChange = vm::setAdditionalUrl, onAdd = vm::addAdditionalUrl, onRemove = vm::removeAdditionalUrl, ) IdentitySuggestField("username", f.username, usernameSuggestions, vm::setUsername) IdentitySuggestField("email", f.email, usernameSuggestions, vm::setEmail) PasswordField( value = f.password, show = f.showPassword, onChange = vm::setPassword, onToggleShow = vm::toggleShowPassword, onToggleGenerator = vm::toggleGenerator, ) GeneratorDrawer( open = f.generatorOpen, length = f.genLength, upper = f.genUpper, lower = f.genLower, numbers = f.genNumbers, symbols = f.genSymbols, password = f.password, copied = f.genCopied, onLength = vm::setGenLength, onUpper = vm::setGenUpper, onLower = vm::setGenLower, onNumbers = vm::setGenNumbers, onSymbols = vm::setGenSymbols, onGenerate = vm::generate, onCopy = vm::copyGenerated, ) PasswordHistorySection( history = f.history, expanded = f.showHistory, onToggle = vm::toggleHistory, onRestore = vm::restorePassword, onCopy = vm::copyHistoryPassword, ) Field("2FA secret (otpauth:// or base32)", f.totp, vm::setTotp) } if (f.type == "card") { CardEditFields( cardNumber = f.cardNumber, cardholderName = f.cardholderName, expiry = f.expiry, cvc = f.cvc, showCvc = f.showCvc, network = f.cardNetwork, onCardNumber = vm::setCardNumber, onCardholder = vm::setCardholderName, onExpiry = vm::setExpiry, onCvc = vm::setCvc, onToggleShowCvc = vm::toggleShowCvc, ) } Field("notes", f.notes, vm::setNotes, singleLine = false) TagChipEditor( tags = f.tags, allTags = vm.allTags, onAdd = vm::addTag, onRemove = vm::removeTag, ) f.error?.let { Spacer(Modifier.height(8.dp)) Text(it, color = peachColors.Breach, fontFamily = PeachType.Mono, fontSize = 11.sp) } Spacer(Modifier.height(40.dp)) } } } } } @Composable private fun EntryTypePill(type: String, selected: Boolean, onClick: () -> Unit) { val accent = LocalPeachAccent.current Box( modifier = Modifier .clip(RoundedCornerShape(PeachRadius.TypeTab)) .border( 1.dp, if (selected) accent.copy(alpha = 0.5f) else peachColors.T3, RoundedCornerShape(PeachRadius.TypeTab), ) .background(if (selected) accent.copy(alpha = 0.08f) else Color.Transparent) .clickable(onClick = onClick) .padding(horizontal = 12.dp, vertical = 5.dp), ) { Text( text = type.uppercase(), color = if (selected) accent else peachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.08.em, ) } } @Composable private fun Field( label: String, value: String, onChange: (String) -> Unit, singleLine: Boolean = true, transformation: VisualTransformation = VisualTransformation.None, trailing: (@Composable () -> Unit)? = null, ) { val accent = LocalPeachAccent.current Column(Modifier.fillMaxWidth().padding(bottom = 14.dp)) { Text(label.uppercase(), color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Spacer(Modifier.height(6.dp)) Row(verticalAlignment = Alignment.CenterVertically) { Box( Modifier.weight(1f) .border(1.dp, peachColors.T3, RoundedCornerShape(10.dp)) .padding(horizontal = 12.dp, vertical = 12.dp), ) { BasicTextField( value = value, onValueChange = onChange, singleLine = singleLine, textStyle = TextStyle(color = peachColors.T0, fontFamily = PeachType.Mono, fontSize = 13.sp), cursorBrush = SolidColor(accent), visualTransformation = transformation, modifier = Modifier.fillMaxWidth(), ) } if (trailing != null) { Spacer(Modifier.width(8.dp)); trailing() } } } } @Composable private fun PasswordField( value: String, show: Boolean, onChange: (String) -> Unit, onToggleShow: () -> Unit, onToggleGenerator: () -> Unit, ) { Field( label = "password", value = value, onChange = onChange, transformation = if (show) VisualTransformation.None else PasswordVisualTransformation(), trailing = { Text(if (show) "hide" else "show", color = LocalPeachAccent.current, fontFamily = PeachType.Mono, fontSize = 9.sp, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleShow).padding(6.dp)) Text("⟳", color = LocalPeachAccent.current, fontSize = 16.sp, modifier = Modifier.clip(RoundedCornerShape(6.dp)).clickable(onClick = onToggleGenerator).padding(6.dp)) }, ) } @Composable private fun GeneratorDrawer( open: Boolean, length: Int, upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean, password: String, copied: Boolean, onLength: (Int) -> Unit, onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit, onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit, onGenerate: () -> Unit, onCopy: () -> Unit, ) { val height by animateDpAsState( targetValue = if (open) 234.dp else 0.dp, animationSpec = tween(320, easing = PeachMotion.Spring), label = "gen-drawer", ) Box( Modifier.fillMaxWidth().height(height).clipToBounds() .clip(RoundedCornerShape(12.dp)).background(peachColors.S2), ) { GeneratorBody( length = length, upper = upper, lower = lower, numbers = numbers, symbols = symbols, password = password, copied = copied, onLength = onLength, onUpper = onUpper, onLower = onLower, onNumbers = onNumbers, onSymbols = onSymbols, onGenerate = onGenerate, onCopy = onCopy, ) } if (open) Spacer(Modifier.height(14.dp)) } /** * Shared generator body — used by the inline drawer here and the detail * ModalBottomSheet (D4). Stateless; the generate row carries the [Copy] drain. */ @Composable internal fun GeneratorBody( length: Int, upper: Boolean, lower: Boolean, numbers: Boolean, symbols: Boolean, password: String, copied: Boolean, onLength: (Int) -> Unit, onUpper: (Boolean) -> Unit, onLower: (Boolean) -> Unit, onNumbers: (Boolean) -> Unit, onSymbols: (Boolean) -> Unit, onGenerate: () -> Unit, onCopy: () -> Unit, ) { val accent = LocalPeachAccent.current Column(Modifier.fillMaxWidth().padding(16.dp)) { Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { Text("GENERATOR", color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 8.sp) Text("length $length", color = accent, fontFamily = PeachType.Mono, fontSize = 9.sp) } Slider( value = length.toFloat(), onValueChange = { onLength(it.toInt()) }, valueRange = 8f..128f, colors = SliderDefaults.colors( thumbColor = accent, activeTrackColor = accent, inactiveTrackColor = peachColors.T3, ), ) Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { TogglePill("A-Z", upper, onUpper) TogglePill("a-z", lower, onLower) TogglePill("0-9", numbers, onNumbers) TogglePill("!@#", symbols, onSymbols) } Spacer(Modifier.height(12.dp)) Row(verticalAlignment = Alignment.CenterVertically) { Box( Modifier.weight(1f).clip(RoundedCornerShape(10.dp)) .border(1.dp, accent.copy(alpha = 0.5f), RoundedCornerShape(10.dp)) .background(accent.copy(alpha = 0.10f)) .clickable(onClick = onGenerate).padding(vertical = 12.dp), contentAlignment = Alignment.Center, ) { Text("generate", color = accent, fontFamily = PeachType.Mono, fontSize = 12.sp) } Spacer(Modifier.width(8.dp)) CopyButton(copied = copied, onCopy = { if (password.isNotEmpty()) onCopy() }) } } } @Composable private fun TogglePill(label: String, on: Boolean, onToggle: (Boolean) -> Unit) { val accent = LocalPeachAccent.current Box( Modifier.clip(RoundedCornerShape(PeachRadius.TypeTab)) .border(1.dp, if (on) accent.copy(alpha = 0.5f) else peachColors.T3, RoundedCornerShape(PeachRadius.TypeTab)) .background(if (on) accent.copy(alpha = 0.10f) else Color.Transparent) .clickable { onToggle(!on) } .padding(horizontal = 10.dp, vertical = 6.dp), ) { Text(label, color = if (on) accent else peachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp) } } // PasswordHistorySection / HistoryRow / HistoryAction / timeSince were extracted // to ui/vault/PasswordHistorySection.kt (D7) so EntryDetailScreen can reuse them.