package com.example.kmptpc_compose_sample.demos.material

import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.text.intl.LocaleList
import androidx.compose.ui.unit.dp

@Composable
internal fun LocaleInfoDemo() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Text(
            text = "Locale 信息测试",
            style = MaterialTheme.typography.h6
        )
        Divider()

        // 当前 Locale 信息
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 4.dp
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Text(
                    text = "当前 Locale",
                    style = MaterialTheme.typography.subtitle1
                )
                val currentLocale = Locale.current
                LocaleInfoRow("语言标签", currentLocale.toLanguageTag())
                LocaleInfoRow("语言代码", currentLocale.language)
                LocaleInfoRow("脚本代码", currentLocale.script.ifEmpty { "(空)" })
                LocaleInfoRow("地区代码", currentLocale.region)
                // LocaleInfoRow("是否为 RTL", if (currentLocale.platformLocale.isRtl()) "是" else "否")
            }
        }

        // 首选语言列表
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 4.dp
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Text(
                    text = "首选语言列表",
                    style = MaterialTheme.typography.subtitle1
                )
                val preferredLocales = LocaleList.current
                preferredLocales.forEachIndexed { index, locale ->
                    LocaleInfoRow("语言 ${index + 1}", locale.toLanguageTag())
                }
            }
        }

        // 测试不同 Locale 的解析
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 4.dp
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Text(
                    text = "Locale 解析测试",
                    style = MaterialTheme.typography.subtitle1
                )
                val testLocales = listOf("zh-CN", "en-US", "tr-TR", "ar-SA", "ja-JP")
                testLocales.forEach { tag ->
                    val locale = Locale(tag)
                    Column(
                        modifier = Modifier.padding(vertical = 4.dp)
                    ) {
                        Text(
                            text = tag,
                            style = MaterialTheme.typography.body2,
                            color = MaterialTheme.colors.primary
                        )
                        LocaleInfoRow("  语言", locale.language)
                        LocaleInfoRow("  地区", locale.region)
                        // LocaleInfoRow("  RTL", if (locale.platformLocale.isRtl()) "是" else "否")
                    }
                }
            }
        }
    }
}

@Composable
private fun LocaleInfoRow(label: String, value: String) {
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween
    ) {
        Text(
            text = "$label:",
            style = MaterialTheme.typography.body2,
            color = MaterialTheme.colors.onSurface.copy(alpha = 0.6f)
        )
        Text(
            text = value,
            style = MaterialTheme.typography.body1
        )
    }
}

