package com.example.kmptpc_compose_sample.demos.material
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.InputTransformation
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.foundation.text.input.allCaps
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.KeyboardOptions
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.text.capitalize
import androidx.compose.ui.text.decapitalize
import androidx.compose.ui.text.toLowerCase
import androidx.compose.ui.text.toUpperCase
import androidx.compose.ui.text.intl.Locale
import androidx.compose.ui.text.intl.LocaleList
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.text.input.TextFieldBuffer

@OptIn(ExperimentalFoundationApi::class)
@Composable
internal fun TextFieldLocaleComparisonDemo() {
    val currentLocale = Locale.current
    val scrollState = rememberScrollState()

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
            .verticalScroll(scrollState),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        Text(
            text = "字符串本地化测试",
            style = MaterialTheme.typography.h6
        )
        Divider()

        // ========== NativePlatformLocale 能力测试 ==========
        Text(
            text = "1. PlatformLocale 能力 (NativePlatformLocale)",
            style = MaterialTheme.typography.subtitle1,
            color = MaterialTheme.colors.primary
        )
        // 解析多种 languageTag，展示 language / region / script / toLanguageTag
        val testTags = listOf("zh-CN", "en-US", "tr-TR", "sr-Latn-RS", "ar-EG")
        testTags.forEach { tag ->
            val locale = remember(tag) { Locale(tag) }
            Card(
                modifier = Modifier.fillMaxWidth(),
                elevation = 1.dp
            ) {
                Column(modifier = Modifier.padding(8.dp)) {
                    Text("languageTag: ${locale.toLanguageTag()}", style = MaterialTheme.typography.caption)
                    Text("language: ${locale.language}, region: ${locale.region}, script: ${locale.script}")
                }
            }
        }
        // LocaleList.current（系统首选语言列表）
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 1.dp
        ) {
            Column(modifier = Modifier.padding(8.dp)) {
                Text("LocaleList.current (首选语言列表)", style = MaterialTheme.typography.caption)
                Text(
                    LocaleList.current.localeList.joinToString { it.toLanguageTag() }.ifEmpty { "(空)" }
                )
            }
        }

        Divider()

        // ========== NativeStringDelegate 能力测试 (toUpperCase / toLowerCase / capitalize / decapitalize) ==========
        Text(
            text = "2. StringDelegate 能力 (NativeStringDelegate)",
            style = MaterialTheme.typography.subtitle1,
            color = MaterialTheme.colors.primary
        )
        var delegateInput by remember { mutableStateOf("hello world") }
        val delegateLocale = currentLocale
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 1.dp
        ) {
            Column(
                modifier = Modifier.padding(12.dp),
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Text("输入文本，观察按当前 Locale 的大小写转换结果：", style = MaterialTheme.typography.caption)
                OutlinedTextField(
                    value = delegateInput,
                    onValueChange = { delegateInput = it },
                    modifier = Modifier.fillMaxWidth(),
                    singleLine = true
                )
                Text("toUpperCase: ${delegateInput.toUpperCase(delegateLocale)}")
                Text("toLowerCase: ${delegateInput.toLowerCase(delegateLocale)}")
                Text("capitalize: ${delegateInput.capitalize(delegateLocale)}")
                Text("decapitalize: ${delegateInput.decapitalize(delegateLocale)}")
            }
        }
        // 可选：用不同 Locale 对比（如土耳其 tr 的 I -> ı）
        var compareInput by remember { mutableStateOf("i") }
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = 1.dp
        ) {
            Column(
                modifier = Modifier.padding(12.dp),
                verticalArrangement = Arrangement.spacedBy(4.dp)
            ) {
                Text("多 Locale 对比 (如 tr 中 i↔I 与 en 不同)：", style = MaterialTheme.typography.caption)
                OutlinedTextField(
                    value = compareInput,
                    onValueChange = { compareInput = it },
                    modifier = Modifier.fillMaxWidth(),
                    singleLine = true
                )
                val trLocale = remember { Locale("tr-TR") }
                val enLocale = remember { Locale("en-US") }
                Text("tr-TR toUpperCase: \"$compareInput\" → \"${compareInput.toUpperCase(trLocale)}\"")
                Text("en-US toUpperCase: \"$compareInput\" → \"${compareInput.toUpperCase(enLocale)}\"")
            }
        }

    }
}
