package com.example.kmptpc_compose_sample.demos.foundation

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * Demo 用于测试文本选择（SelectionController）：
 * - 触摸：长按选词，拖拽扩展选区；双击选词，三击选段。
 * - 鼠标/触控板：单击定位、双击选词、三击选段；Shift+拖拽扩展选区。
 */
@Composable
internal fun FoundationTextSelectionDemo() {
    val scrollState = rememberScrollState()

    Column(
        modifier = Modifier
            .verticalScroll(scrollState)
            .padding(16.dp)
    ) {
        Text(
            "文本选择测试",
            style = MaterialTheme.typography.h6,
            modifier = Modifier.padding(bottom = 8.dp)
        )
        Text(
            "下方文本支持选区。触摸：长按选词并拖拽扩展；双击选词、三击选段。",
            style = MaterialTheme.typography.caption,
            modifier = Modifier.padding(bottom = 16.dp)
        )

        SelectionContainer(
            modifier = Modifier
                .fillMaxWidth()
                .background(MaterialTheme.colors.surface)
                .padding(16.dp)
        ) {
            Column {
                Text(
                    text = "第一段：请长按任意词语开始选择，然后拖拽以扩展选区。在支持鼠标/触控板的设备上，可双击选词、三击选段，或按住 Shift 拖拽扩展选区。",
                    style = MaterialTheme.typography.body1,
                    modifier = Modifier.padding(bottom = 12.dp)
                )
                Text(
                    text = "第二段：This is English text. You can select words by long-press (touch) or double-click (pointer). 中文与 English 混排也可正常选区。",
                    style = MaterialTheme.typography.body1,
                    modifier = Modifier.padding(bottom = 12.dp)
                )
                Text(
                    text = buildAnnotatedString {
                        append("第三段：")
                        withStyle(SpanStyle(color = MaterialTheme.colors.primary)) {
                            append("高亮样式")
                        }
                        append(" 与普通文字均可被选中。")
                    },
                    style = MaterialTheme.typography.body1
                )
            }
        }

        Spacer(Modifier.height(24.dp))

        Text(
            "单行可选区文本（便于快速验证）：",
            style = MaterialTheme.typography.caption,
            modifier = Modifier.padding(bottom = 8.dp)
        )
        SelectionContainer(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color(0xFFE8E8E8))
                .padding(12.dp)
        ) {
            Text(
                text = "Quick select: 快速选择这段文字以验证 SelectionController 是否生效。",
                style = MaterialTheme.typography.body1,
                fontSize = 14.sp
            )
        }
    }
}
