package com.example.kmptpc_compose_sample.navigation

enum class ComposeGroup {
    Ui, Foundation, Material, Material3, Game
}

data class ComponentDemo(
    val id: String,
    val title: String,
    val group: ComposeGroup
)

val componentDemos: List<ComponentDemo> = listOf(
    // ui
    ComponentDemo("ui_box", "Box", ComposeGroup.Ui),
    // foundation
    ComponentDemo("foundation_basic_text", "BasicText", ComposeGroup.Foundation),
    ComponentDemo("foundation_basic_text_field", "BasicTextField (BTF2)", ComposeGroup.Foundation),
    ComponentDemo("foundation_lazy_column", "LazyColumn", ComposeGroup.Foundation),
    ComponentDemo("foundation_haptic_feedback", "触觉反馈", ComposeGroup.Foundation),
    ComponentDemo("foundation_drag_and_drop", "拖拽", ComposeGroup.Foundation),
    ComponentDemo("foundation_text_selection", "文本选择", ComposeGroup.Foundation),
    ComponentDemo("foundation_text_field_selection_gesture", "文本框选择手势", ComposeGroup.Foundation),
    ComponentDemo("foundation_text_field_scroll", "文本框滚动", ComposeGroup.Foundation),
    ComponentDemo("navigation_animation_test", "导航动画测试", ComposeGroup.Foundation),
    ComponentDemo("navigation_library_test", "Navigation 库测试", ComposeGroup.Foundation),
    ComponentDemo("navigation_gesture_test", "导航手势测试", ComposeGroup.Foundation),
    ComponentDemo("navigation_typed_route_test", "Navigation 类型安全路由", ComposeGroup.Foundation),
    ComponentDemo("skiko_capabilities", "Skiko 能力", ComposeGroup.Foundation),
    ComponentDemo("orientation_safe_area", "界面方向与系统栏", ComposeGroup.Foundation),
    // material 组：新增完整清单
    ComponentDemo("material_alert_dialog", "AlertDialogSample", ComposeGroup.Material),
    ComponentDemo("material_app_bar", "AppBarSamples", ComposeGroup.Material),
    ComponentDemo("material_backdrop_scaffold", "BackdropScaffoldSamples", ComposeGroup.Material),
    ComponentDemo("material_badge", "BadgeSamples", ComposeGroup.Material),
    ComponentDemo("material_bottom_navigation", "BottomNavigationSamples", ComposeGroup.Material),
    ComponentDemo("material_bottom_sheet_scaffold", "BottomSheetScaffoldSamples", ComposeGroup.Material),
    ComponentDemo("material_button_samples", "ButtonSamples", ComposeGroup.Material),
    ComponentDemo("material_card_samples", "CardSamples", ComposeGroup.Material),
    ComponentDemo("material_chip_samples", "ChipSamples", ComposeGroup.Material),
    ComponentDemo("material_content_alpha", "ContentAlphaSamples", ComposeGroup.Material),
    ComponentDemo("material_drawer_samples", "DrawerSamples", ComposeGroup.Material),
    ComponentDemo("material_elevation_samples", "ElevationSamples", ComposeGroup.Material),
    ComponentDemo("material_exposed_dropdown_menu", "ExposedDropdownMenuSamples", ComposeGroup.Material),
    ComponentDemo("material_fab_samples", "FloatingActionButtonSamples", ComposeGroup.Material),
    ComponentDemo("material_icon_button_samples", "IconButtonSamples", ComposeGroup.Material),
    ComponentDemo("material_list_samples", "ListSamples", ComposeGroup.Material),
    ComponentDemo("material_menu_samples", "MenuSamples", ComposeGroup.Material),
    ComponentDemo("material_modal_bottom_sheet", "ModalBottomSheetSamples", ComposeGroup.Material),
    ComponentDemo("material_navigation_rail", "NavigationRailSample", ComposeGroup.Material),
    ComponentDemo("material_progress_indicator", "ProgressIndicatorSamples", ComposeGroup.Material),
    ComponentDemo("material_pull_refresh", "PullRefreshSamples", ComposeGroup.Material),
    ComponentDemo("material_scaffold_samples", "ScaffoldSamples", ComposeGroup.Material),
    ComponentDemo("material_selection_controls", "SelectionControlsSamples", ComposeGroup.Material),
    ComponentDemo("material_slider_sample", "SliderSample", ComposeGroup.Material),
    ComponentDemo("material_surface_samples", "SurfaceSamples", ComposeGroup.Material),
    ComponentDemo("material_swipeable_samples", "SwipeableSamples", ComposeGroup.Material),
    ComponentDemo("material_swipe_to_dismiss", "SwipeToDismissSamples", ComposeGroup.Material),
    ComponentDemo("material_tab_samples", "TabSamples", ComposeGroup.Material),
    ComponentDemo("material_text_field_samples", "TextFieldSamples", ComposeGroup.Material),
    ComponentDemo("material_text_samples", "TextSamples", ComposeGroup.Material),
    ComponentDemo("material_theme_samples", "ThemeSamples", ComposeGroup.Material),
    ComponentDemo("material_locale_info", "本地化信息", ComposeGroup.Material),
    ComponentDemo("material_text_field_locale", "字符串本地化", ComposeGroup.Material),
    // material3 组（与 Material / Foundation 平级）
    ComponentDemo("material3_button", "M3 Button", ComposeGroup.Material3),
    ComponentDemo("material3_text_field", "M3 TextField", ComposeGroup.Material3),
    ComponentDemo("material3_card", "M3 Card", ComposeGroup.Material3),
    ComponentDemo("material3_alert_dialog", "M3 AlertDialog", ComposeGroup.Material3),
    ComponentDemo("material3_switch", "M3 Switch", ComposeGroup.Material3),
    ComponentDemo("material3_fab", "M3 FAB", ComposeGroup.Material3),
    ComponentDemo("material3_icon_button", "M3 IconButton", ComposeGroup.Material3),
    ComponentDemo("material3_date_picker", "M3 DatePicker（日历）", ComposeGroup.Material3),
    ComponentDemo("material3_date_picker_input", "M3 DatePicker（输入）", ComposeGroup.Material3),
    ComponentDemo("material3_date_picker_dialog", "M3 DatePickerDialog", ComposeGroup.Material3),
    ComponentDemo("material3_date_range_picker", "M3 DateRangePicker", ComposeGroup.Material3),
    ComponentDemo("lib01", "Lib01 页面（100 组件）", ComposeGroup.Game),
)

