package com.rosetta.messenger.ui.settings

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.*
import com.rosetta.messenger.ui.icons.TelegramIcons
import compose.icons.TablerIcons
import compose.icons.tablericons.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun ProfileLogsScreen(
    isDarkTheme: Boolean,
    logs: List<String>,
    onBack: () -> Unit,
    onClearLogs: () -> Unit
) {
    val view = LocalView.current
    if (!view.isInEditMode) {
        DisposableEffect(isDarkTheme) {
            val window = (view.context as android.app.Activity).window
            val insetsController = androidx.core.view.WindowCompat.getInsetsController(window, view)
            val prev = insetsController.isAppearanceLightStatusBars
            insetsController.isAppearanceLightStatusBars = !isDarkTheme
            window.statusBarColor = android.graphics.Color.TRANSPARENT
            onDispose { insetsController.isAppearanceLightStatusBars = prev }
        }
    }

    val backgroundColor = if (isDarkTheme) Color(0xFF1A1A1A) else Color(0xFFFFFFFF)
    val surfaceColor = if (isDarkTheme) Color(0xFF2C2C2E) else Color(0xFFF2F2F7)
    val textColor = if (isDarkTheme) Color.White else Color.Black
    val secondaryTextColor = if (isDarkTheme) Color(0xFF8E8E93) else Color(0xFF666666)
    val successColor = if (isDarkTheme) Color(0xFF4CAF50) else Color(0xFF2E7D32)
    val errorColor = if (isDarkTheme) Color(0xFFEF5350) else Color(0xFFD32F2F)
    
    val listState = rememberLazyListState()
    
    // Auto-scroll to bottom when new logs added
    LaunchedEffect(logs.size) {
        if (logs.isNotEmpty()) {
            listState.animateScrollToItem(logs.size - 1)
        }
    }
    
    // Handle back gesture
    BackHandler { onBack() }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(backgroundColor)
    ) {
        // Top Bar
        Surface(
            modifier = Modifier.fillMaxWidth(),
            color = backgroundColor
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .statusBarsPadding()
                    .padding(horizontal = 4.dp, vertical = 8.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                IconButton(onClick = onBack) {
                    Icon(
                        imageVector = TablerIcons.ChevronLeft,
                        contentDescription = "Back",
                        tint = textColor
                    )
                }
                Text(
                    text = "Profile Logs",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = textColor,
                    modifier = Modifier
                        .weight(1f)
                        .padding(start = 8.dp)
                )
                IconButton(onClick = onClearLogs) {
                    Icon(
                        painter = TelegramIcons.Delete,
                        contentDescription = "Clear logs",
                        tint = secondaryTextColor
                    )
                }
            }
        }

        Divider(color = if (isDarkTheme) Color(0xFF3A3A3A) else Color(0xFFE8E8E8))

        // Logs content
        if (logs.isEmpty()) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "No logs yet.\nSave profile to see logs.",
                    fontSize = 14.sp,
                    color = secondaryTextColor,
                    textAlign = androidx.compose.ui.text.style.TextAlign.Center
                )
            }
        } else {
            LazyColumn(
                state = listState,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(horizontal = 8.dp, vertical = 8.dp)
            ) {
                items(logs) { log ->
                    LogItem(
                        log = log,
                        textColor = textColor,
                        successColor = successColor,
                        errorColor = errorColor,
                        isDarkTheme = isDarkTheme
                    )
                }
            }
        }
    }
}

@Composable
private fun LogItem(
    log: String,
    textColor: Color,
    successColor: Color,
    errorColor: Color,
    isDarkTheme: Boolean
) {
    val logColor = when {
        log.contains("✅ SUCCESS") -> successColor
        log.contains("❌ ERROR") -> errorColor
        log.contains("===") -> if (isDarkTheme) Color(0xFF9C27B0) else Color(0xFF6A1B9A)
        else -> textColor
    }
    
    val backgroundColor = when {
        log.contains("✅ SUCCESS") -> if (isDarkTheme) Color(0xFF1B5E20).copy(alpha = 0.2f) else Color(0xFFC8E6C9)
        log.contains("❌ ERROR") -> if (isDarkTheme) Color(0xFFB71C1C).copy(alpha = 0.2f) else Color(0xFFFFCDD2)
        else -> Color.Transparent
    }
    
    Surface(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 2.dp),
        color = backgroundColor,
        shape = androidx.compose.foundation.shape.RoundedCornerShape(4.dp)
    ) {
        Text(
            text = log,
            fontSize = 12.sp,
            color = logColor,
            fontFamily = FontFamily.Monospace,
            modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
            lineHeight = 16.sp
        )
    }
}
