local task       = require "lib.utils.task"

--- @class LightBehavior : Behavior
--- @field intensity number
--- @field color Vec3
--- @field seed integer
--- @field private animateColorTask? Task
local behavior   = {}
behavior.__index = behavior
behavior.id      = "light"

---@param values {intensity: number?, color: Vec3?, seed: integer?}
---@return LightBehavior
function behavior.new(values)
    return setmetatable({
        intensity = values.intensity or 1,
        color = values.color or Vec3 { 1, 1, 1 },
        seed = values.seed or math.random(math.pow(2, 16))
    }, behavior)
end

function behavior:update(dt)
    -- All logic moved to tasks
end

function behavior:animateColor(targetColor, duration, easing)
    -- If there's support for canceling tasks, we should do it here
    return task.tween(self, { color = targetColor }, duration or 800, easing)
end

function behavior:draw()
    local positioned = self.owner:has(Tree.behaviors.positioned)
    if not positioned then return end

    Tree.level.render:enqueue(Tree.level.render.LAYERS.LIGHT, positioned.position.y, function()
        love.graphics.setBlendMode("add", "premultiplied")
        local shader = Tree.assets.files.shaders.light
        shader:send("color", { self.color.x, self.color.y, self.color.z })
        shader:send("time", love.timer:getTime() + self.seed)
        love.graphics.setShader(shader)
        love.graphics.draw(Tree.assets.files.masks.circle128, positioned.position.x - self.intensity / 2,
            positioned.position.y - self.intensity / 2, 0, self.intensity / 128,
            self.intensity / 128)

        love.graphics.setShader()
        love.graphics.setBlendMode("alpha")
    end)
end

return behavior
