package com.example.kmptpc_compose_sample.demos.foundation

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.IntVar
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.value
import org.jetbrains.skiko.ClipboardManager
import org.jetbrains.skiko.URIManager
import org.jetbrains.skiko.currentSystemTheme
import platform.BasicServicesKit.Pasteboard.OH_Pasteboard_Create
import platform.BasicServicesKit.Pasteboard.OH_Pasteboard_Destroy
import platform.BasicServicesKit.Pasteboard.OH_Pasteboard_GetData
import platform.BasicServicesKit.Pasteboard.* 

@Composable
internal actual fun SkikoCapabilitiesDemoContent() {
    val clipboard = remember { ClipboardManager() }
    var clipboardInput by remember { mutableStateOf("Skiko 测试文本") }
    var clipboardReadResult by remember { mutableStateOf<String?>(null) }
    var hasTextResult by remember { mutableStateOf<Boolean?>(null) }
    var pasteboardGetDataStatus by remember { mutableStateOf<Int?>(null) }

    Column(
        modifier = Modifier.fillMaxWidth(),
        verticalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        // 1. 当前系统主题 (currentSystemTheme)
        Card(
            modifier = Modifier.fillMaxWidth(),
            backgroundColor = MaterialTheme.colors.surface,
            elevation = 2.dp
        ) {
            Column(Modifier.padding(16.dp)) {
                Text(
                    text = "1. 系统主题 (currentSystemTheme)",
                    style = MaterialTheme.typography.subtitle1
                )
                Spacer(modifier = Modifier.height(8.dp))
                Text(
                    text = "当前主题: $currentSystemTheme",
                    style = MaterialTheme.typography.body1
                )
            }
        }

        // 2. 打开 URI (URIManager.openUri)
        Card(
            modifier = Modifier.fillMaxWidth(),
            backgroundColor = MaterialTheme.colors.surface,
            elevation = 2.dp
        ) {
            Column(Modifier.padding(16.dp)) {
                Text(
                    text = "2. 打开链接 (URIManager.openUri)",
                    style = MaterialTheme.typography.subtitle1
                )
                Spacer(modifier = Modifier.height(8.dp))
                Button(
                    onClick = { URIManager().openUri("https://www.baidu.com") }
                ) {
                    Text("打开 https://www.baidu.com")
                }
            }
        }

        // 3. 剪贴板 setText / getText / hasText
        Card(
            modifier = Modifier.fillMaxWidth(),
            backgroundColor = MaterialTheme.colors.surface,
            elevation = 2.dp
        ) {
            Column(Modifier.padding(16.dp)) {
                Text(
                    text = "3. 剪贴板 (ClipboardManager)注意需要获取权限",
                    style = MaterialTheme.typography.subtitle1
                )
                Spacer(modifier = Modifier.height(8.dp))
                OutlinedTextField(
                    value = clipboardInput,
                    onValueChange = { clipboardInput = it },
                    label = { Text("要写入剪贴板的内容") },
                    modifier = Modifier.fillMaxWidth()
                )
                Spacer(modifier = Modifier.height(8.dp))
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    Button(onClick = { clipboard.setText(clipboardInput) }) {
                        Text("setText")
                    }
                    Button(onClick = { clipboardReadResult = clipboard.getText() }) {
                        Text("getText")
                    }
                    Button(onClick = { hasTextResult = clipboard.hasText() }) {
                        Text("hasText")
                    }
                }
                if (clipboardReadResult != null || hasTextResult != null) {
                    Spacer(modifier = Modifier.height(8.dp))
                    SelectionContainer {
                        Column {
                            clipboardReadResult?.let { text ->
                                Text(
                                    text = "getText() = \"$text\"",
                                    style = MaterialTheme.typography.body2
                                )
                            }
                            hasTextResult?.let { has ->
                                Text(
                                    text = "hasText() = $has",
                                    style = MaterialTheme.typography.body2
                                )
                            }
                        }
                    }
                }
            }
        }

        // 4. 系统剪贴板 C API：OH_Pasteboard_GetData 触发读权限申请
        Card(
            modifier = Modifier.fillMaxWidth(),
            backgroundColor = MaterialTheme.colors.surface,
            elevation = 2.dp
        ) {
            Column(Modifier.padding(16.dp)) {
                Text(
                    text = "4. 系统剪贴板 C API (OH_Pasteboard_GetData)",
                    style = MaterialTheme.typography.subtitle1
                )
                Text(
                    text = "调用 OH_Pasteboard_GetData 会触发 ohos.permission.READ_PASTEBOARD 权限申请",
                    style = MaterialTheme.typography.body2,
                    modifier = Modifier.padding(vertical = 4.dp)
                )
                Spacer(modifier = Modifier.height(8.dp))
                Button(
                    onClick = {
                        pasteboardGetDataStatus = runPasteboardGetData()
                    }
                ) {
                    Text("调用 OH_Pasteboard_GetData 触发权限")
                }
                pasteboardGetDataStatus?.let { status ->
                    Spacer(modifier = Modifier.height(8.dp))
                    Text(
                        text = "OH_Pasteboard_GetData 返回 status = $status (0=成功)",
                        style = MaterialTheme.typography.body2
                    )
                }
            }
        }
    }
}

@OptIn(ExperimentalForeignApi::class)
private fun runPasteboardGetData(): Int {
    val pb = OH_Pasteboard_Create() ?: return -1
    return try {
        memScoped {
            val status = alloc<IntVar>()
            OH_Pasteboard_GetData(pb, status.ptr)
            status.value
        }
    } finally {
        OH_Pasteboard_Destroy(pb)
    }
}
