package com.pixelized.desktop.lwa.ui.composable.scene.drawables

import androidx.compose.foundation.clickable
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.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.theme.lwa
import com.pixelized.desktop.lwa.ui.theme.typography.LwaTypography

@Stable
class SceneElement(
    id: String,
    name: String,
    texture: ImageBitmap,
    offset: IntOffset = IntOffset.Companion.Zero,
    size: IntSize = IntSize(texture.width, texture.height),
    alpha: Float = 1f,
) : SceneDrawable(
    id = id,
    name = name,
    texture = texture,
    offset = offset,
    size = size,
    initialAlpha = alpha,
) {
    val position = IntOffset(
        x = offset.x - size.width / 2,
        y = offset.y - size.height / 2,
    )
}

@Composable
fun SceneElementDebug(
    modifier: Modifier = Modifier,
    element: SceneElement,
    isOpen: Boolean = true,
    style: LwaTypography.Debug = MaterialTheme.lwa.typography.debug,
    padding: Dp = MaterialTheme.lwa.dimen.debug.offset,
) {
    val isOpen = remember { mutableStateOf(isOpen) }

    Column(
        modifier = Modifier
            .clickable { isOpen.value = isOpen.value.not() }
            .widthIn(min = MaterialTheme.lwa.dimen.debug.panelWidth)
            .then(other = modifier),
        verticalArrangement = Arrangement.spacedBy(space = 2.dp),
    ) {
        Text(
            style = MaterialTheme.lwa.typography.debug.title,
            color = MaterialTheme.lwa.colorScheme.base.primary,
            text = element.name,
        )
        if (isOpen.value) {
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("id: ") }
                    append(element.id)
                },
            )
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("offset: ") }
                    append(element.offset.toString())
                },
            )
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("size: ") }
                    append(element.size.toString())
                },
            )
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("alpha: ") }
                    append(element.alpha.toString())
                },
            )
        }
    }
}