--[[
	BongosStats
		A movable memory, latency and fps display for Bongos
		
	Version History
		10/8/5
			Initial release
		10/11/5
			Updated for Bongos 0.4.0/Patch 1800
		12/16/05
			Made the text bigger
			Added a seperate keybinding
		8/14/6
			Made into a full Bongos Bar
--]]

local function Stats_OnUpdate()
	if this.toNextUpdate <= 0 then
		this.toNextUpdate = 1;
		
		local fpsText = getglobal(this:GetName() .. "FPS");
		fpsText:SetText(format("%.1f", GetFramerate()) .. "FPS");
		
		local memoryText = getglobal(this:GetName() .. "Memory");
		memoryText:SetText(format("%.3f", gcinfo() / 1000).."MB");
		
		local latencyText = getglobal(this:GetName() .. "Latency");
		local _, _, latency = GetNetStats();
		if (latency > PERFORMANCEBAR_MEDIUM_LATENCY) then
			latencyText:SetTextColor(1, 0, 0);									
		elseif (latency > PERFORMANCEBAR_LOW_LATENCY) then
			latencyText:SetTextColor(1, 1, 0);		
		else
			latencyText:SetTextColor(0, 1, 0);	
		end
		latencyText:SetText(latency .. "ms");
		
		this:SetWidth(fpsText:GetStringWidth() + memoryText:GetStringWidth() + latencyText:GetStringWidth() + 4);
	else
		this.toNextUpdate = this.toNextUpdate - arg1;
	end
end

BScript.AddStartupAction(function()
	local name = "BongosStats";
	local bar = BBar.Create("stats", name, "BStatsSets");	
	local fpsText = getglobal(name .. "FPS");	
	if not fpsText then
		fpsText = bar:CreateFontString(name .. "FPS");
		fpsText:SetFontObject("GameFontNormalLarge");
		fpsText:SetPoint("LEFT", bar, "LEFT");
		
		local memoryText = bar:CreateFontString(name .. "Memory");
		memoryText:SetFontObject("GameFontHighlightLarge");
		memoryText:SetPoint("LEFT", fpsText, "RIGHT", 2, 0);
		
		local latencyText = bar:CreateFontString(name .. "Latency");
		latencyText:SetFontObject("GameFontHighlightLarge");
		latencyText:SetPoint("LEFT", memoryText, "RIGHT", 2, 0);
	end
	
	bar:SetHeight(20)
	bar:SetWidth(120);
	bar.toNextUpdate = 0;
	bar:SetScript("OnUpdate", Stats_OnUpdate);
	
	if not bar:IsUserPlaced() then
		bar:SetPoint("CENTER", UIParent, "CENTER");
	end
end)