package com.dong.demo013

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.dong.demo013.Log.HighLogDemo
import com.dong.demo013.navigation.ComponentDemo
import com.dong.demo013.navigation.ComposeGroup
import com.dong.demo013.navigation.Page
import com.dong.maxhap.demos.FaultLogDemo
import com.dong.maxhap.demos.NativeFaultDemo

@Composable
internal fun App() {
    MaterialTheme {
        // 使用 Page 类型的状态，修复类型推断报错
        var page by remember { mutableStateOf<Page>(Page.Home) }

        when (val p = page) {
            is Page.Home -> {
                HomeScreen(
                    onSelect = { demo ->
                        page = Page.Detail(demo)
                    }
                )
            }
            is Page.Detail -> {
                DemoScreen(demo = p.demo, onBack = { page = Page.Home })
            }
        }
    }
}

@Composable
internal fun HomeScreen(
    onSelect: (ComponentDemo) -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // 第一个模块：嵌套页面
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 8.dp,
            shape = RoundedCornerShape(12.dp)
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
            ) {
                Text(
                    text = "debug测试",
                    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(12.dp)) {
                        Text("还在施工中........", style = MaterialTheme.typography.body1)
                        Spacer(Modifier.height(8.dp))
                    }
                }
            }
        }

        // 第二个模块：日志测试
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 8.dp,
            shape = RoundedCornerShape(12.dp)
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
            ) {
                Text(
                    text = "日志测试",
                    style = MaterialTheme.typography.h6,
                    modifier = Modifier.padding(bottom = 12.dp)
                )
                
                // 日志测试按钮区域
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = { 
                            val logDemo = ComponentDemo(
                                id = "log_test_demo",
                                title = "日志测试演示(持续产生)",
                                group = ComposeGroup.Foundation
                            )
                            onSelect(logDemo)
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("进入日志测试")
                    }

                }
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            val logDemo = ComponentDemo(
                                id = "log_fault_demo",
                                title = "Fault日志",
                                group = ComposeGroup.Foundation
                            )
                            onSelect(logDemo)
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("进入Fault日志测试")
                    }

                }
            }
        }


    }
}

@Composable
internal fun DemoScreen(
    demo: ComponentDemo,
    onBack: () -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        // 返回按钮
        Button(
            onClick = onBack,
            modifier = Modifier.padding(bottom = 16.dp)
        ) {
            Text("返回首页")
        }
        
        // 根据 demo.id 显示对应的内容
        when (demo.id) {
            "log_test_demo" -> {
                HighLogDemo()
            }
            "log_fault_demo" -> {
                FaultLogDemo()
            }
            "log_over_demo" -> {
                NativeFaultDemo()
            }
            else -> {
                Text(
                    text = "暂未实现: ${demo.title}",
                    style = MaterialTheme.typography.h6
                )
            }
        }
    }
}