-- Advanced  Tools for Mission Editor
local thisModule


local function onTimer(events)
	for _id, _ in events:pairs() do
		if events:isUserTriggerEvent(_id) then
			if _id == "TEST1" then
				local unit = ATME.C_AIUnit.getByName("Unité #028")
				if unit ~= nil then
					local group = unit:getNearestReadyToBoardGroups(500)
					if group ~= nil then
						unit:load(group, 200)
						thisModule:createAbsoluteUserTrigger("TEST2", "+60")
					end
				end
			elseif _id == "TEST2" then
				local unit = ATME.C_AIUnit.getByName("Unité #028")
				if unit ~= nil then
					local groupsNameOnBoard = unit:getGroupsNameOnBoard()
					
					if #groupsNameOnBoard > 0 then
						local err, newGroup, nbDeadUnits = unit:unload(1)
					end
				end
			end
		elseif events:isCoreEvent(_id) == true then
			thisModule:output(events:getCoreEventType(_id), 1)
			if events:getCoreEventType(_id) == "SIGNAL_UNIT_IN_ZONE" then
				local datas = events:getCoreEventDatas(_id)
				datas.linkedGroup:changeReadyToBoard(true)
			elseif events:getCoreEventType(_id) == "TRANSPORT_END_OF_DISEMBARK" then
				local datas = events:getCoreEventDatas(_id)
				thisModule:output("Le groupe " .. datas.linkedGroup:getName() .. " a été débarqué (avec " .. datas.nbDeadUnits .. " pertes)",1)
			end
		end
	end
end

local function onCreateGroup(_group)
	if _group:getName() == "rescue1" or _group:getName() == "rescue2" then
		_group:setPickupZone("myZone", 0.7)
	elseif _group:getName() == "rescue3" or _group:getName() == "rescue4" then
		_group:setPickupZone("ZonePickup1", 1)
		_group:setSignal(3000, "SIGNAL_SMOKE RED")
	elseif _group:getName() == "Vehicule" then
		_group:setSignal(2500, "SIGNAL_SMOKE GREEN")
	end
end

local function onStart(areUnitsAndPlayersInitialized)
	if areUnitsAndPlayersInitialized == true then
		local err, datasG = ATME.C_GroupSpawnDatas.duplicateFromMissionDatas("rescue0", "rescue2")
		
		if err == false then
			local group = ATME.C_Group.getByName("rescue0")
			local unit = group:getFirstUnit()
			local gpPos = unit:getPosition()
			datasG:spawn(gpPos, "rescue1",2)
		end
		
		err, datasG = ATME.C_GroupSpawnDatas.duplicateFromMissionDatas("origine", "nouveauZone")
		
		if err == false then
			datasG:spawn("spawnZone")
		end
		
		thisModule:createAbsoluteUserTrigger("TEST1", "50")
	end
end

-- MAIN
do
	local newHandlers = {
		onCreatePlayerHandler = nil,
		onDeletePlayerHandler = nil,
		onUpdatePlayerHandler = nil,
		onTakeoffPlayerHandler = nil,
		onLandingPlayerHandler = nil,
		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 = onStart,
	}
		
	thisModule = ATME.C_Module("TestRescue", newHandlers, true)
end