--[[
	DynamicData

	By sarf

	This mod allows you to access dynamic data in WoW without being forced to rely on strange Blizzard functions

	Thanks goes to the Cosmos team, the nice (but strange) people at #cosmostesters and Blizzard.
	
	CosmosUI URL:
	http://www.cosmosui.org/forums/viewtopic.php?t=NOT_YET_ANNOUNCED
	
   ]]

function DynamicDataScriptFrame_OnLoad()
	-- item events
	this:RegisterEvent("BAG_UPDATE");
	this:RegisterEvent("BAG_UPDATE_COOLDOWN");
	this:RegisterEvent("ITEM_LOCK_CHANGED");
	this:RegisterEvent("UNIT_INVENTORY_CHANGED");
	this:RegisterEvent("UPDATE_INVENTORY_ALERTS");

	-- effect events
	this:RegisterEvent("UNIT_AURA");
	this:RegisterEvent("PLAYER_AURAS_CHANGED");
	this:RegisterEvent("UNIT_AURASTATE");
	this:RegisterEvent("PARTY_MEMBERS_CHANGED");
	this:RegisterEvent("PARTY_MEMBER_ENABLE");
	this:RegisterEvent("PARTY_MEMBER_DISABLE");
	this:RegisterEvent("PLAYER_PET_CHANGED");
	
	-- action events
	this:RegisterEvent("ACTIONBAR_SLOT_CHANGED");
	
	-- spell events
	this:RegisterEvent("SPELLS_CHANGED");
	this:RegisterEvent("LEARNED_SPELL_IN_TAB");

	-- variable events
	this:RegisterEvent("VARIABLES_LOADED");

	DynamicData.util.OnLoad();
	DynamicData.action.OnLoad();
	DynamicData.effect.OnLoad();
	DynamicData.item.OnLoad();
	DynamicData.spell.OnLoad();
end

function DynamicDataScriptFrame_OnEvent(event)
	-- item events
	if ( event == "BAG_UPDATE" ) then
		DynamicData.item.updateItems(arg1);
	end
	if ( event == "BAG_UPDATE_COOLDOWN" ) then
		DynamicData.item.updateItemCooldowns();
	end
	if ( event == "ITEM_LOCK_CHANGED" ) then
		DynamicData.item.updateItemLocks();
	end
	if ( event == "UNIT_INVENTORY_CHANGED" ) then
		if ( arg1 == "player" ) then
			DynamicData.item.updateItems(-1);
		end
	end
	if ( event == "UPDATE_INVENTORY_ALERTS" ) then
		DynamicData.item.updateItemAlerts();
	end
	-- effect events
	if ( event == "UNIT_AURA" ) then
		DynamicData.effect.initiateUpdateEffects(arg1);
		return;
	end
	if ( event == "PLAYER_AURAS_CHANGED" ) then
		DynamicData.effect.initiateUpdateEffects("player");
		return;
	end
	if ( event == "UNIT_AURASTATE" ) then
		DynamicData.effect.initiateUpdateEffects(arg1);
		return;
	end
	if ( event == "PARTY_MEMBER_ENABLE" or event == "PARTY_MEMBER_DISABLE" ) then
		local unit = arg1;
		if ( unit ) and ( not strfind(unit, "party") ) then
			unit = "party"..unit;
		end
		DynamicData.effect.initiateUpdateEffects(unit);
	end
	if ( event == "PARTY_MEMBERS_CHANGED" ) then
		local unit = "party";
		for i = 1, 4 do
			unit = "party"..i;
			DynamicData.effect.initiateUpdateEffects(unit);
		end
		return;
	end
	if ( event == "PLAYER_PET_CHANGED" ) then
		DynamicData.effect.initiateUpdateEffects("pet");
	end	
	-- target changed event
	if ( event == "PLAYER_TARGET_CHANGED" ) then
		DynamicData.effect.initiateUpdateEffects("target");
	end
	-- action events
	if ( event == "ACTIONBAR_SLOT_CHANGED" ) then
		if ( arg1 == -1 ) then
			DynamicData.action.updateActions();
		else
			DynamicData.action.updateActions(arg1);
		end
	end
	-- spell events
	if ( (event == "SPELLS_CHANGED") or ( event == "LEARNED_SPELL_IN_TAB") ) then
		DynamicData.spell.updateSpells();
		return;
	end
	-- variable events
	if ( event == "VARIABLES_LOADED" ) then
		DynamicData.util.variablesLoaded();
	end
end



function DynamicDataScriptFrame_OnUpdate(elapsed)
	DynamicData.util.notifyWhateverHandlers(DynamicData.util.onUpdateHandlers, elapsed);
end