raise DeprecationWarning
from command import Scene
import skia
import struct
from PIL import Image
import numpy as np

# 暂时先照抄小帕的了，之后有空再单独封装一下


async def render(cmd: str, width: int, height: int):
    scene = Scene(cmd)
    surface = skia.Surface(width, height)
    sksl_code = scene.compile()

    runtime_effect = skia.RuntimeEffect.MakeForShader(scene.compile())
    if runtime_effect is None:
        raise Exception("cannot compile sksl shader")

    uv_uniform = struct.pack("ff", float(width), float(height))
    uniform_data = skia.Data.MakeWithCopy(uv_uniform)
    shader = runtime_effect.makeShader(uniform_data, None, 0)
    canvas = surface.getCanvas()
    canvas.clear(skia.Color(0, 0, 0, 0))
    paint = skia.Paint()
    paint.setShader(shader)
    canvas.drawRect(skia.Rect.MakeWH(width, height), paint)
    image = surface.makeImageSnapshot()
    target_info = skia.ImageInfo.Make(
        image.width(),
        image.height(),
        skia.ColorType.kBGRA_8888_ColorType,
        skia.AlphaType.kPremul_AlphaType,
    )
    pixel_data = bytearray(
        image.width() * image.height() * 4
    )  # 4 bytes per pixel (BGRA)
    success = image.readPixels(target_info, pixel_data, target_info.minRowBytes())
    img_array = np.frombuffer(pixel_data, dtype=np.uint8).reshape((height, width, 4))
    rgb_array = img_array[:, :, [2, 1, 0]]
    pil_img = Image.fromarray(rgb_array)
    return pil_img
