package dev.fishpi.mobile
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.background
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.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material.icons.filled.Mail
import androidx.compose.material.icons.filled.QrCodeScanner
import androidx.compose.material.icons.filled.Security
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
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.res.painterResource
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.foundation.Image
import dev.fishpi.mobile.data.FishPiApiClient
import dev.fishpi.mobile.data.SavedAccount
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable
internal fun LoginScreen(
initialError: String?,
savedAccounts: List<SavedAccount>,
onSwitchAccount: (SavedAccount) -> Unit,
onAuthenticated: (AppSession) -> Unit,
) {
val api = remember { FishPiApiClient.shared }
val scope = rememberCoroutineScope()
var nameOrEmail by rememberSaveable { mutableStateOf("") }
var password by rememberSaveable { mutableStateOf("") }
var mfaCode by rememberSaveable { mutableStateOf("") }
var error by remember { mutableStateOf(initialError) }
var isLoading by remember { mutableStateOf(false) }
// Focus management for better form flow
val passwordFocus = remember { FocusRequester() }
val mfaFocus = remember { FocusRequester() }
val context = LocalContext.current
val focusManager = LocalFocusManager.current
var showQrScanner by remember { mutableStateOf(false) }
// Auto-dismiss error after user starts typing
LaunchedEffect(nameOrEmail, password) {
if (error != null) error = null
}
Box(
modifier = Modifier
.fillMaxSize()
.background(
Brush.verticalGradient(
listOf(
MaterialTheme.colorScheme.surface,
MaterialTheme.colorScheme.primary.copy(alpha = 0.10f),
MaterialTheme.colorScheme.surfaceContainerLow,
),
),
)
.imePadding(),
contentAlignment = Alignment.Center,
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 24.dp)
.verticalScroll(rememberScrollState()),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(0.dp),
) {
// ── Brand Header ─────────────────────────────────
Surface(
modifier = Modifier.size(72.dp),
shape = RoundedCornerShape(18.dp),
color = MaterialTheme.colorScheme.surface,
tonalElevation = 0.dp,
shadowElevation = 0.dp,
) {
Box(contentAlignment = Alignment.Center) {
Image(
painter = painterResource(R.drawable.ic_fishpi_logo),
contentDescription = "摸鱼派",
modifier = Modifier.size(48.dp),
)
}
}
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "连接鱼派",
style = MaterialTheme.typography.headlineSmall,
color = MaterialTheme.colorScheme.onSurface,
fontWeight = FontWeight.Bold,
)
Text(
text = "密码登录或扫码登录",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Spacer(modifier = Modifier.height(28.dp))
// ── Login Card ───────────────────────────────────
Surface(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(18.dp),
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.72f),
tonalElevation = 0.dp,
shadowElevation = 0.dp,
) {
Column(
modifier = Modifier.padding(24.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
// Username / Email
OutlinedTextField(
value = nameOrEmail,
onValueChange = { nameOrEmail = it },
modifier = Modifier.fillMaxWidth(),
label = { Text("用户名或邮箱") },
placeholder = { Text("请输入用户名或邮箱") },
leadingIcon = {
Icon(
imageVector = Icons.Default.Mail,
contentDescription = null,
)
},
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Email,
imeAction = ImeAction.Next,
),
keyboardActions = KeyboardActions(
onNext = { passwordFocus.requestFocus() },
),
shape = RoundedCornerShape(14.dp),
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = MaterialTheme.colorScheme.primary,
unfocusedBorderColor = MaterialTheme.colorScheme.outlineVariant,
focusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLowest,
unfocusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLowest,
),
)
// Password
OutlinedTextField(
value = password,
onValueChange = { password = it },
modifier = Modifier
.fillMaxWidth()
.focusRequester(passwordFocus),
label = { Text("密码") },
placeholder = { Text("请输入密码") },
leadingIcon = {
Icon(
imageVector = Icons.Default.Lock,
contentDescription = null,
)
},
singleLine = true,
visualTransformation = PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password,
imeAction = ImeAction.Next,
),
keyboardActions = KeyboardActions(
onNext = { mfaFocus.requestFocus() },
),
shape = RoundedCornerShape(14.dp),
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = MaterialTheme.colorScheme.primary,
unfocusedBorderColor = MaterialTheme.colorScheme.outlineVariant,
focusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLowest,
unfocusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLowest,
),
)
// MFA Code (optional)
OutlinedTextField(
value = mfaCode,
onValueChange = { mfaCode = it },
modifier = Modifier
.fillMaxWidth()
.focusRequester(mfaFocus),
label = { Text("两步验证码(可选)") },
placeholder = { Text("未开启可以留空") },
leadingIcon = {
Icon(
imageVector = Icons.Default.Security,
contentDescription = null,
)
},
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done,
),
keyboardActions = KeyboardActions(
onDone = { focusManager.clearFocus() },
),
shape = RoundedCornerShape(14.dp),
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = MaterialTheme.colorScheme.primary,
unfocusedBorderColor = MaterialTheme.colorScheme.outlineVariant,
focusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLowest,
unfocusedContainerColor = MaterialTheme.colorScheme.surfaceContainerLowest,
),
)
// ── Error Message ─────────────────────────
AnimatedVisibility(
visible = error != null,
enter = fadeIn(),
exit = fadeOut(),
) {
error?.let {
Surface(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(12.dp),
color = MaterialTheme.colorScheme.errorContainer,
) {
Text(
text = it,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp),
color = MaterialTheme.colorScheme.onErrorContainer,
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
)
}
}
}
// ── Login Button ──────────────────────────
Button(
onClick = {
val account = nameOrEmail.trim()
val rawPassword = password
if (account.isBlank() || rawPassword.isBlank()) {
error = "账号和密码不能为空"
return@Button
}
isLoading = true
error = null
focusManager.clearFocus()
scope.launch {
runCatching {
withContext(Dispatchers.IO) {
val token = api.login(
nameOrEmail = account,
password = rawPassword,
mfaCode = mfaCode.trim().ifBlank { null },
)
AppSession(token, api.getUser(token))
}
}.onSuccess {
onAuthenticated(it)
}.onFailure {
error = it.message ?: "验证失败"
}
isLoading = false
}
},
modifier = Modifier
.fillMaxWidth()
.height(52.dp),
enabled = !isLoading,
shape = RoundedCornerShape(14.dp),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primary,
contentColor = MaterialTheme.colorScheme.onPrimary,
disabledContainerColor = MaterialTheme.colorScheme.surfaceContainerHigh,
disabledContentColor = MaterialTheme.colorScheme.onSurfaceVariant,
),
) {
if (isLoading) {
CircularProgressIndicator(
modifier = Modifier.size(20.dp),
color = MaterialTheme.colorScheme.onPrimary,
strokeWidth = 2.dp,
)
Spacer(modifier = Modifier.width(8.dp))
}
Text(
text = if (isLoading) "登录中..." else "登录",
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.SemiBold,
)
}
// ── Divider ──────────────────────────────
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
) {
Spacer(modifier = Modifier.weight(1f).height(1.dp)
.background(MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f)))
Text(
" 或 ", color = MaterialTheme.colorScheme.onSurfaceVariant,
fontSize = 13.sp,
)
Spacer(modifier = Modifier.weight(1f).height(1.dp)
.background(MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f)))
}
// ── Scan QR Code Button ────────────────────
OutlinedButton(
onClick = { showQrScanner = true },
modifier = Modifier.fillMaxWidth().height(44.dp),
enabled = !isLoading,
shape = RoundedCornerShape(14.dp),
) {
Icon(
imageVector = Icons.Filled.QrCodeScanner,
contentDescription = null,
modifier = Modifier.size(18.dp),
)
Spacer(Modifier.width(6.dp))
Text("扫码登录", fontSize = 14.sp)
}
}
}
if (savedAccounts.isNotEmpty()) {
Spacer(modifier = Modifier.height(14.dp))
Surface(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(14.dp),
color = MaterialTheme.colorScheme.surface.copy(alpha = 0.58f),
tonalElevation = 0.dp,
) {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
Text(
text = "已保存账号",
style = MaterialTheme.typography.titleSmall,
color = MaterialTheme.colorScheme.onSurface,
fontWeight = FontWeight.SemiBold,
)
savedAccounts.forEach { account ->
OutlinedButton(
onClick = { onSwitchAccount(account) },
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(12.dp),
) {
Text(
text = account.displayName.ifBlank {
account.userName.ifBlank { "已保存账号" }
},
)
}
}
}
}
}
Spacer(modifier = Modifier.height(16.dp))
}
if (showQrScanner) {
QrScannerScreen(
onResult = { raw ->
showQrScanner = false
val token = parseScanLoginApiKey(raw.trim())
if (token.isNullOrBlank()) {
error = "二维码格式不支持"
return@QrScannerScreen
}
isLoading = true
error = null
scope.launch {
runCatching {
withContext(Dispatchers.IO) {
AppSession(token, api.getUser(token))
}
}.onSuccess { onAuthenticated(it) }
.onFailure { error = it.message ?: "扫码登录失败" }
isLoading = false
}
},
onClose = { showQrScanner = false },
)
}
}
}
private fun parseScanLoginApiKey(raw: String): String? {
val text = raw.trim()
val marker = "login:"
val idx = text.lowercase().indexOf(marker)
if (idx < 0) return null
val apiKey = text.substring(idx + marker.length).trim()
return apiKey.takeIf { it.isNotBlank() }
}