package com.dong.demo013

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.withFrameMillis
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

private fun perfPlatformTag(): String {
    val n = "11111"
    return when {
        n.contains("iOS", ignoreCase = true) || n.contains("iPadOS", ignoreCase = true) -> "ios"
        n.contains("Android", ignoreCase = true) -> "android"
        else -> "harmony"
    }
}

@Composable
internal fun Compose1500Text() {
    val scrollState = rememberScrollState()
    val platformTag = perfPlatformTag()

    LaunchedEffect(Unit) {
        // 至少等到一帧真正调度后再记「首帧」，避免与点击同一时刻的伪首帧
        withFrameMillis {}//组合完成
        //等一帧-开始渲染
        os_signal_set("响应时间-上帧(结束)","respose_time",false)

        // 等布局给出可滚动高度后再滚到底，再等一帧记「完成」- 确保可交互
        while (scrollState.maxValue == 0) {
            withFrameMillis { }
        }

        os_signal_set("结束时间-加载完成+可交互(结束)","finish_time",false)

        //这里发送开始滚动
        os_signal_set("滚动(开始)","1500_scroll",true)
        scrollState.scrollTo(scrollState.maxValue)
        withFrameMillis { }
        os_signal_set("滚动(结束)","1500_scroll",false)
    }

    Column(
        modifier =
            Modifier.verticalScroll(scrollState).fillMaxWidth().semantics {
                contentDescription = "包含 1500 个文本项目的性能测试列表"
            },
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        repeat(1500) { index ->
            Text(
                text = "Compose1500Text Item #$index",
                fontSize = 16.sp,
                modifier =
                    Modifier.width(300.dp)
                        .height(50.dp)
                        .border(1.dp, Color.Gray)
                        .padding(10.dp)
                        .semantics { contentDescription = "文本项目 第 $index 号" }
            )
        }
    }
}
