package com.acitelight

import org.jetbrains.skia.Bitmap
import org.jetbrains.skia.BlendMode
import org.jetbrains.skia.Canvas
import org.jetbrains.skia.Color
import org.jetbrains.skia.EncodedImageFormat
import org.jetbrains.skia.FilterQuality
import org.jetbrains.skia.Font
import org.jetbrains.skia.Image
import org.jetbrains.skia.Paint
import org.jetbrains.skia.Rect
import org.jetbrains.skia.Surface
import org.jetbrains.skiko.SkiaLayer
import org.jetbrains.skiko.SkiaLayerRenderDelegate
import org.jetbrains.skiko.SkikoRenderDelegate

import java.awt.Dimension
import java.awt.Rectangle
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import javax.swing.JFrame
import javax.swing.SwingUtilities
import javax.swing.WindowConstants

val arch = java.io.File("/home/acite/IdeaProjects/skacite/res/logo.png").readBytes()
val arch_image = Image.makeFromEncoded(arch)

val waao = java.io.File("/home/acite/IdeaProjects/skacite/res/waao.png").readBytes()
val waao_image = Image.makeFromEncoded(waao)

fun renderer(canvas: Canvas, width: Int, height: Int, progress: Float)
{

    canvas.clear(Color.TRANSPARENT)

    canvas.drawImageRect(
        arch_image,
        Rect(width / 2f - 325f, height / 2f - 35 - 112f, width / 2f + 325f, height / 2f - 35f)
    )

    canvas.drawRect(
        Rect(width / 2f - 325f, height / 2f + 35f, width / 2f + 325f, height / 2f + 35f + 5f),
        Paint().apply {
            color = Color.makeRGB(50, 50, 50)
        })
    canvas.drawRect(
        Rect(width / 2f - 325f, height / 2f + 35f, (width / 2f - 325f) + (650f * progress), height / 2f + 35f + 5f),
        Paint().apply {
        color = Color.makeRGB(50, 100, 250)
    })

    var fin = 1.0f;
    if (progress > 0.9f) {
        fin = (1.0f - progress) * 10f
    }
    val waao_size = 1200f * (1 - fin)

    var alpha = 1f;
    if( fin < 0.2f)
    {
        alpha -= ((1 - fin) - 0.8f) * 5f
    }

    canvas.drawImageRect(
        waao_image,
        Rect.makeXYWH(width / 2f - waao_size / 2f, height / 2f - waao_size / 2f, waao_size, waao_size),
        Paint().apply {
            color = Color.makeARGB((255 * alpha).toInt(), 50, 100, 250)
        }
    )

}

fun saveBitmapToPNG(bitmap: Bitmap, path: String) {
    val image = Image.makeFromBitmap(bitmap)

    val pngData = image.encodeToData(EncodedImageFormat.PNG)
        ?: throw IllegalStateException("Failed to encode bitmap as PNG")

    Files.write(File(path).toPath(), pngData.bytes)
}

fun genFrames()
{
    val width = 1920
    val height = 1080
    val outputDir = File("./pics")
    if (!outputDir.exists()) outputDir.mkdirs()

    val surface = Surface.makeRasterN32Premul(width, height)

    val totalFrames = 160
    for (i in 0 until totalFrames) {
        val canvas = surface.canvas
        val progress = i.toFloat() / (totalFrames - 1)

        renderer(canvas, width, height, progress)

        val snapshot = surface.makeImageSnapshot()
        val bmp = Bitmap()
        bmp.allocPixels(snapshot.imageInfo)
        snapshot.readPixels(bmp)

        val filename = if (i < 80) {
            "progress-%02d.png".format(i)
        } else {
            "animation-%02d.png".format(i - 80)
        }

        val path = File(outputDir, filename).absolutePath
        saveBitmapToPNG(bmp, path)
        println("Saved: $filename")

        // 清空画布避免上一帧残留
        canvas.clear(Color.TRANSPARENT)
    }

    println("✅ All frames saved to ${outputDir.absolutePath}")
}

fun main() {
    genFrames()
    val skiaLayer = SkiaLayer()
    skiaLayer.renderDelegate = SkiaLayerRenderDelegate(skiaLayer, object : SkikoRenderDelegate {
        override fun onRender(canvas: Canvas, width: Int, height: Int, nanoTime: Long) {
            val ts = nanoTime / 1_000_000
            renderer(canvas, width, height, (ts % 5000) / 5000.0f)
        }
    })

    SwingUtilities.invokeLater {
        val window = JFrame("Skiko Plymouth").apply {
            defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
            preferredSize = Dimension(1920, 1080)
        }
        skiaLayer.attachTo(window.contentPane)
        skiaLayer.needRedraw()
        window.pack()
        window.isVisible = true
    }
}