--[[

Main program function for Enhanced Flight Map.

Localisation of text should be done in localization.lua.

All other code in here should not be modified unless you know what you are doing,
and if you do modify something, please let me know.

]]

---------------------------------------------------------------------------
-- Functions to deal with the various methods the program can be called
---------------------------------------------------------------------------

-- Events to register for.
function EnhancedFlightMap_OnLoad()
	this:RegisterEvent("ADDON_LOADED");
	this:RegisterEvent("WORLD_MAP_UPDATE");
	this:RegisterEvent("TAXIMAP_OPENED");
	this:RegisterEvent("PLAYER_ENTERING_WORLD");
	this:RegisterEvent("PLAYER_LEAVING_WORLD");
end

-- What to do when events are seen.
function EnhancedFlightMap_OnEvent()
	if ((event == "ADDON_LOADED") and (arg1 == "EnhancedFlightMap")) then
		-- Register our slash commands
		SLASH_EFM1 = EFM_SLASH1;
		SLASH_EFM2 = EFM_SLASH2;
		SlashCmdList["EFM"] = function(msg)
			EFM_SlashCommandHandler(msg);
		end

		-- Define program Hooks
		-- Hook DrawOneHopLines function.
		-- We do this to find the direct flight paths from each node.
		if (DrawOneHopLines ~= EFM_FM_DrawOneHopLines) then
			EFM_Orig_DrawOneHopLines	= DrawOneHopLines;
			DrawOneHopLines			= EFM_FM_DrawOneHopLines;
		end

		-- Hook TaxiNodeOnButtonEnter function.
		-- We do this to be able to display the additional data for the blizzard-defined nodes as well as our nodes.
		if (TaxiNodeOnButtonEnter ~= EFM_FM_TaxiNodeOnButtonEnter) then
			EFM_OriginalTaxiNodeOnButtonEnter	= TaxiNodeOnButtonEnter;
			TaxiNodeOnButtonEnter				= EFM_FM_TaxiNodeOnButtonEnter;
		end

		-- Hook the TakeTaxiNode function.
		-- This is done as there is no way (currently) to determine what node we are flying to while in flight.
		if (TakeTaxiNode ~= EFM_Timer_TakeTaxiNode) then
			Original_TakeTaxiNode	= TakeTaxiNode;
			TakeTaxiNode		= EFM_Timer_TakeTaxiNode;
		end

		-- Display login message
		if (EFM_SF_VERS_NUM) then
			EFM_SF_ProgramLoadedText(EFM_Version_String);
		end

		-- Call various init routines.
		EFM_DefineData();
		EFM_ValidateVersions();
		EFM_GUI_Init();

		-- Get CastingBarPosition
		EFM_Timer_GetCastingBarPosition();
		return;

	elseif (event == "PLAYER_ENTERING_WORLD") then
		EFM_Timer_GetCastingBarPosition();
		EFM_ENABLE = true;
		return;

	elseif (event == "PLAYER_LEAVING_WORLD") then
		EFM_Timer_StartRecording	= false;
		EFM_Timer_Recording		= false;
		EFM_ENABLE			= false;
		return;

	elseif (EFM_ENABLE) then
		if (event == "WORLD_MAP_UPDATE") then
			EFM_Map_WorldMapEvent();
			return;

		elseif (event == "TAXIMAP_OPENED") then
			EFM_FM_TaxiMapOpenEvent();
			return;

		end
	end
end
