local task = require "lib.utils.task"
local effect = require "lib.spell.effect"
local easing = require "lib.utils.easing"

local bleeding = effect.new({
    tag = "bleeding",
    afterBirth = function(owner, intensity)
        local light = require "lib/character/character".spawn("Bleeding Light Effect")
        light:addBehavior {
            Tree.behaviors.light.new { color = Vec3 { 1, 0., 0. }, intensity = 4 },
            Tree.behaviors.positioned.new(owner:has(Tree.behaviors.positioned).position + Vec3 { 0.5, 0.5 }),
        }

        return task.wait({ task.chain(task.tween(light:has(Tree.behaviors.light) --[[@as LightBehavior]],
            { intensity = 1, color = Vec3 { 0, 0., 0. } }, 800, easing.easeInCubic), function()
            light:die()
            return task.fromValue()
        end) })
    end
})

function bleeding:beforeTurn(owner, intensity)
    local stats = owner:has(Tree.behaviors.stats)
    local sprite = owner:has(Tree.behaviors.sprite)
    if not stats or not sprite then return end
    stats:dealDamage(intensity)
    return task.wait({ sprite:animate("hurt") })
end

function bleeding:afterTurn(owner, intensity)
    local behavior = owner:has(Tree.behaviors.effects)
    if not behavior then
        print('[Effect]: yo man what the hell wheres your behavior how thats possible please stop thats not normal')
        return
    end
    behavior:deleteStacks(self, 1)
end

--- meow
function bleeding:afterCast(owner, intensity)
    Tree.audio:play(Tree.assets.files.audio.sounds.meow)
end

function bleeding:beforeCast(owner, intensity)
    Tree.audio:play(Tree.assets.files.audio.sounds.meow)
end

local effectbook = {
    bleeding = bleeding
}

--- не уверен зачем нам это, но вай нот ай саппоуз
--- @param list Effect[]
function effectbook.of(list)
    local efb = {}
    for i, ef in ipairs(list) do
        print(i)
        efb[i] = setmetatable({}, { __index = ef })
    end
    return efb
end

return effectbook
