package com.peachpasswords.ui.pairing import androidx.compose.foundation.background 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.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.systemBarsPadding import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.peachpasswords.sync.PairingPhase import com.peachpasswords.ui.components.BirdAnimation import com.peachpasswords.ui.components.PairingAnimState import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.PeachColors import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground @Composable fun ChirpPairScreen( onBack: () -> Unit, vm: ChirpPairViewModel = hiltViewModel(), ) { val progress by vm.progress.collectAsStateWithLifecycle() val sas by vm.sasCode.collectAsStateWithLifecycle() val accent = LocalPeachAccent.current val phase = progress.phase ShellBackground { Column(modifier = Modifier.fillMaxSize().systemBarsPadding().padding(horizontal = 24.dp)) { // Header Row( modifier = Modifier.fillMaxWidth().height(56.dp), verticalAlignment = Alignment.CenterVertically, ) { Text( text = "‹", color = PeachColors.T1, fontSize = 26.sp, modifier = Modifier.clip(CircleShape).clickable(onClick = onBack).padding(horizontal = 8.dp), ) Spacer(Modifier.size(8.dp)) Text("Sync", color = PeachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 19.sp) } StatusChip(phase, accent) Box( modifier = Modifier.fillMaxWidth().weight(1f), contentAlignment = Alignment.Center, ) { BirdAnimation( state = animState(phase), modifier = Modifier.fillMaxWidth().height(220.dp), ) } Copy(phase, progress.peerDeviceName, progress.errorMessage) Spacer(Modifier.height(20.dp)) PrimaryAction(phase, vm) Spacer(Modifier.height(28.dp)) } if (sas != null) { SasOverlay( code = sas!!, accent = accent, onConfirm = { if (vm.isDebug && progress.phase == PairingPhase.HANDSHAKE) vm.demoComplete() else vm.confirmSas(true) }, onCancel = { vm.confirmSas(false); vm.reset() }, ) } } } private fun animState(phase: PairingPhase): PairingAnimState = when (phase) { PairingPhase.IDLE -> PairingAnimState.WAITING PairingPhase.LISTENING, PairingPhase.CHIRPING, PairingPhase.HANDSHAKE -> PairingAnimState.LISTENING PairingPhase.PAIRED -> PairingAnimState.CONNECTED else -> PairingAnimState.WAITING } @Composable private fun StatusChip(phase: PairingPhase, accent: Color) { val (label, dot) = when (phase) { PairingPhase.IDLE -> "ready" to PeachColors.T2 PairingPhase.LISTENING, PairingPhase.CHIRPING -> "listening" to accent PairingPhase.HANDSHAKE -> "verifying" to accent PairingPhase.PAIRED -> "paired" to Color(0xFF7FB069) PairingPhase.MIC_DENIED -> "mic blocked" to PeachColors.Breach else -> "interrupted" to PeachColors.Breach } Row(verticalAlignment = Alignment.CenterVertically) { Box(Modifier.size(6.dp).clip(CircleShape).background(dot)) Spacer(Modifier.size(6.dp)) Text(label, color = PeachColors.T1, fontFamily = PeachType.Mono, fontSize = 9.sp) } } @Composable private fun Copy(phase: PairingPhase, peer: String?, error: String?) { val (headline, sub) = when (phase) { PairingPhase.IDLE -> "Pair a device" to "Peach pairs over a short, inaudible chirp — no codes typed, no servers." PairingPhase.LISTENING, PairingPhase.CHIRPING -> "Listening…" to "Hold this phone near the other device and start pairing there too." PairingPhase.HANDSHAKE -> "Almost there" to "Check that the code below matches on ${peer ?: "the other device"}." PairingPhase.PAIRED -> "Paired" to "${peer ?: "Your devices"} will now keep your vault in step over the local network." PairingPhase.TIMEOUT -> "No chirp heard" to "Nothing answered. Move the devices closer and try again." PairingPhase.MIC_DENIED -> "Microphone needed" to "Pairing listens for an inaudible chirp. Grant mic access to continue." PairingPhase.VERSION_MISMATCH -> "Version mismatch" to "Update Peach on both devices, then try pairing again." else -> "Pairing interrupted" to (error ?: "Something went wrong. Try again.") } Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { Text(headline, color = PeachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 26.sp, textAlign = TextAlign.Center) Spacer(Modifier.height(8.dp)) Text(sub, color = PeachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp, textAlign = TextAlign.Center) } } @Composable private fun PrimaryAction(phase: PairingPhase, vm: ChirpPairViewModel) { val accent = LocalPeachAccent.current when (phase) { PairingPhase.IDLE -> PillButton("Start pairing", accent, filled = true) { vm.startPairing() } PairingPhase.LISTENING, PairingPhase.CHIRPING, PairingPhase.HANDSHAKE -> PillButton("Cancel", PeachColors.T2, filled = false) { vm.reset() } PairingPhase.PAIRED -> PillButton("Done", accent, filled = true) { vm.reset() } else -> PillButton("Try again", accent, filled = true) { vm.startPairing() } } if (vm.isDebug && phase == PairingPhase.IDLE) { Spacer(Modifier.height(10.dp)) Text( "demo flow", color = PeachColors.T3, fontFamily = PeachType.Mono, fontSize = 9.sp, modifier = Modifier.fillMaxWidth().clickable { vm.runDemo() }.padding(8.dp), textAlign = TextAlign.Center, ) } } @Composable private fun PillButton(label: String, color: Color, filled: Boolean, onClick: () -> Unit) { val accent = LocalPeachAccent.current Box( modifier = Modifier .fillMaxWidth() .clip(RoundedCornerShape(14.dp)) .background(if (filled) accent.copy(alpha = 0.14f) else Color.Transparent) .clickable(onClick = onClick) .padding(vertical = 15.dp), contentAlignment = Alignment.Center, ) { Text(label, color = if (filled) accent else color, fontFamily = PeachType.Mono, fontSize = 13.sp) } } @Composable private fun SasOverlay(code: String, accent: Color, onConfirm: () -> Unit, onCancel: () -> Unit) { Box( modifier = Modifier.fillMaxSize().background(Color(0xCC0C0A06)), contentAlignment = Alignment.Center, ) { Column( modifier = Modifier.fillMaxWidth().padding(horizontal = 36.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { Text("Verify the code", color = PeachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 22.sp) Spacer(Modifier.height(6.dp)) Text("It must match on both devices.", color = PeachColors.T2, fontFamily = PeachType.Body, fontSize = 13.sp, textAlign = TextAlign.Center) Spacer(Modifier.height(28.dp)) Text( text = code, color = accent, fontFamily = PeachType.Mono, fontSize = 38.sp, letterSpacing = 6.sp, ) Spacer(Modifier.height(34.dp)) PillButton("They match — pair", accent, filled = true, onClick = onConfirm) Spacer(Modifier.height(10.dp)) PillButton("Cancel", PeachColors.T2, filled = false, onClick = onCancel) } } }