package com.durian.tssparty

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.durian.tssparty.domain.model.AppReadyState
import com.durian.tssparty.domain.model.ShareBackup
import com.durian.tssparty.domain.model.TokenType
import com.durian.tssparty.presentation.components.BottomNavItem
import com.durian.tssparty.presentation.components.TssBottomNavigation
import com.durian.tssparty.presentation.screens.*
import com.durian.tssparty.presentation.viewmodel.MainViewModel
import com.durian.tssparty.presentation.viewmodel.ConnectionTestResult as ViewModelConnectionTestResult
import com.durian.tssparty.ui.theme.TssPartyTheme
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TssPartyTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    TssPartyApp(
                        onCopyToClipboard = { text ->
                            val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                            val clip = ClipData.newPlainText("邀请码", text)
                            clipboard.setPrimaryClip(clip)
                            Toast.makeText(this, "邀请码已复制", Toast.LENGTH_SHORT).show()
                        }
                    )
                }
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TssPartyApp(
    viewModel: MainViewModel = hiltViewModel(),
    onCopyToClipboard: (String) -> Unit = {}
) {
    val navController = rememberNavController()
    val appState by viewModel.appState.collectAsState()
    val uiState by viewModel.uiState.collectAsState()
    val shares by viewModel.shares.collectAsState()
    val sessionStatus by viewModel.sessionStatus.collectAsState()
    val settings by viewModel.settings.collectAsState()
    val createdInviteCode by viewModel.createdInviteCode.collectAsState()
    val balances by viewModel.balances.collectAsState()
    val walletBalances by viewModel.walletBalances.collectAsState()
    val currentSessionId by viewModel.currentSessionId.collectAsState()
    val sessionParticipants by viewModel.sessionParticipants.collectAsState()
    val currentRound by viewModel.currentRound.collectAsState()
    val totalRounds by viewModel.totalRounds.collectAsState()
    val publicKey by viewModel.publicKey.collectAsState()
    val hasEnteredSession by viewModel.hasEnteredSession.collectAsState()

    // Transfer state
    val preparedTx by viewModel.preparedTx.collectAsState()
    val signSessionId by viewModel.signSessionId.collectAsState()
    val signInviteCode by viewModel.signInviteCode.collectAsState()
    val signParticipants by viewModel.signParticipants.collectAsState()
    val signCurrentRound by viewModel.signCurrentRound.collectAsState()
    val signature by viewModel.signature.collectAsState()
    val txHash by viewModel.txHash.collectAsState()

    // Join keygen state
    val joinSessionInfo by viewModel.joinSessionInfo.collectAsState()
    val joinKeygenParticipants by viewModel.joinKeygenParticipants.collectAsState()
    val joinKeygenRound by viewModel.joinKeygenRound.collectAsState()
    val joinKeygenPublicKey by viewModel.joinKeygenPublicKey.collectAsState()

    // CoSign state
    val coSignSessionInfo by viewModel.coSignSessionInfo.collectAsState()
    val coSignParticipants by viewModel.coSignParticipants.collectAsState()
    val coSignRound by viewModel.coSignRound.collectAsState()
    val coSignSignature by viewModel.coSignSignature.collectAsState()

    // Settings test connection results
    val messageRouterTestResult by viewModel.messageRouterTestResult.collectAsState()
    val accountServiceTestResult by viewModel.accountServiceTestResult.collectAsState()
    val kavaApiTestResult by viewModel.kavaApiTestResult.collectAsState()

    // Export/Import state
    val exportResult by viewModel.exportResult.collectAsState()
    val importResult by viewModel.importResult.collectAsState()

    // Transaction history state
    val transactionRecords by viewModel.transactionRecords.collectAsState()
    val isSyncingHistory by viewModel.isSyncingHistory.collectAsState()
    val syncResultMessage by viewModel.syncResultMessage.collectAsState()

    // Current transfer wallet
    var transferWalletId by remember { mutableStateOf<Long?>(null) }

    // Export/Import file handling
    val context = LocalContext.current
    // Use rememberSaveable to persist across configuration changes (e.g., file picker activity)
    var pendingExportJson by rememberSaveable { mutableStateOf<String?>(null) }
    var pendingExportAddress by rememberSaveable { mutableStateOf<String?>(null) }

    // File picker for saving backup
    val createDocumentLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.CreateDocument(ShareBackup.MIME_TYPE)
    ) { uri: Uri? ->
        android.util.Log.d("MainActivity", "[EXPORT-FILE] ========== createDocumentLauncher callback ==========")
        android.util.Log.d("MainActivity", "[EXPORT-FILE] uri: $uri")
        android.util.Log.d("MainActivity", "[EXPORT-FILE] pendingExportJson isNull: ${pendingExportJson == null}")
        android.util.Log.d("MainActivity", "[EXPORT-FILE] pendingExportJson length: ${pendingExportJson?.length ?: 0}")
        uri?.let { targetUri ->
            pendingExportJson?.let { json ->
                try {
                    android.util.Log.d("MainActivity", "[EXPORT-FILE] Opening output stream to: $targetUri")
                    context.contentResolver.openOutputStream(targetUri)?.use { outputStream ->
                        android.util.Log.d("MainActivity", "[EXPORT-FILE] Writing ${json.length} bytes...")
                        outputStream.write(json.toByteArray(Charsets.UTF_8))
                        android.util.Log.d("MainActivity", "[EXPORT-FILE] Write completed")
                    }
                    android.util.Log.d("MainActivity", "[EXPORT-FILE] File saved successfully!")
                    Toast.makeText(context, "备份文件已保存", Toast.LENGTH_SHORT).show()
                } catch (e: Exception) {
                    android.util.Log.e("MainActivity", "[EXPORT-FILE] Failed to save file: ${e.message}", e)
                    Toast.makeText(context, "保存失败: ${e.message}", Toast.LENGTH_LONG).show()
                }
                android.util.Log.d("MainActivity", "[EXPORT-FILE] Clearing pendingExportJson and pendingExportAddress")
                pendingExportJson = null
                pendingExportAddress = null
            } ?: run {
                android.util.Log.w("MainActivity", "[EXPORT-FILE] pendingExportJson is null, nothing to write!")
            }
        } ?: run {
            android.util.Log.w("MainActivity", "[EXPORT-FILE] User cancelled file picker (uri is null)")
        }
        android.util.Log.d("MainActivity", "[EXPORT-FILE] ========== callback finished ==========")
    }

    // File picker for importing backup
    val openDocumentLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.OpenDocument()
    ) { uri: Uri? ->
        android.util.Log.d("MainActivity", "[IMPORT-FILE] ========== openDocumentLauncher callback ==========")
        android.util.Log.d("MainActivity", "[IMPORT-FILE] uri: $uri")
        uri?.let { sourceUri ->
            try {
                android.util.Log.d("MainActivity", "[IMPORT-FILE] Opening input stream from: $sourceUri")
                context.contentResolver.openInputStream(sourceUri)?.use { inputStream ->
                    val json = inputStream.bufferedReader().readText()
                    android.util.Log.d("MainActivity", "[IMPORT-FILE] Read ${json.length} bytes")
                    android.util.Log.d("MainActivity", "[IMPORT-FILE] JSON preview: ${json.take(100)}...")
                    android.util.Log.d("MainActivity", "[IMPORT-FILE] Calling viewModel.importShareBackup...")
                    viewModel.importShareBackup(json)
                    android.util.Log.d("MainActivity", "[IMPORT-FILE] viewModel.importShareBackup called")
                }
            } catch (e: Exception) {
                android.util.Log.e("MainActivity", "[IMPORT-FILE] Failed to read file: ${e.message}", e)
                Toast.makeText(context, "读取文件失败: ${e.message}", Toast.LENGTH_LONG).show()
            }
        } ?: run {
            android.util.Log.w("MainActivity", "[IMPORT-FILE] User cancelled file picker (uri is null)")
        }
        android.util.Log.d("MainActivity", "[IMPORT-FILE] ========== callback finished ==========")
    }

    // Handle export result - trigger file save dialog
    LaunchedEffect(pendingExportJson) {
        android.util.Log.d("MainActivity", "[EXPORT-EFFECT] LaunchedEffect(pendingExportJson) triggered")
        android.util.Log.d("MainActivity", "[EXPORT-EFFECT] pendingExportJson isNull: ${pendingExportJson == null}")
        android.util.Log.d("MainActivity", "[EXPORT-EFFECT] pendingExportJson length: ${pendingExportJson?.length ?: 0}")
        pendingExportJson?.let { json ->
            val timestamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date())
            val addressSuffix = pendingExportAddress?.take(8) ?: "wallet"
            val fileName = "tss_backup_${addressSuffix}_$timestamp.${ShareBackup.FILE_EXTENSION}"
            android.util.Log.d("MainActivity", "[EXPORT-EFFECT] Launching file picker with filename: $fileName")
            createDocumentLauncher.launch(fileName)
            android.util.Log.d("MainActivity", "[EXPORT-EFFECT] File picker launched")
        }
    }

    // Handle import result - show toast
    LaunchedEffect(importResult) {
        android.util.Log.d("MainActivity", "[IMPORT-EFFECT] LaunchedEffect(importResult) triggered")
        android.util.Log.d("MainActivity", "[IMPORT-EFFECT] importResult: $importResult")
        importResult?.let { result ->
            android.util.Log.d("MainActivity", "[IMPORT-EFFECT] isSuccess: ${result.isSuccess}, error: ${result.error}, message: ${result.message}")
            when {
                result.isSuccess -> {
                    android.util.Log.d("MainActivity", "[IMPORT-EFFECT] Showing success toast")
                    Toast.makeText(context, result.message ?: "导入成功", Toast.LENGTH_SHORT).show()
                    viewModel.clearExportImportResult()
                }
                result.error != null -> {
                    android.util.Log.d("MainActivity", "[IMPORT-EFFECT] Showing error toast: ${result.error}")
                    Toast.makeText(context, result.error, Toast.LENGTH_LONG).show()
                    viewModel.clearExportImportResult()
                }
            }
        }
    }

    // Track if startup is complete
    // Use rememberSaveable to persist across configuration changes (e.g., file picker activity)
    var startupComplete by rememberSaveable { mutableStateOf(false) }
    android.util.Log.d("MainActivity", "[STATE] TssPartyApp composing, startupComplete: $startupComplete")

    // Handle success messages
    LaunchedEffect(uiState.successMessage) {
        if (uiState.successMessage != null) {
            // Navigate back to wallets on success
            navController.navigate(BottomNavItem.Wallets.route) {
                popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
            }
            // Reset all session states so next time user enters a fresh state
            viewModel.resetSessionState()
            viewModel.resetJoinKeygenState()
            viewModel.resetCoSignState()
            viewModel.resetTransferState()
            viewModel.clearSuccess()
            viewModel.clearCreatedInviteCode()
        }
    }

    // Show startup check screen if not complete
    if (!startupComplete) {
        StartupCheckScreen(
            appState = appState,
            onEnterApp = { startupComplete = true },
            onRetry = { viewModel.checkAllServices() }
        )
        return
    }

    // Main app with bottom navigation
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = navBackStackEntry?.destination?.route ?: BottomNavItem.Wallets.route

    Scaffold(
        bottomBar = {
            TssBottomNavigation(
                currentRoute = currentRoute,
                onNavigate = { item ->
                    navController.navigate(item.route) {
                        // Pop up to the start destination to avoid building up a large stack
                        popUpTo(BottomNavItem.Wallets.route) {
                            saveState = true
                        }
                        // Avoid multiple copies of the same destination
                        launchSingleTop = true
                        // Restore state when reselecting a previously selected item
                        restoreState = true
                    }
                }
            )
        }
    ) { paddingValues ->
        NavHost(
            navController = navController,
            startDestination = BottomNavItem.Wallets.route,
            modifier = Modifier.padding(paddingValues)
        ) {
            // Tab 1: My Wallets (我的钱包)
            composable(BottomNavItem.Wallets.route) {
                // Fetch balances when entering wallets screen
                LaunchedEffect(shares) {
                    viewModel.fetchAllBalances()
                }

                WalletsScreen(
                    shares = shares,
                    isConnected = uiState.isConnected,
                    balances = balances,
                    walletBalances = walletBalances,
                    networkType = settings.networkType,
                    onDeleteShare = { viewModel.deleteShare(it) },
                    onRefreshBalance = { address -> viewModel.fetchBalance(address) },
                    onTransfer = { shareId ->
                        transferWalletId = shareId
                        navController.navigate("transfer/$shareId")
                    },
                    onHistory = { shareId, address ->
                        navController.navigate("history/$shareId/$address")
                    },
                    onExportBackup = { shareId, _ ->
                        android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] ========== onExportBackup called ==========")
                        android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] shareId: $shareId")
                        // Get address for filename
                        val share = shares.find { it.id == shareId }
                        android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] share found: ${share != null}, address: ${share?.address}")
                        pendingExportAddress = share?.address
                        android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] pendingExportAddress set to: $pendingExportAddress")
                        // Export and save to file
                        android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] Calling viewModel.exportShareBackup...")
                        viewModel.exportShareBackup(shareId) { json ->
                            android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] exportShareBackup callback received")
                            android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] json length: ${json.length}")
                            android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] Setting pendingExportJson...")
                            pendingExportJson = json
                            android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] pendingExportJson set, length: ${pendingExportJson?.length}")
                        }
                        android.util.Log.d("MainActivity", "[EXPORT-TRIGGER] viewModel.exportShareBackup called (async)")
                    },
                    onImportBackup = {
                        android.util.Log.d("MainActivity", "[IMPORT-TRIGGER] ========== onImportBackup called ==========")
                        android.util.Log.d("MainActivity", "[IMPORT-TRIGGER] Launching file picker...")
                        // Open file picker to select backup file
                        openDocumentLauncher.launch(arrayOf("*/*"))
                        android.util.Log.d("MainActivity", "[IMPORT-TRIGGER] File picker launched")
                    },
                    onCreateWallet = {
                        navController.navigate(BottomNavItem.Create.route)
                    }
                )
            }

            // Transfer Screen
            composable("transfer/{shareId}") { backStackEntry ->
                val shareId = backStackEntry.arguments?.getString("shareId")?.toLongOrNull()
                val wallet = shareId?.let { viewModel.getWalletById(it) }

                if (wallet != null) {
                    TransferScreen(
                        wallet = wallet,
                        balance = balances[wallet.address],
                        walletBalance = walletBalances[wallet.address],
                        sessionStatus = sessionStatus,
                        participants = signParticipants,
                        currentRound = signCurrentRound,
                        totalRounds = if (totalRounds > 0) totalRounds else 9, // Default to sign rounds
                        preparedTx = preparedTx,
                        signSessionId = signSessionId,
                        inviteCode = signInviteCode,
                        signature = signature,
                        txHash = txHash,
                        isLoading = uiState.isLoading,
                        error = uiState.error,
                        networkType = settings.networkType,
                        rpcUrl = settings.kavaRpcUrl,
                        onPrepareTransaction = { toAddress, amount, tokenType ->
                            viewModel.prepareTransfer(shareId, toAddress, amount, tokenType)
                        },
                        onConfirmTransaction = { includeServerBackup ->
                            // 【新增】根据用户选择调用相应的签名方法
                            // includeServerBackup = true: 使用新方法，包含服务器备份参与方
                            // includeServerBackup = false: 使用现有方法，排除服务器方（默认行为）
                            if (includeServerBackup) {
                                viewModel.initiateSignSessionWithOptions(
                                    shareId = shareId,
                                    password = "",
                                    includeServerBackup = true
                                )
                            } else {
                                viewModel.initiateSignSession(shareId, "")
                            }
                        },
                        onCopyInviteCode = {
                            signInviteCode?.let { onCopyToClipboard(it) }
                        },
                        onBroadcastTransaction = {
                            viewModel.broadcastTransaction()
                        },
                        onCancel = {
                            viewModel.resetTransferState()
                            viewModel.clearError()
                            navController.popBackStack()
                        },
                        onBackToWallets = {
                            viewModel.resetTransferState()
                            navController.navigate(BottomNavItem.Wallets.route) {
                                popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                            }
                        }
                    )
                }
            }

            // Transaction History Screen
            composable("history/{shareId}/{address}") { backStackEntry ->
                val shareId = backStackEntry.arguments?.getString("shareId")?.toLongOrNull() ?: 0L
                val address = backStackEntry.arguments?.getString("address") ?: ""

                // Load records and sync when entering screen
                LaunchedEffect(shareId, address) {
                    viewModel.loadTransactionRecords(shareId)
                    // Auto-sync from blockchain on first entry
                    if (address.isNotEmpty()) {
                        viewModel.syncTransactionHistory(shareId, address)
                    }
                }

                TransactionHistoryScreen(
                    shareId = shareId,
                    walletAddress = address,
                    transactions = transactionRecords,
                    networkType = settings.networkType,
                    isSyncing = isSyncingHistory,
                    syncResultMessage = syncResultMessage,
                    onBack = { navController.popBackStack() },
                    onRefresh = { viewModel.syncTransactionHistory(shareId, address) },
                    onClearSyncMessage = { viewModel.clearSyncResultMessage() }
                )
            }

            // Tab 2: Create Wallet (创建钱包)
            composable(BottomNavItem.Create.route) {
                CreateWalletScreen(
                    isLoading = uiState.isLoading,
                    error = uiState.error,
                    inviteCode = createdInviteCode,
                    sessionId = currentSessionId,
                    sessionStatus = sessionStatus,
                    hasEnteredSession = hasEnteredSession,
                    participants = sessionParticipants,
                    currentRound = currentRound,
                    totalRounds = if (totalRounds > 0) totalRounds else 4, // Default to keygen rounds
                    publicKey = publicKey,
                    countdownSeconds = uiState.countdownSeconds,
                    onCreateSession = { name, t, n, participantName ->
                        viewModel.createKeygenSession(name, t, n, participantName)
                    },
                    onCopyInviteCode = {
                        createdInviteCode?.let { onCopyToClipboard(it) }
                    },
                    onEnterSession = {
                        viewModel.enterSession()
                    },
                    onCancel = {
                        viewModel.cancelSession()
                        viewModel.clearError()
                        viewModel.resetSessionState()
                        navController.navigate(BottomNavItem.Wallets.route) {
                            popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                        }
                    },
                    onBackToHome = {
                        viewModel.resetSessionState()
                        navController.navigate(BottomNavItem.Wallets.route) {
                            popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                        }
                    }
                )
            }

            // Tab 3: Join Keygen (加入创建)
            composable(BottomNavItem.JoinKeygen.route) {
                // Convert JoinKeygenSessionInfo to JoinSessionInfo for the screen
                val screenSessionInfo = joinSessionInfo?.let {
                    JoinSessionInfo(
                        sessionId = it.sessionId,
                        walletName = it.walletName,
                        thresholdT = it.thresholdT,
                        thresholdN = it.thresholdN,
                        initiator = it.initiator,
                        currentParticipants = it.currentParticipants,
                        totalParticipants = it.totalParticipants
                    )
                }

                JoinKeygenScreen(
                    sessionStatus = sessionStatus,
                    isLoading = uiState.isLoading,
                    error = uiState.error,
                    sessionInfo = screenSessionInfo,
                    participants = joinKeygenParticipants,
                    currentRound = joinKeygenRound,
                    totalRounds = if (totalRounds > 0) totalRounds else 4, // Default to keygen rounds
                    publicKey = joinKeygenPublicKey,
                    countdownSeconds = uiState.countdownSeconds,
                    onValidateInviteCode = { inviteCode ->
                        viewModel.validateInviteCode(inviteCode)
                    },
                    onJoinKeygen = { inviteCode, password ->
                        viewModel.joinKeygen(inviteCode, password)
                    },
                    onCancel = {
                        // Cancel from input screen - navigate away
                        viewModel.cancelSession()
                        viewModel.clearError()
                        viewModel.resetJoinKeygenState()
                        navController.navigate(BottomNavItem.Wallets.route) {
                            popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                        }
                    },
                    onResetState = {
                        // Reset from confirm/joining/progress screens - stay on page
                        viewModel.cancelSession()
                        viewModel.clearError()
                        viewModel.resetJoinKeygenState()
                    },
                    onBackToHome = {
                        viewModel.resetJoinKeygenState()
                        navController.navigate(BottomNavItem.Wallets.route) {
                            popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                        }
                    }
                )
            }

            // Tab 4: Co-Sign (参与签名)
            composable(BottomNavItem.CoSign.route) {
                // Convert CoSignSessionInfo to SignSessionInfo for the screen
                val screenSignSessionInfo = coSignSessionInfo?.let {
                    SignSessionInfo(
                        sessionId = it.sessionId,
                        keygenSessionId = it.keygenSessionId,
                        walletName = it.walletName,
                        messageHash = it.messageHash,
                        thresholdT = it.thresholdT,
                        thresholdN = it.thresholdN,
                        currentParticipants = it.currentParticipants
                    )
                }

                CoSignJoinScreen(
                    shares = shares,
                    sessionStatus = sessionStatus,
                    isLoading = uiState.isLoading,
                    error = uiState.error,
                    signSessionInfo = screenSignSessionInfo,
                    participants = coSignParticipants,
                    currentRound = coSignRound,
                    totalRounds = if (totalRounds > 0) totalRounds else 9, // Default to sign rounds
                    signature = coSignSignature,
                    countdownSeconds = uiState.countdownSeconds,
                    onValidateInviteCode = { inviteCode ->
                        viewModel.validateSignInviteCode(inviteCode)
                    },
                    onJoinSign = { inviteCode, shareId, password ->
                        viewModel.joinSign(inviteCode, shareId, password)
                    },
                    onCancel = {
                        // Cancel from input screen - navigate away
                        viewModel.cancelSession()
                        viewModel.clearError()
                        viewModel.resetCoSignState()
                        navController.navigate(BottomNavItem.Wallets.route) {
                            popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                        }
                    },
                    onResetState = {
                        // Reset from select_share/joining/signing screens - stay on page
                        viewModel.cancelSession()
                        viewModel.clearError()
                        viewModel.resetCoSignState()
                    },
                    onBackToHome = {
                        viewModel.resetCoSignState()
                        navController.navigate(BottomNavItem.Wallets.route) {
                            popUpTo(BottomNavItem.Wallets.route) { inclusive = true }
                        }
                    }
                )
            }

            // Tab 5: Settings (设置)
            composable(BottomNavItem.Settings.route) {
                // Convert ViewModel ConnectionTestResult to Screen ConnectionTestResult
                val screenMessageRouterStatus: ConnectionTestResult? = messageRouterTestResult?.let {
                    ConnectionTestResult(
                        success = it.success,
                        message = it.message,
                        latency = it.latency
                    )
                }
                val screenAccountServiceStatus: ConnectionTestResult? = accountServiceTestResult?.let {
                    ConnectionTestResult(
                        success = it.success,
                        message = it.message,
                        latency = it.latency
                    )
                }
                val screenKavaApiStatus: ConnectionTestResult? = kavaApiTestResult?.let {
                    ConnectionTestResult(
                        success = it.success,
                        message = it.message,
                        latency = it.latency
                    )
                }

                SettingsScreen(
                    settings = settings,
                    isConnected = uiState.isConnected,
                    messageRouterStatus = screenMessageRouterStatus,
                    accountServiceStatus = screenAccountServiceStatus,
                    kavaApiStatus = screenKavaApiStatus,
                    onSaveSettings = { newSettings ->
                        viewModel.updateSettings(newSettings)
                    },
                    onTestMessageRouter = { url ->
                        viewModel.testMessageRouter(url)
                    },
                    onTestAccountService = { url ->
                        viewModel.testAccountService(url)
                    },
                    onTestKavaApi = { url ->
                        viewModel.testKavaApi(url)
                    }
                )
            }
        }
    }
}
