package com.dong.maxhap

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier

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

        // 首页状态提升：滚动、搜索、分组展开状态
        val listState = androidx.compose.foundation.lazy.rememberLazyListState()
        var searchQuery by remember { mutableStateOf("") }
        val expandedGroups = remember {
            mutableStateMapOf<com.dong.maxhap.navigation.ComposeGroup, Boolean>().apply {
                com.dong.maxhap.navigation.ComposeGroup.entries.forEach { put(it, true) }
            }
        }


        Column(
            modifier = Modifier
                .safeContentPadding()
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            when (val p = page) {
                is com.dong.maxhap.navigation.Page.Home -> {
                    HomeScreen(
                        onSelect = { demo ->
                            page = com.dong.maxhap.navigation.Page.Detail(demo)
                        },
                        listState = listState,
                        searchQuery = searchQuery,
                        onSearchChange = { searchQuery = it },
                        expandedGroups = expandedGroups
                    )
                }
                is com.dong.maxhap.navigation.Page.Detail -> {
                    DemoScreen(demo = p.demo, onBack = { page = com.dong.maxhap.navigation.Page.Home })
                }
            }
        }
    }
}