val groupTitles: Map<ComposeGroup, String> = mapOf(
    ComposeGroup.Ui to "compose.ui",
    ComposeGroup.Foundation to "compose.foundation",
    ComposeGroup.Material to "compose.material",
    ComposeGroup.Material3 to "compose.material3",
    ComposeGroup.Game to "小游戏",
)

fun demosByGroup(): Map<ComposeGroup, List<ComponentDemo>> =
    ComposeGroup.entries.associateWith { g -> componentDemos.filter { it.group == g } }

fun demosJson(): String = buildString {
    append('[')
    componentDemos.forEachIndexed { index, demo ->
        if (index > 0) append(',')
        append("{\"id\":\"")
        append(jsonEscape(demo.id))
        append("\",\"title\":\"")
        append(jsonEscape(demo.title))
        append("\",\"group\":\"")
        append(demo.group.name)
        append("\"}")
    }
    append(']')
}

fun groupTitlesJson(): String = buildString {
    append('{')
    ComposeGroup.entries.forEachIndexed { index, group ->
        if (index > 0) append(',')
        append('"')
        append(group.name)
        append("\":\"")
        append(jsonEscape(groupTitles[group] ?: group.name))
        append('"')
    }
    append('}')
}

private fun jsonEscape(input: String): String = buildString(input.length) {
    input.forEach { ch ->
        when (ch) {
            '\\' -> append("\\\\")
            '"' -> append("\\\"")
            '\n' -> append("\\n")
            '\r' -> append("\\r")
            '\t' -> append("\\t")
            else -> append(ch)
        }
    }
}

sealed interface Page {
    data object Home : Page
    data class Detail(val demo: ComponentDemo) : Page
}
