package com.lanyun.wanglaotou.public

import android.view.MenuItem
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.VectorConverter
import androidx.compose.animation.core.spring
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.detectVerticalDragGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.*
import androidx.compose.runtime.R
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import com.lanyun.wanglaotou.viewModel.NodeViewAction
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlin.math.roundToInt

//@Composable
//fun PositionSelector(position: Float, onPositionChanged: (Float) -> Unit) {
//    Text(
//        text = "Position $position",
//        modifier = Modifier.pointerInput(Unit) {
//            detectVerticalDragGestures { _, dragAmount ->
//                onPositionChanged(position - dragAmount)
//            }
//        })
//}


@Composable
fun FollowMouseDropdownMenu(
    items: List<String>,
    onItemSelected: (Int) -> Unit
) {
    var expanded by remember { mutableStateOf(false) }
    val animatedOffset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) }
    Box(
        modifier = Modifier
            .pointerInput(Unit) {
                coroutineScope {
                    while (true) {
                        //获取点击位置
                        val boxOffset = awaitPointerEventScope {
                            awaitFirstDown().position
                        }
                        expanded = true
                        launch {
                            animatedOffset.animateTo(
                                boxOffset,
                                animationSpec = spring(stiffness = Spring.StiffnessLow)
                            )
                        }

                    }
                }
            }

    ) {
        Icon(
            Icons.Default.MoreVert, contentDescription = null, modifier = Modifier
            .clickable {
                expanded = !expanded
            }
            .padding(start = 10.dp), tint = MaterialTheme.colors.background)
        Box(modifier = Modifier.offset {
            IntOffset(
                animatedOffset.value.x.roundToInt(),
                animatedOffset.value.y.roundToInt()
            )
        } ) {
            DropdownMenu(
                expanded = expanded,
                onDismissRequest = { expanded = false }
            ) {

                items.forEachIndexed { index, s ->
                    DropdownMenuItem(onClick = {
                        onItemSelected(index)
                        expanded = false
                    }) {
                        Text(s)
                    }
                }

            }
        }
    }
}
