local LT = require("LOVELyTree") ---@module "LOVElyTree"

function CustomWaitAction(duration)
    local action = {}
    action.timer = 0
    action.name = "WaitAction (" .. duration .. "s)"
    
    function action:update(dt)
        self.timer = self.timer + dt
        return (self.timer >= duration) and LT.RESULT.SUCCESS or LT.RESULT.RUNNING
    end

    --- This method is optional.
    --- When it doesn't exist, the internal action will just call the function again
    --- To generate a new clean state. If you need a custom cleanup of the node or need more performance
    --- Just define this function. Returning false means that the object won't be recreated
    function action:reset()
        self.timer = 0
        
        return false
    end

    return action
end

local WaitActionWrapped = LT.WrapAction(CustomWaitAction)

local function simple_tree()
    local branch1 = LT.Compositors.Sequence({
        WaitActionWrapped(1.5),
        LT.Action(CustomWaitAction, 2),
        LT.Decorators.Invert(LT.Actions.Wait(2))
    })
    
    local branch2 = LT.Compositors.Sequence({
        LT.Compositors.RandomOnce({
            LT.Action(CustomWaitAction, 2),
            LT.Decorators.AlwaysFail(LT.Action(CustomWaitAction, 2))
        }),
    })

    local root = LT.Compositors.Selector({ branch1, branch2 })
    return LT.BehaviorTree(root)
end

return simple_tree