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

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.TooltipPlacement
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp

@Stable
class BasicTooltipUio(
    val title: String?,
    val description: String,
)

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

    @Stable
    val elevation: Dp = 2.dp

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

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun BasicTooltipLayout(
    modifier: Modifier = Modifier,
    delayMillis: Int = BasicTooltipDefault.delay,
    elevation: Dp = BasicTooltipDefault.elevation,
    tooltipPlacement: TooltipPlacement = BasicTooltipDefault.placement,
    tooltip: BasicTooltipUio?,
    content: @Composable () -> Unit,
) {
    TooltipLayout(
        modifier = modifier,
        delayMillis = delayMillis,
        tooltipPlacement = tooltipPlacement,
        tips = tooltip,
        tooltip = {
            BasicTooltip(
                modifier = Modifier.widthIn(max = 448.dp),
                elevation = elevation,
                tooltip = it,
            )
        },
        content = content,
    )
}

@Composable
private fun BasicTooltip(
    modifier: Modifier = Modifier,
    elevation: Dp = BasicTooltipDefault.elevation,
    tooltip: BasicTooltipUio,
) {
    CompositionLocalProvider(
        LocalLayoutDirection provides LayoutDirection.Ltr,
    ) {
        Surface(
            modifier = Modifier
                .padding(16.dp)
                .then(other = modifier),
            color = MaterialTheme.colors.surface,
            elevation = elevation,
            shape = remember { RoundedCornerShape(4.dp) }
        ) {
            Column(
                modifier = Modifier.padding(16.dp),
                verticalArrangement = Arrangement.spacedBy(space = 4.dp),
            ) {
                tooltip.title?.let {
                    Text(
                        style = MaterialTheme.typography.body2,
                        fontWeight = FontWeight.Bold,
                        text = it
                    )
                }
                Text(
                    style = MaterialTheme.typography.caption,
                    text = tooltip.description
                )
            }
        }
    }
}