-- Initialisation

local thisModule



local function onCreateUnit(unit)
	if unit:getName() == "Unité #1" then
		if thisModule:userTriggerExists("TEST1") == false then
			thisModule:createAbsoluteUserTrigger("TEST1", "25")
		end
		if thisModule:userTriggerExists("TEST2") == false then
			thisModule:createFlagRelativeUserTrigger("TEST2", 2, "10")
		end
		
		--thisModule:output("Transmit activation",1)

		unit:startTransmission("fifteen.ogg", "AM", 251000000,5,3)
		unit.modules["ModDefault"].radio = ATME.loopTransmission("squelsh.ogg", unit:getPosition(), "AM", 264000000,5)
		
		thisModule:createAbsoluteUserTrigger("TEST6", "+00:00:15")	
	end
end



local function onCreatePlayer(player)
	player.modules["ModDefault"].antenne = ATME.C_StaticObject.getByName("Antenne"):getPosition()

	player:soundStart("thousand.ogg", 5)
	
	thisModule:createAbsoluteUserTrigger("TEST5", "+00:01:00")
end

local function onUpdatePlayer(player, events)

	if player:isRouteInDirection(player.modules["ModDefault"].antenne) == true then
		player:display("Aligné vers l'antenne",5)
	end


	if events:isUserTriggerEvent("TEST5") == true then
		thisModule:output("Trigger TEST5 a été activé",1)
		player:soundChange("test.ogg", 5, true)
		
		thisModule:createAbsoluteUserTrigger("TEST7", "+00:00:20")
	end
	
	if events:isUserTriggerEvent("TEST7") == true then
		player:soundStop()
	end
end

local function onTimer(events)

	for _name, _ in events:pairs() do
		if events:isUserTriggerEvent(_name) == true then
			local unit1 = ATME.C_AIUnit.getByName("Unité #1")
			local unit2 = ATME.C_AIUnit.getByName("Unité #2")
			local unit3 = ATME.C_AIUnit.getByName("Unité #001")

			
			if _name == "TEST1" then
				ATME.setFlag(2,1)
				ATME.stopTransmission(unit1.modules["ModDefault"].radio)
			end
			if  _name == "TEST2" then
				unit1:explode(100)
				local plane = ATME.C_StaticObject.getByName("B52")
				if plane ~= nil then
					plane:explode(100)
				end
				thisModule:createAbsoluteUserTrigger("TEST3", "+00:00:30 +10")
				thisModule:createAbsoluteUserTrigger("TEST4", "+00:00:20")
			end
			if _name == "TEST6" then
				unit1:stopAllTransmissions()
			end

			
			if _name == "TEST3" then
				--local pos = unit:getPosition()
				--local flare = ATME.C_Flare("RANDOM", pos)
				
				if events:isUserTriggerEventToggle("TEST3") == true then
					thisModule:output("Trigger " .. _name .. " passage à l'état activé",1)
				else
					thisModule:output("Trigger " .. _name .. " activé sans changement d'état",1)
				end
				
				unit2:fireFlare("RANDOM")
				
				--thisModule:createAbsoluteTrigger("TEST4", "+00:00:30")
			end

			
			if _name == "TEST4" then
				local pos = unit3:getPosition()
				local mySmoke = ATME.C_Smoke("BLUE", pos)
				mySmoke:activate(false)
			end
		end
	end
end


-- MAIN
do
	local newHandlers = {
		onCreatePlayerHandler = onCreatePlayer,
		onDeletePlayerHandler = nil,
		onUpdatePlayerHandler = onUpdatePlayer,
		onTakeoffPlayerHandler = nil,
		onLandingPlayerHandler = nil,
		onStartEnginePlayerHandler = nil,
		onStopEnginePlayerHandler = nil,
		
		onCreateAIUnitHandler = onCreateUnit,
		onDeleteAIUnitHandler = 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("ModDefault", newHandlers, true)
end