package com.lanyun.wanglaotou.composable

import android.util.Log
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.gestures.awaitFirstDown
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.lanyun.common.baseDate.updateMainEvent
import com.lanyun.common.model.MainEvent
import com.lanyun.common.widget.sleepTime
import com.lanyun.wanglaotou.public.*
import com.lanyun.wanglaotou.viewModel.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch


@Composable
fun NodeCompose(
    navHostController: NavHostController,
    viewModel: NodeViewModel = viewModel()
) {
    val viewStates = viewModel.viewStates

    LaunchedEffect(Unit) {
//        viewModel.viewEvents.collect {
//            if (it is NodeViewEvent.MsTest) {
//
//            } else if (it is NodeViewEvent.ErrorMessage) {
//
//            }
//        }
    }

    LcePage(pageState = viewStates.pageState, onRetry = {
        viewModel.dispatch(NodeViewAction.UserNode)
    }) {
        BaseScreen {

            MyDropdownMenu(viewModel)

            AppBar(
                "购买",
                leftIcon= Icons.Default.ArrowBack,
                rightIcon = Icons.Default.MoreVert,
                onLeftClick={
                    navHostController.navigateUp()
                },
                onRightClick = {
                    viewModel.dispatch(NodeViewAction.UpdateExpanded(true))
                }
            )



            SwipeRefreshContent(
                viewModel = viewModel,
                listData = viewStates.dataList,
                state = viewStates.listState,
                itemContent = {
                },
                noData = {
                    updateMainEvent(MainEvent.Login)
                },
                content = { index,data ->
                    val ms = viewModel.getMs(data.name)
                    Log.d("ms","index${index},ms{$ms},name{${data.name}}")
                    HomeCardItemContent(data, ms = ms, selected = viewStates.selectedIndex == index,onClick = {
                        viewModel.dispatch(NodeViewAction.UpdateSelect(selectedIndex = index))
                        viewModel.selectNode(data)
                    })
                },
                bottomContent = {

                }
            )
        }
    }

}


@Composable
private  fun MyDropdownMenu(
    viewModel: NodeViewModel
) {

    Box(
        modifier = Modifier
            .wrapContentSize(Alignment.TopEnd)

    ) {
        DropdownMenu(
            offset = DpOffset(x = (-66).dp, y = (-10).dp),
            expanded = viewModel.viewStates.expanded,
            onDismissRequest = {
                viewModel.dispatch(NodeViewAction.UpdateExpanded(false))
            },
        ) {
            DropdownMenuItem(onClick = {
                viewModel.dispatch(NodeViewAction.NodeRefresh(true))
                viewModel.dispatch(NodeViewAction.UpdateExpanded(false))
            }) {
                Text("刷新线路")
            }
            DropdownMenuItem(onClick = {
                viewModel.testAllTcping()
                // updateMainEvent(MainEvent.MainTcpPing)
                viewModel.dispatch(NodeViewAction.UpdateExpanded(false))
            }) {
                Text("线路延迟检测")
            }

        }
    }

}