package com.pixelized.desktop.lwa.ui.composable.tooltip

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.TooltipArea
import androidx.compose.foundation.TooltipPlacement
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp

@Immutable
object TooltipDefault {
    @Stable
    val delay: Int = 500

    @OptIn(ExperimentalFoundationApi::class)
    @Stable
    val placement: TooltipPlacement = TooltipPlacement.CursorPoint(DpOffset(0.dp, 16.dp))
}

@Composable
@OptIn(ExperimentalFoundationApi::class)
fun <T> TooltipLayout(
    modifier: Modifier = Modifier,
    delayMillis: Int = TooltipDefault.delay,
    tooltipPlacement: TooltipPlacement = TooltipDefault.placement,
    tips: T? = null,
    tooltip: (@Composable (tips: T) -> Unit)? = null,
    content: @Composable () -> Unit,
) {
    if (tips != null && tooltip != null) {
        TooltipArea(
            modifier = modifier,
            tooltip = { tooltip(tips) },
            content = content,
            delayMillis = delayMillis,
            tooltipPlacement = tooltipPlacement,
        )
    } else {
        Box(
            modifier = modifier,
            content = { content() },
        )
    }
}
