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

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.imageResource
import java.util.UUID

@Stable
class SceneElement(
    id: String,
    name: String,
    texture: ImageBitmap,
    offset: IntOffset = IntOffset.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
@Stable
fun rememberElementFromResource(
    name: String,
    resource: DrawableResource,
    offset: IntOffset = IntOffset.Zero,
): SceneElement {
    val texture = imageResource(
        resource = resource,
    )
    return remember(resource) {
        SceneElement(
            id = UUID.randomUUID().toString(),
            name = name,
            texture = texture,
            offset = offset,
            alpha = 1f,
        )
    }
}