local thisModule
local moduleName = "Module2"

local unitIsDestroyed = false

local function explodeUnit()
    local unit = ATME.C_AIUnit.getByName("Unit to explode")
    
    if unit ~= nil then
        unit:explode(100)
        unitIsDestroyed = true
    end
end

local F10ExplodeCommand
F10ExplodeCommand = function()      
    thisModule:removeUserTrigger("TEST1")
    explodeUnit()
    ATME.displayForAll("Unité détruite", 5)
end

local function onCreatePlayer(player)
    player:display("Welcome in " .. moduleName, 10)
    
    if unitIsDestroyed == false then
        local menu = player:getF10MenuRoot()
        menu:append(1, "Explode unit", F10ExplodeCommand , nil)
        player.modules["Module2"].menuOK = true
        if thisModule:userTriggerExists("TEST1") == false then
            thisModule:createAbsoluteUserTrigger("TEST1", "00:00:50")
            thisModule:createAbsoluteUserTrigger("TEST2", "00:00:20 +10")
        end
    else
        player.modules["Module2"].menuOK = false
    end
end

local function onUpdatePlayer(player, events)
    if unitIsDestroyed == true and player.modules["Module2"].menuOK == true then
        local menu = player:getF10MenuRoot()
        menu:remove(1)
        player.modules["Module2"].menuOK = false
    end
end

local function onTimer(events)
    if events:isUserTriggerEvent("TEST1") == true then
        explodeUnit()
    elseif events:isUserTriggerEvent("TEST2") == true then
        local unit = ATME.C_AIUnit.getByName("Unit to explode")
    
        if unit ~= nil then
            unit:fireFlare("RANDOM")
        end
    end
end


-- MAIN
do
    local newHandlers = {
        onCreatePlayerHandler = onCreatePlayer,
        onDeletePlayerHandler = nil,
        onUpdatePlayerHandler = onUpdatePlayer,
        onTakeoffPlayerHandler = nil,
        onLandingPlayerHandler = nil,
        onStartEnginePlayerHandler = nil,
        onStopEnginePlayerHandler = nil,
        
        onCreateAIUnitHandler = nil,
        onDeleteAIUnitHandler = nil,
        onDisableAIUnitHandler = nil,
        onTakeoffAIUnitHandler = nil,
        onLandingAIUnitHandler = nil,
        onStartEngineAIUnitHandler = nil,
        onStopEngineAIUnitHandler = nil,
        
        onCreateGroupHandler = nil,
        onDeleteGroupHandler = nil,
        onDisableGroupHandler = nil,
        
        onCreateStaticObjectHandler = nil,
        onDeleteStaticObjectHandler = nil,
    
        onTimerHandler = onTimer,
        onModuleStartHandler = nil,
    }
        
    thisModule = ATME.C_Module(moduleName, newHandlers, true)
end
