-- Advanced  Tools for Mission Editor
local thisModule
local moduleName = "Module4"

local nbHelicoptersInZone = 0
local noMoreHelicopterInZone = false

local displayLabels = {
    FR = {
        "Vous entrez dans la zone",
        "Vous sortez de la zone",
        "Le groupe d'infanterie a été embarqué",
        "Vous vous etes posé trop loin",
    },
    
    EN = {
        "You enter in zone",
        "You exit the zone",
        "Infantry group is on board",
        "You land too far",
    },
}

local function onTimer(events)
    local group = ATME.C_Group.getByName("rescue1")

    for _id, _ in events:pairs() do
        if events:isCoreEvent(_id) == true then
            if events:getCoreEventType(_id) == "SIGNAL_UNIT_IN_ZONE" then
                local datas = events:getCoreEventDatas(_id)
                datas.unit:display(displayLabels[ATME.getLanguage()][1], 5)
                nbHelicoptersInZone = nbHelicoptersInZone + 1
            elseif events:getCoreEventType(_id) == "SIGNAL_UNIT_OUT_OF_ZONE" then
                local datas = events:getCoreEventDatas(_id)
                datas.unit:display(displayLabels[ATME.getLanguage()][2], 5)
                nbHelicoptersInZone = nbHelicoptersInZone - 1
                if nbHelicoptersInZone == 0 and group ~= nil then 
                    noMoreHelicopterInZone = true
                end
            elseif events:getCoreEventType(_id) == "TRANSPORT_END_OF_BOARDING" then
                ATME.displayForAll(displayLabels[ATME.getLanguage()][3],5)
            end
        end
    end
    
    if noMoreHelicopterInZone == true then
        local unit = ATME.C_Group.getByName("My Vehicle"):getFirstUnit()
        unit:load(group, 200)
    end
end

local function onLanding(player)
    local group = ATME.C_Group.getByName("rescue1")

    if group ~= nil then
        local err, errName = player:load(group, 500)
        
 if err == true then
            if errName == "LOAD_UNIT_TOO_FAR" then
                player:display(displayLabels[ATME.getLanguage()][4], 5)
            else
                player:display("Erreur ... " .. errName, 5)
            end
        end
    end
end

local function onCreateGroup(_group)
    if _group:getName() == "My Vehicle" then
        _group:setSignal(2500, "SIGNAL_SMOKE GREEN")
    elseif _group:getName() == "rescue1" then
        _group:changeReadyToBoard(true)
    end
end



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