--[[
	BagnonDBUI
		Functions for the dropdown menu for showing cached data
		No alterations if this code should be needed to make it work with other databases
	
	Essentially the dropdown is used to switch between the inventory of other characters
	Why not use a normal dropdown?  It takes a lot of memory
--]]

--switch to view a different character's data
function BagnonDBUI_ChangePlayer(frame, player)
	frame.player = player;
	
	--update the frame's title
	getglobal(frame:GetName() .. "Title"):SetText( string.format(frame.title, player) );
	
	--update the frame's contents
	BagnonFrame_Generate(frame);
	
	--update the frame's bags, if they exist and are shown
	local bagFrame = getglobal(frame:GetName() .. "Bags");
	if( bagFrame and bagFrame:IsShown() ) then
		for _, bag in pairs({ bagFrame:GetChildren() }) do
			BagnonBag_UpdateTexture(frame, bag:GetID());
		end
	end
	
	--update banknon's purchase button
	if(frame == Banknon) then
		Banknon_UpdatePurchaseButtonVis();
	end
end

--[[ Character List ]]--

--create a player button, which is used to switch between characters
local function CreatePlayerButton(id, parent)
	local button = CreateFrame("CheckButton", parent:GetName() .. id, parent, "BagnonDBUINameBox");
	if(id == 1) then
		button:SetPoint("TOPLEFT", getglobal(parent:GetName() .. "Text"), "BOTTOMLEFT", -24, 2);
	else
		button:SetPoint("TOP", getglobal(parent:GetName() .. (id - 1) ), "BOTTOM", 0, 6);
	end
	return button;
end

function BagnonDBUI_ShowCharacterList(parentFrame)
	BagnonDBUICharacterList.frame = parentFrame;
	
	--update button info
	local button, player;
	local index = 0;
		
	for _, player in pairs(BagnonDB.GetPlayerList()) do
		index = index + 1;
		
		button = getglobal("BagnonDBUICharacterList" .. index) or CreatePlayerButton(index, BagnonDBUICharacterList);
		button:SetText( player );

		if( parentFrame.player == player ) then
			button:SetChecked(true);
			button:Show();
		else
			button:SetChecked(false);
		end
	end
		
	local i = index + 1;
	while getglobal("BagnonDBUICharacterList" .. i) do
		getglobal("BagnonDBUICharacterList" .. i):Hide();
		i = i + 1;
	end
		
	--resize and position the frame
	BagnonDBUICharacterList:SetHeight(38 + index * 20);
	local x, y = GetCursorPosition();
	x = x / UIParent:GetScale();
	y = y / UIParent:GetScale();

	BagnonDBUICharacterList:ClearAllPoints();
	BagnonDBUICharacterList:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x - 24, y + 24);
	BagnonDBUICharacterList:Show();
end

--[[ Function Hooks and Overrides ]]--

--Alters the tooltip of bagnon moneyframes to show total gold across all characters on the current realm
function BagnonFrameMoney_OnEnter()
	if( this:GetLeft() > ( UIParent:GetRight() / 2) ) then
		GameTooltip:SetOwner(this, "ANCHOR_LEFT");
	else
		GameTooltip:SetOwner(this, "ANCHOR_RIGHT");
	end
	
	GameTooltip:SetText( string.format(BAGNON_FOREVER_MONEY_ON_REALM, GetRealmName()) );
	
	local money = 0;
	for _, player in pairs(BagnonDB.GetPlayerList()) do
		money = money + BagnonDB.GetMoney(player);
	end
	
	SetTooltipMoney(GameTooltip, money);
	GameTooltip:Show();
end

--hide any menus attached to the frame, if they're visible and we're hiding the frame
local oBagnonFrame_OnHide = BagnonFrame_OnHide;
BagnonFrame_OnHide = function()
	oBagnonFrame_OnHide();
	
	if(BagnonDBUICharacterList.frame == this) then
		BagnonDBUICharacterList:Hide();
	end
end