package com.bomostory.sceneeditmodule

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import com.bomostory.sceneeditmodule.basicdata.Scene
import kotlin.math.pow

object SceneDrawer {
    fun drawScene(scene: Scene, trackX: Int, scaleWidth: Int, scaleHeight: Int): Bitmap? {
        var sceneBitmap: Bitmap? = null

        scene?.apply {
            sceneBitmap = BitmapFactory.decodeFile(backgroundPath)
            sceneBitmap = Bitmap.createScaledBitmap(sceneBitmap, scaleWidth, scaleHeight, true)

            val canvas = Canvas(sceneBitmap)
            for (layer in layers) {
                for (actor in layer.actors) {
                    var actorBitmap = BitmapFactory.decodeFile(actor.resourcePath)
                    actorBitmap = Bitmap.createScaledBitmap(actorBitmap, actor.sideLength, actor.sideLength, true)

                    canvas.save()
                    canvas.translate(trackX / 2f.pow(layers.indexOf(layer)), 0f)
                    canvas.drawBitmap(actorBitmap, actor.positionX.toFloat(), actor.positionY.toFloat(), Paint())
                    canvas.restore()

                    actorBitmap.recycle()
                }
            }
        }
        return sceneBitmap
    }
}