package ui

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material3.OutlinedCard
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.drawText
import androidx.compose.ui.text.rememberTextMeasurer
import domain.Animals
import domain.Item
import domain.ItemClass
import domain.ItemClassCompanion
import org.jetbrains.compose.ui.tooling.preview.Preview
import ui.theme.emojiFontFamily
import kotlin.math.min

@Composable
fun <C : ItemClass<C>> Selector(
    modifier: Modifier = Modifier,
    category: ItemClassCompanion<C>,
    options: List<Item<C>>,
    selectedItem: Item<C>?,
    onSelectItem: (Item<C>?) -> Unit
) {
    if (selectedItem != null) {
        DrawItem(item = selectedItem, modifier = modifier.clickable { onSelectItem(null) })
    } else {
        OutlinedCard(modifier = modifier.aspectRatio(1f)) {
            LazyVerticalGrid(
                columns = GridCells.Fixed(3),
                verticalArrangement = Arrangement.Center,
                horizontalArrangement = Arrangement.Center
            ) {
                for (option in options) {
                    item {
                        DrawItem(
                            item = option,
                            modifier = Modifier.clickable { onSelectItem(option) }
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun <C : ItemClass<C>> DrawItem(
    modifier: Modifier = Modifier,
    item: Item<C>
) {
    OutlinedCard(modifier = modifier.aspectRatio(1f)) {
        val emoji = item.symbol

        val textMeasurer = rememberTextMeasurer()

        Canvas(
            modifier = Modifier.fillMaxSize(1f),
            onDraw = {
                val textSize = textMeasurer.measure(text = emoji)
                val minTextSizeDimension = min(
                    textSize.size.width,
                    textSize.size.height
                )
                val fontSizeBase = minTextSizeDimension * min(
                    size.width / textSize.size.width,
                    size.height / textSize.size.height
                )
                val fontSize = (0.9f * fontSizeBase).toDp().toSp()

                val offset = Offset(
                    x = size.width / 2 - textSize.size.width * fontSizeBase / (2f * minTextSizeDimension),
                    y = size.height / 2 - textSize.size.height * fontSizeBase / (2f * minTextSizeDimension)
                )
                drawText(
                    textMeasurer = textMeasurer,
                    text = emoji,
                    style = TextStyle(fontSize = fontSize, fontFamily = emojiFontFamily ?: TextStyle.Default.fontFamily),
                    topLeft = offset
                )
            }
        )
    }
}
