package com.dong.demo013

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.dong.demo013.Log.HighLogDemo
import com.dong.demo013.debug.DebugStepIntoDemo
import com.dong.demo013.debug.DebugStepOutDemo
import com.dong.demo013.debug.DebugStepOverDemo
import com.dong.demo013.debug.DebugTestDemo
import com.dong.demo013.debug.allBreakpoints
import com.dong.demo013.debug.debugall
import com.dong.demo013.debug.tenComplexObject
import com.dong.demo013.debug.testFrame1
import com.dong.demo013.debug.threeComplexObject
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
import kotlin.native.concurrent.ThreadLocal

@Composable
internal fun App() {
    LaunchedEffect(Unit){
        allBreakpoints()
    }
    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)
            .verticalScroll(rememberScrollState())
        ,
        verticalArrangement = Arrangement.spacedBy(16.dp),
    ) {
        // 第一个模块：debug页面
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 8.dp,
            shape = RoundedCornerShape(12.dp)
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp)
            ) {
                // debug测试按钮区域
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Text("断点测试")
                    Button(
                        onClick = {
                            allBreakpoints()
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("attach断点测试触发")
                    }

                    Button(
                        onClick = {
                            testFrame1()
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("测试不同线帧变量")
                    }
//
//                    Button(
//                        onClick = {
//                            val list = listOf("A", "B", "C")
//                            // 直接越界访问 - 立即崩溃
//                            val item = list[5]  // ArrayIndexOutOfBoundsException
//                            println(item)
//                        },
//                        modifier = Modifier.fillMaxWidth()
//                    ) {
//                        Text("测试debug调试时应用退出")
//                    }
//                    Button(
//                        onClick = {
//                            // 触发卡死
//                            val testInfinite: Boolean = true;
//                            fun infinite(): Int = infinite() + 1
//                            infinite()  // 触发 StackOverflowError
//                        },
//                        modifier = Modifier.fillMaxWidth()
//                    ) {
//                        Text("测试debug调试时应用卡死")
//                    }


                }
                // 复杂对象
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            threeComplexObject()
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("普通复杂对象断点测试触发")
                    }

                }
                // 复杂对象
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            tenComplexObject()
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("多层复杂对象断点测试触发")
                    }

                }
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Text("单步调试")
                    Button(
                        onClick = {
                            val debugDataDemo = ComponentDemo(
                                id = "debug_stepover_demo",
                                title = "单步调试-步过",
                                group = ComposeGroup.Foundation
                            )
                            onSelect(debugDataDemo)
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("进入单步调试-步过")
                    }

                }
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            val debugDataDemo = ComponentDemo(
                                id = "debug_stepinto_demo",
                                title = "单步调试-步进",
                                group = ComposeGroup.Foundation
                            )
                            onSelect(debugDataDemo)
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("进入单步调试-步进")
                    }

                }
                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            val debugDataDemo = ComponentDemo(
                                id = "debug_stepout_demo",
                                title = "单步调试-步出",
                                group = ComposeGroup.Foundation
                            )
                            onSelect(debugDataDemo)
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("进入单步调试-步出")
                    }

                }
            }
        }

        // 第二个模块：日志测试
        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)
                ) {
                    Text("日志打印")
                    Button(
                        onClick = {
                            try {
                                pirntSingleHiLog()
                            } catch (e: Exception) {
                                print("printSingleLog error")
                            }
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("单次日志测试")
                    }

                }

                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(
                        onClick = {
                            pirntAllLevelHiLog();
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("一次打印全部层级日志")
                    }

                }

                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)
                ) {
                    Text("无限递归 - 栈溢出（延迟崩溃）")
                    Button(
                        onClick = {
                            // 无限递归 - 栈溢出
                            val testInfinite: Boolean = true;
                            fun infinite(): Int = infinite() + 1
                            infinite()  // 触发 StackOverflowError
                        },
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Text("测试崩溃日志")
                    }

                }


            }
        }


    }
}

@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()
            }
            //调试
            "debug_test_demo" -> {
                DebugTestDemo()
            }
            "debug_stepover_demo" -> {
                DebugStepOverDemo()
            }
            "debug_stepinto_demo" -> {
                DebugStepIntoDemo()
            }
            "debug_stepout_demo" -> {
                DebugStepOutDemo()
            }
            else -> {
                Text(
                    text = "暂未实现: ${demo.title}",
                    style = MaterialTheme.typography.h6
                )
            }
        }
    }
}

