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

import androidx.compose.animation.animateContentSize
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.Scene
import com.pixelized.desktop.lwa.ui.theme.lwa
import com.pixelized.desktop.lwa.ui.theme.typography.LwaTypography

@Composable
fun SceneDebugPanel(
    modifier: Modifier = Modifier,
    style: LwaTypography.Debug = MaterialTheme.lwa.typography.debug,
    padding: Dp = MaterialTheme.lwa.dimen.debug.offset,
    spacing: Dp = 2.dp,
    scene: Scene,
    isOpen: Boolean = true,
) {
    val isOpen = remember { mutableStateOf(isOpen) }

    Column(
        modifier = Modifier
            .clickable { isOpen.value = isOpen.value.not() }
            .widthIn(min = MaterialTheme.lwa.dimen.debug.panelWidth)
            .animateContentSize()
            .then(other = modifier),
        verticalArrangement = Arrangement.spacedBy(space = spacing),
    ) {
        Text(
            style = MaterialTheme.lwa.typography.debug.title,
            color = MaterialTheme.lwa.colorScheme.base.primary,
            text = "Scene",
        )
        if (isOpen.value) {
            Text(
                modifier = Modifier.padding(start = padding),
                style = MaterialTheme.lwa.typography.debug.propertyValue,
                text = buildAnnotatedString {
                    withStyle(style.propertyIdSpan) { append("Size: ") }
                    append(scene.size.toString())
                },
            )
            Column(
                modifier = Modifier.padding(start = padding),
            ) {
                Text(
                    style = MaterialTheme.lwa.typography.debug.propertyId,
                    text = "Layers:(${scene.layers.size})"
                )
                scene.layers.forEach { layer ->
                    LayerDebugPanel(
                        modifier = Modifier.padding(start = padding),
                        isOpen = false,
                        layer = layer,
                    )
                }
            }
            Column(
                modifier = Modifier.padding(start = padding),
            ) {
                Text(
                    style = MaterialTheme.lwa.typography.debug.propertyId,
                    text = "Elements:(${scene.elements.size})"
                )
                scene.elements.forEach { element ->
                    ElementDebugPanel(
                        modifier = Modifier.padding(start = padding),
                        isOpen = false,
                        element = element,
                    )
                }
            }
        }
    }
}