package com.dong.demo013.Log

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.dong.demo013.LogController
import com.dong.demo013.TimerController
import com.dong.demo013.createTimer
import com.dong.demo013.startLog
import kotlinx.coroutines.delay

@OptIn(ExperimentalLayoutApi::class)
@Composable
internal fun HighLogDemo() {
    // 日志参数状态
    var logSpeed by remember { mutableStateOf("100") }
    var logCount by remember { mutableStateOf(0) }
    var isGenerating by remember { mutableStateOf(false) }
    var logController by remember { mutableStateOf<LogController?>(null) }
    
    // 计时器状态
    var timerController by remember { mutableStateOf<TimerController?>(null) }
    var displayTime by remember { mutableStateOf(0L) }
    
    // 初始化计时器
    LaunchedEffect(Unit) {
        timerController = createTimer()
    }
    
    // 定期更新显示时间
    LaunchedEffect(timerController) {
        while (true) {
            delay(100) // 每100ms更新一次显示
            displayTime = timerController?.elapsedTime ?: 0
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // 计时器显示区域
        Card(backgroundColor = MaterialTheme.colors.surface) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
            ) {
                Text(
                    "运行时间",
                    style = MaterialTheme.typography.h6,
                    modifier = Modifier.padding(bottom = 12.dp)
                )

                // 时间显示
                Card(
                    backgroundColor = MaterialTheme.colors.primary.copy(alpha = 0.1f),
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Column(
                        modifier = Modifier.padding(16.dp),
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Text(
                            "${displayTime}秒",
                            style = MaterialTheme.typography.h4,
                            color = MaterialTheme.colors.primary
                        )
                        if (isGenerating) {
                            Text(
                                "日志生成中...",
                                style = MaterialTheme.typography.caption,
                                color = MaterialTheme.colors.secondary
                            )
                        }
                    }
                }
            }
        }

        // 日志生成功能区域
        Card(backgroundColor = MaterialTheme.colors.surface) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
            ) {
                Text(
                    "日志生成工具",
                    style = MaterialTheme.typography.h6,
                    modifier = Modifier.padding(bottom = 12.dp)
                )

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    OutlinedTextField(
                        value = logSpeed,
                        onValueChange = { newValue ->
                            // 只允许数字输入
                            if (newValue.all { it.isDigit() }) {
                                logSpeed = newValue
                            }
                        },
                        label = { Text("间隔(ms)") },
                        modifier = Modifier.weight(1f),
                        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                        singleLine = true
                    )
                }

                Spacer(Modifier.height(12.dp))

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            val speed = logSpeed.toIntOrNull() ?: 100
                            logCount = 0
                            
                            // 重置并启动计时器
                            timerController?.reset()
                            timerController?.start()
                            
                            // 启动日志生成
                            isGenerating = true
                            logController = startLog(speed,
                                onLogGenerated = { count ->
                                    logCount = count
                                },
                                onTimerUpdated = { time ->
                                    // 日志生成的计时现在只用于内部统计，不更新UI
                                }
                            )
                        },
                        modifier = Modifier.weight(1f),
                        enabled = logSpeed.isNotEmpty() && !isGenerating
                    ) {
                        Text("开始生成")
                    }
                    
                    Button(
                        onClick = {
                            logController?.stop()
                            timerController?.stop()
                            isGenerating = false
                        },
                        modifier = Modifier.weight(1f),
                        enabled = isGenerating
                    ) {
                        Text("停止生成")
                    }
                }

                // 显示日志生成状态
                if (isGenerating || logCount > 0) {
                    Spacer(Modifier.height(12.dp))
                    Card(
                        backgroundColor = MaterialTheme.colors.secondary.copy(alpha = 0.1f),
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Column(
                            modifier = Modifier.padding(12.dp),
                            horizontalAlignment = Alignment.CenterHorizontally
                        ) {
                            Text(
                                "生成日志数量",
                                style = MaterialTheme.typography.subtitle1,
                                color = MaterialTheme.colors.secondary
                            )
                            Text(
                                "$logCount 条",
                                style = MaterialTheme.typography.h4,
                                color = MaterialTheme.colors.secondary,
                                modifier = Modifier.padding(top = 4.dp)
                            )
                        }
                    }
                }
            }
        }
    }
}