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

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.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.pixelized.desktop.lwa.ui.composable.scene.drawables.SceneLayer
import com.pixelized.desktop.lwa.ui.theme.lwa
import com.pixelized.desktop.lwa.ui.theme.typography.LwaTypography

@Composable
fun LayerDebugPanel(
    modifier: Modifier = Modifier,
    layer: SceneLayer,
    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.propertyId,
            color = MaterialTheme.lwa.colorScheme.base.primary,
            text = layer.name,
        )
        if (isOpen.value) {
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("id: ") }
                    append(layer.id)
                },
            )
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("offset: ") }
                    append(layer.offset.toString())
                },
            )
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("size: ") }
                    append(layer.size.toString())
                },
            )
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("alpha: ") }
                    append(layer.alpha.toString())
                },
            )
        }
    }
}