trigger_event = {
    list = {},
}

trigger_ev = {}

function trigger_event.new ( _name, _tr_func, _tr_init, _tr_update, _a, _b, _time, _text, _distancia, _hab, _collision_req )
    table.insert( trigger_event.list, { 
        name    = _name,
        trigger = _tr_func,
        init    = _tr_init,
        update  = _tr_update,
        actor   = _a,
        actor_b = _b,
        time    = _time,
        text    = _text,
        hab     = _hab,
        distancia = _distancia,
        enabled = true,
        collision_req = _collision_req,
        running = false,
    })
end

function trigger_event.update ()
    for _,t in ipairs(trigger_event.list) do
        if t.enabled and not t.running then
            t.running = t:trigger()
        end
    end
end

function trigger_event.disable(_name)
print("Disabling ".._name)
    for _,t in ipairs(trigger_event.list) do
        if t.name == _name then
            print("Disabled trigger ".._name)
            t.running = false
            t.enabled = false
        end
    end
end

-- PREMIERE HEALER - BOSS 1 TRIGGER
function trigger_ev:premiere_boss1_trigger( )
    if stages.boss_loaded and
       abad.objects.clau_premiere~=nil and
       abad.energia<=abad.max_energia/2
    then
        self:init()
        return true
    end
    return false
end

function trigger_ev:premiere_healer_init()
    -- if premiere.update==premiere_update_healer then return true end
    -- Guardar l'estat de Premiere
    premiere.from_hab = premiere.hab 
    premiere.from_x = premiere.x
    premiere.from_y = premiere.y
    premiere.from_update = premiere.update
    -- Moure a premiere al Boss stage
    premiere.hab = 54
    premiere.x = 2
    premiere.y = 3
    local world_x, world_y = coords.room_to_world(premiere.hab,premiere.x,premiere.y)
    premiere.x=world_x
    premiere.y=world_y
    premiere.health_wait = 100
    premiere.update = self.update
    -- El trigger el deshabilitaria al acabar el update
    -- pero en estos moments no se com fer-ho
    -- perque l'update s'executa dins de premiere i no
    -- dins del trigger
    trigger_event.disable(self.name)
end

-- function trigger_ev:premiere_healer_update()
--      premiere.wait=premiere.wait+1
-- 
--     -- Esperar mentres dona la vida
--     if premiere.health_wait>0 and premiere.health_wait<100 then
--         premiere.health_wait = premiere.health_wait - 1
--     else
--         premiere.x = premiere.x+1
--         if premiere.wait>=6 then
--             premiere.wait = 0
--             premiere.step=(premiere.step+1)%4
--             premiere.frame=premiere.anim[premiere.step+1]
--         end
--     end
-- 
--     -- Lloc de pausa
--     local hab, tx, ty = coords.world_to_tile(premiere.x, premiere.y)
--     if hab == 55 and tx==0 and ty==3 and premiere.health_wait==100 then
--         -- 55 (0,3)
--         local _text = "Tenne! Io t'achudo. Figo!";
--         table.insert(
--             dialeg.list, 
--             { actor=premiere, 
--               text=_text,
--               time=200,
--               w = math.floor(4.6*#_text), 
--               h = 20,
--             }
--         )
--         premiere.health_wait = premiere.health_wait - 1
--         table.insert( actors, health_potion.new(
--             premiere.hab, 
--             premiere.x+premiere.bb.x+premiere.bb.w, 
--             premiere.y+premiere.bb.y+premiere.bb.h/3, 
--             1, 24, 3) 
--         )
--         -- health_potion.init(premiere.hab, 
--         --                    premiere.x+premiere.bb.x+premiere.bb.w,
--         --                    premiere.y+premiere.bb.y+premiere.bb.h/3,
--         --                    1, 24, 2)
--     elseif hab >= 56 then
--         -- Restaurar a Premiere
--         premiere.hab    = premiere.from_hab
--         premiere.x      = premiere.from_x
--         premiere.y      = premiere.from_y
--         premiere.update = premiere.from_update
--     end
-- end

-- DIALEG GENERAL
function trigger_ev:dialeg_trigger ()
    local in_hab = false
    if (self.hab~=nil and self.hab==self.actor.hab) or self.hab==nil then in_hab=true end
    
    local is_collision = true
    if self.collision_req then
        is_collision = h_collision( self.actor, self.actor_b )
    end
    
    if in_hab and is_collision and distancia ( self.actor, self.actor_b )<=self.distancia then
        self:init()
        return true
    end
    return false
end

function trigger_ev:dialeg_init()
    table.insert(
        dialeg.list, 
        { actor=self.actor, 
          text=self.text,
          time=self.time,
          w = math.floor(4.6*#self.text), 
          h = 20,
        }
    )
    trigger_event.disable(self.name)
end
