package com.aiosman.ravenow.ui.composables

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.platform.debugInspectorInfo
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 防抖点击修饰符
 * @param enabled 是否启用点击
 * @param debounceTime 防抖时间（毫秒），默认500ms
 * @param onClick 点击回调
 */
fun Modifier.debouncedClickable(
    enabled: Boolean = true,
    debounceTime: Long = 500L,
    onClick: () -> Unit
): Modifier = composed(
    inspectorInfo = debugInspectorInfo {
        name = "debouncedClickable"
        properties["enabled"] = enabled
        properties["debounceTime"] = debounceTime
    }
) {
    var isClickable by remember { mutableStateOf(true) }
    val scope = rememberCoroutineScope()
    
    clickable(
        enabled = enabled && isClickable,
        interactionSource = remember { MutableInteractionSource() },
        indication = null
    ) {
        if (isClickable) {
            isClickable = false
            onClick()
            scope.launch {
                delay(debounceTime)
                isClickable = true
            }
        }
    }
}

/**
 * 防抖点击修饰符（带涟漪效果）
 * @param enabled 是否启用点击
 * @param debounceTime 防抖时间（毫秒），默认500ms
 * @param onClick 点击回调
 */
fun Modifier.debouncedClickableWithRipple(
    enabled: Boolean = true,
    debounceTime: Long = 500L,
    onClick: () -> Unit
): Modifier = composed(
    inspectorInfo = debugInspectorInfo {
        name = "debouncedClickableWithRipple"
        properties["enabled"] = enabled
        properties["debounceTime"] = debounceTime
    }
) {
    var isClickable by remember { mutableStateOf(true) }
    val scope = rememberCoroutineScope()
    
    clickable(
        enabled = enabled && isClickable,
        interactionSource = remember { MutableInteractionSource() },
        indication = androidx.compose.material.ripple.rememberRipple()
    ) {
        if (isClickable) {
            isClickable = false
            onClick()
            scope.launch {
                delay(debounceTime)
                isClickable = true
            }
        }
    }
}

/**
 * 通用防抖处理器
 * 可以用于任何需要防抖的场景
 */
@Composable
fun rememberDebouncer(debounceTime: Long = 500L): ((() -> Unit) -> Unit) {
    var isExecuting by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()
    
    return remember {
        { action ->
            if (!isExecuting) {
                isExecuting = true
                action()
                scope.launch {
                    delay(debounceTime)
                    isExecuting = false
                }
            }
        }
    }
}

/**
 * 防抖状态管理器
 * 可以手动控制防抖状态
 */
@Composable
fun rememberDebouncedState(
    debounceTime: Long = 500L
): Triple<Boolean, () -> Unit, () -> Unit> {
    var isDebouncing by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()
    
    val startDebounce: () -> Unit = remember {
        {
            isDebouncing = true
            scope.launch {
                delay(debounceTime)
                isDebouncing = false
            }
        }
    }
    
    val reset = remember {
        {
            isDebouncing = false
        }
    }
    
    return Triple(isDebouncing, startDebounce, reset)
}

/**
 * 防抖的导航处理器
 * 专门用于导航操作的防抖
 */
@Composable
fun rememberDebouncedNavigation(
    debounceTime: Long = 1000L
): ((() -> Unit) -> Unit) {
    var isNavigating by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()
    
    return remember {
        { navigation ->
            if (!isNavigating) {
                isNavigating = true
                try {
                    navigation()
                } finally {
                    scope.launch {
                        delay(debounceTime)
                        isNavigating = false
                    }
                }
            }
        }
    }
}
