-- Advanced  Tools for Mission Editor
-- Rescue : Sunski34 - V1.0.0
-- 30/12/16

local thisModule


local displayLabels = {
	FR = {
		"Transport de troupes",
		"Embarquer un groupe",
		"Faire débarquer %s",
		"Vous n'êtes pas posé !",
		"Vous êtes trop loin du groupe à secourir !",
		"Groupe %s embarqué !",
		"Groupe %s débarqué sans pertes !",
		"Groupe %s débarqué avec une perte !",
		"Groupe %s débarqué avec %d pertes !",
		"Il reste %d unités à bord.",
		"Il ne reste aucune unité à bord.",
		"Vous ne pouvez pas débarquer d'unité ici, trop de présence d'eau !",
		"Tout le groupe s'est noyé.",
		"Aucun groupe à récupérer.",
		"Vous n'avez pas assez de place pour embarquer tout le groupe !"
	},
	
	EN = {
		"Personnel carrier",
		"Load group",
		"Unload %s",
		"You're in air !",
		"You're too far from troops to rescue !",
		"Group %s on board !",
		"Group %s disembarked without losses !",
		"Group %s disembarked with losses !",
		"Group %s disembarked with losses !",
		"There's %d units on board.",
		"No more unit on board.",
		"You can't disembark units here. Too much water !",
		"All group drowned.",
		"No group to rescue.",
		"You can't embark all units !"
	},
}



local debark
debark = function(args)		
	local player = args[1]
	local index = args[2]

	if player:inAir() == true then
		player:display(displayLabels[ATME.getLanguage()][4], 5)
	else
		local err, newGroup, nbDeadUnits = player:unload(index)

		if err == false then
			player.modules["Rescue"].F10SubMenu:remove(index + 1)
			
			if nbDeadUnits == 0 then
				player:display(string.format(displayLabels[ATME.getLanguage()][7], newGroup:getName()), 5)
			elseif nbDeadUnits == 1 then
				player:display(string.format(displayLabels[ATME.getLanguage()][8], newGroup:getName()), 5)
			else
				player:display(string.format(displayLabels[ATME.getLanguage()][9], newGroup:getName(), nbDeadUnits), 5)
			end
			
			if player:getNbUnitsOnBoard() > 0 then
				player:display(string.format(displayLabels[ATME.getLanguage()][10], player:getNbUnitsOnBoard()), 5)
			else
				player:display(displayLabels[ATME.getLanguage()][11], 5)
			end
		else
			if newGroup == "PLAYER_UNLOAD_ONLY_WATER" then
				player:display(displayLabels[ATME.getLanguage()][12], 5)
			elseif newGroup == "PLAYER_UNLOAD_ALL_GROUP_DEAD" then
				player:display(displayLabels[ATME.getLanguage()][13], 5)
			end
		end
	end
end


local embark
embark = function(args)
	local player = args[1]
	
	local group = player:getNearestReadyToBoardGroups(200)

	local err, var = player:load(group, 200)
	
	if err == true then
		if var == "EMBARK_NO_GROUP_AVAILABLE" then
			player:display(displayLabels[ATME.getLanguage()][14], 5)
		elseif var == "EMBARK_UNIT_IN_AIR" then
			player:display(displayLabels[ATME.getLanguage()][4], 5)
		elseif var == "EMBARK_TOO_MUCH_UNITS" then
			player:display(displayLabels[ATME.getLanguage()][15], 5)
		end
	end
end

local function onCreatePlayer(_player)
	if _player:isHelicopter() == true and _player:isPersonnelCarrier() == true then
		_player.modules["Rescue"].F10SubMenu = ATME.C_F10Menu(_player, displayLabels[ATME.getLanguage()][1], nil)
		_player.modules["Rescue"].found = false
	end
end

local function onUpdatePlayer(_player, events)

	if _player:isPersonnelCarrier() == true then
		local group = _player:getNearestReadyToBoardGroups(200)
		if _player:inAir() == false and group ~= nil and _player.modules["Rescue"].found == false then
			_player.modules["Rescue"].F10SubMenu:append(1, displayLabels[ATME.getLanguage()][2], embark , {_player})
			_player.modules["Rescue"].found = true
		elseif _player.modules["Rescue"].found == true and (_player:inAir() == true or group == nil) then
			_player.modules["Rescue"].F10SubMenu:remove(1)
			_player.modules["Rescue"].found = false
		end
	end
end

local function onTimer(events)
	for _id, _ in events:pairs() do
		if events:isCoreEvent(_id) == true then
			thisModule:output(events:getCoreEventType(_id), 1)
			if events:getCoreEventType(_id) == "TRANSPORT_END_OF_BOARDING" then
				local datas = events:getCoreEventDatas(_id)
				if ATME.isObjectClass(datas.unit, ATME.C_Player) == true then
					-- datas.unit est le player en l'occurence
					datas.unit:display(string.format(displayLabels[ATME.getLanguage()][6], datas.onBoardGroupName), 5)
					local itemMenuText = string.format(displayLabels[ATME.getLanguage()][3], datas.onBoardGroupName)
					datas.unit.modules["Rescue"].F10SubMenu:append(datas.index + 1, itemMenuText, debark , {datas.unit, datas.index})
				end
			end
		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("Rescue", newHandlers, false)
end