package com.dong.demo013

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.rememberLazyListState
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.mutableStateOf
import androidx.compose.runtime.remember
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
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

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()
    val listState = rememberLazyListState()

    // ✅ 记录滚动开始和结束
    var isScrolling by remember { mutableStateOf(false) }
    var scrollStartTime by remember { mutableStateOf(0L) }

    // 监听滚动状态变化
    LaunchedEffect(scrollState.isScrollInProgress) {
        if (scrollState.isScrollInProgress && !isScrolling) {
            // 滚动开始
            isScrolling = true
            os_signal_set("滚动(开始)","1500_scroll",true)
        }
        else if (!scrollState.isScrollInProgress && isScrolling) {
            // 滚动结束
            isScrolling = false
            os_signal_set("滚动(结束)","1500_scroll",false)
        }
    }

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

        // 等布局给出可滚动高度后再滚到底，再等一帧记「完成」
        while (scrollState.maxValue == 0) {
            withFrameMillis { }
        }
        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 号" }
            )
        }
    }
}
