--[[
	Events.lua
		This handles global events and the loading of Bagnon and Banknon
		
	If there's a bug with item slots not updating properly, its probably because I'm now using 
	MERCHANT_UPDATE since ITEM_LOCK_CHANGED covers nearly any other function.  
	My best guess is that there will be a potential failure on trading
--]]

function BagnonEvents_OnLoad()
	--Slash handler
	BagnonSlash_Load();
	
	--Rescaling detection, for whenever the UI scale changes
	Infield_AddRescaleAction(BagnonEvents_RescaleAll);
	
	--Events
	this:RegisterEvent("ADDON_LOADED");
end

--[[
	Event Handler
--]]

function BagnonEvents_OnEvent(event)

	if ( event == "BAG_UPDATE" or event == "ITEM_LOCK_CHANGED" or event == "BAG_UPDATE_COOLDOWN") then
	
		if( Bagnon and Bagnon:IsVisible() ) then
			BagnonCore_Update(Bagnon);		
		end
		
		if( Banknon and Banknon:IsVisible() ) then
			BagnonCore_Update(Banknon);
		end
		
	elseif( event == "BANKFRAME_OPENED") then
	
		if( IsAddOnEnabled("Bagnon") and not IsAddOnLoaded("Bagnon") ) then
			LoadAddOn("Bagnon");
		end
		
		if( IsAddOnEnabled("Banknon") ) then
			this:UnregisterEvent("BANKFRAME_OPENED");
			LoadAddOn("Banknon");
		else
			BagnonEvents_ShowBlizBank();
		end
		
	--the following are tweaks because I read that bag update and item lock changed fire off lots of times when zoning
	elseif(event == "PLAYER_ENTERING_WORLD") then
		
		this:RegisterEvent("BAG_UPDATE");
		this:RegisterEvent("ITEM_LOCK_CHANGED");
		this:RegisterEvent("BAG_UPDATE_COOLDOWN");
		
		if(Bagnon and Bagnon:IsVisible() ) then
			BagnonCore_Update(Bagnon);
		end

		if(Banknon and Banknon:IsVisible() ) then
			BagnonCore_Update(Banknon)
		end	
		
	elseif(event == "PLAYER_LEAVING_WORLD") then
	
		this:UnregisterEvent("BAG_UPDATE");
		this:UnregisterEvent("ITEM_LOCK_CHANGED");
		this:UnregisterEvent("BAG_UPDATE_COOLDOWN");
		
	elseif( event == "ADDON_LOADED" and arg1 == "Bagnon_Core") then
	
		--disable the ItemOnUpdate function for those using KCItems
		if(KC_Items) then
			BagnonItem_OnUpdate = function() return; end;
		end

		this:UnregisterEvent("ADDON_LOADED");
		
		this:RegisterEvent("PLAYER_LEAVING_WORLD");
		this:RegisterEvent("PLAYER_ENTERING_WORLD");
		
		--dynamically loading frame stuff
		this:RegisterEvent("BANKFRAME_OPENED");
		
		BagnonEvents_Load();
	end
end

function BagnonEvents_Load()
	BagnonEvents_LoadVariables();
	
	--unregister the bank frame
	BankFrame:UnregisterAllEvents();
end

function BagnonEvents_LoadVariables()
	if(not BagnonSets) then	
		BagnonSets = {};
		BagnonMsg(BAGNON_INITIALIZED);
	elseif(BagnonSets.version ~= BAGNON_VERSION) then
		BagnonSets.version = BAGNON_VERSION;
		BagnonMsg(BAGNON_UPDATED);
	end
end

function BagnonEvents_RescaleAll()
	if(Bagnon) then
		BagnonCore_Reposition(Bagnon);
	end
	
	if(Banknon) then
		BagnonCore_Reposition(Banknon);
	end
end

--[[
	Taken from Blizzard's code
	Shows the normal bank frame
--]]

function BagnonEvents_ShowBlizBank()

	BankFrameTitleText:SetText(UnitName("npc"));
	SetPortraitTexture(BankPortraitTexture,"npc");
	ShowUIPanel(BankFrame);
	
	if ( not BankFrame:IsVisible() ) then
		CloseBankFrame();
	end
	UpdateBagSlotStatus();
end