local addonName,ns = ...
ns.ADDUIEVENT("PLAYER_LOGIN", function()
if not AddUIDB.dimi then return end
--金币
local gold = ns.AddText(UIParent,13)
gold:SetPoint("BOTTOMRIGHT",UIParent,-2,0)

--耐久
local durable = ns.AddText(UIParent,13)
durable:SetPoint("BOTTOMRIGHT",UIParent,-80,0)


--内存
local addon = ns.AddText(UIParent,13)
addon:SetPoint("BOTTOMRIGHT",UIParent,-160,0)


--帧数
local fps = ns.AddText(UIParent,13)
fps:SetPoint("BOTTOMRIGHT",UIParent,-230,0)



-----------金币----------
local SILVER_AMOUNT_TEXTURE = "\124TInterface\\MoneyFrame\\UI-SilverIcon:%d:%d:1:0\124t";
local GOLD_AMOUNT_TEXTURE = "\124TInterface\\MoneyFrame\\UI-GoldIcon:%d:%d:1:0\124t";
local COPPER_AMOUNT_TEXTURE = "\124TInterface\\MoneyFrame\\UI-CopperIcon:%d:%d:1:0\124t";
local GoldIcon = format(GOLD_AMOUNT_TEXTURE)
local SilverIcon = format(SILVER_AMOUNT_TEXTURE)
local CopperIcon = format(COPPER_AMOUNT_TEXTURE)
local Profit	= 0
local Spent		= 0
local OldMoney	= 0

local function formatMoney(money)
	local gold = floor(math.abs(money) / 10000)
	local silver = mod(floor(math.abs(money) / 100), 100)
	local copper = mod(floor(math.abs(money)), 100)
	if gold ~= 0 then
		return format("%s"..GoldIcon.." %s"..SilverIcon.." %s"..CopperIcon, gold, silver, copper)
	elseif silver ~= 0 then
		return format("%s"..SilverIcon.." %s"..CopperIcon, silver, copper)
	else
		return format("%s"..CopperIcon, copper)
	end
end

local function formatTextMoney(money)
	return format("%.0f|cffffd700%s|r", money * 0.0001, GOLD_AMOUNT_SYMBOL)
end

local function FormatTooltipMoney(money)
	local gold, silver, copper = abs(money / 10000), abs(mod(money / 100, 100)), abs(mod(money, 100))
	local cash = ""
	cash = format("%d"..GoldIcon.." %d"..SilverIcon.." %d"..CopperIcon, gold, silver, copper)		
	return cash
end	

local goldupdate = CreateFrame("Frame")
goldupdate:RegisterEvent("PLAYER_MONEY")
goldupdate:RegisterEvent("SEND_MAIL_MONEY_CHANGED")
goldupdate:RegisterEvent("SEND_MAIL_COD_CHANGED")
goldupdate:RegisterEvent("PLAYER_TRADE_MONEY")
goldupdate:RegisterEvent("TRADE_MONEY_CHANGED")
goldupdate:RegisterEvent("PLAYER_ENTERING_WORLD")
goldupdate:SetScript("OnEvent", function(self,event)
	if event == "PLAYER_ENTERING_WORLD" then
		OldMoney = GetMoney()
	end
	local NewMoney = GetMoney()
	local Change = NewMoney-OldMoney -- Positive if we gain money
	
	if OldMoney > NewMoney then		-- Lost Money
		Spent = Spent - Change
	else							-- Gained Moeny
		Profit = Profit + Change
	end
	gold:SetText(formatTextMoney(NewMoney))
	gold:SetScript("OnEnter", function()
		GameTooltip:SetOwner(gold, "ANCHOR_TOP", 0, 6);
		GameTooltip:ClearAllPoints()
		GameTooltip:SetPoint("BOTTOM", gold, "TOP", 0, 1)
		GameTooltip:ClearLines()
		GameTooltip:AddLine(CURRENCY,0,.6,1)
		GameTooltip:AddLine(" ")
		GameTooltip:AddLine("本次登陆: ",.6,.8,1)
		GameTooltip:AddDoubleLine("获得:", formatMoney(Profit), 1, 1, 1, 1, 1, 1)
		GameTooltip:AddDoubleLine("花费:", formatMoney(Spent), 1, 1, 1, 1, 1, 1)
		if Profit < Spent then
			GameTooltip:AddDoubleLine("亏损:", formatMoney(Profit-Spent), 1, 0, 0, 1, 1, 1)
		elseif (Profit-Spent)>0 then
			GameTooltip:AddDoubleLine("盈利:", formatMoney(Profit-Spent), 0, 1, 0, 1, 1, 1)
		end				
		GameTooltip:Show()
	end)
	OldMoney = GetMoney()
	gold:SetScript("OnLeave", function() GameTooltip:Hide() end)
end)

-----------耐久----------
local classc = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[select(2,UnitClass('player'))] 
local Colored = ("|cff%.2x%.2x%.2x"):format(classc.r * 255, classc.g * 255, classc.b * 255)
local gradient = function(perc)
	perc = perc > 1 and 1 or perc < 0 and 0 or perc -- Stay between 0-1
	local seg, relperc = math.modf(perc*2)
	local r1,g1,b1,r2,g2,b2 = select(seg*3+1,1,0,0,1,1,0,0,1,0,0,0,0) -- R -> Y -> G
	local r,g,b = r1+(r2-r1)*relperc,g1+(g2-g1)*relperc,b1+(b2-b1)*relperc
	return format("|cff%02x%02x%02x",r*255,g*255,b*255),r,g,b
end
local durableupdate = CreateFrame("Frame")
durableupdate:RegisterEvent("UPDATE_INVENTORY_DURABILITY")
durableupdate:RegisterEvent("MERCHANT_SHOW")
durableupdate:RegisterEvent("PLAYER_ENTERING_WORLD")
durableupdate:SetScript("OnEvent", function(self,event)
	local localSlots = {
		[1] = {1, "头部", 1000},
		[2] = {3, "肩部", 1000},
		[3] = {5, "胸部", 1000},
		[4] = {6, "腰部", 1000},
		[5] = {9, "手腕", 1000},
		[6] = {10, "手", 1000},
		[7] = {7, "腿部", 1000},
		[8] = {8, "脚", 1000},
		[9] = {16, "主手", 1000},
		[10] = {17, "副手", 1000},
		[11] = {18, "远程", 1000}
	}
	local Total = 0
	local current, max
	for i = 1, 11 do
		if GetInventoryItemLink("player", localSlots[i][1]) ~= nil then
			current, max = GetInventoryItemDurability(localSlots[i][1])
			if current then 
				localSlots[i][3] = current/max
				Total = Total + 1
			end
		end
	end
	table.sort(localSlots, function(a, b) return a[3] < b[3] end)
	
	if Total > 0 then
		durable:SetText(format(gsub("[color]%d|r%%".."耐久","%[color%]",(gradient(floor(localSlots[1][3]*100)/100))), floor(localSlots[1][3]*100)))
	else
		durable:SetText(Colored.."无".."|r%D")
	end

	durable:SetScript("OnEnter", function()
		local total, equipped = GetAverageItemLevel()
		GameTooltip:SetOwner(durable, "ANCHOR_TOP", 0, 6);
		GameTooltip:ClearAllPoints()
		GameTooltip:SetPoint("BOTTOM", durable, "TOP", 0, 1)
		GameTooltip:ClearLines()
		GameTooltip:AddDoubleLine(DURABILITY,format("%s: %d/%d", STAT_AVERAGE_ITEM_LEVEL, equipped, total),0,.6,1,0,.6,1)
		GameTooltip:AddLine(" ")
		for i = 1, 11 do
			if localSlots[i][3] ~= 1000 then
				green = localSlots[i][3]*2
				red = 1 - green
				GameTooltip:AddDoubleLine(localSlots[i][2], floor(localSlots[i][3]*100).."%", 1, 1, 1, red + 1, green, 0)
			end
		end
		GameTooltip:AddDoubleLine(" ","--------------",1,1,1,0.5,0.5,0.5)
		GameTooltip:Show()
	end)
	durable:SetScript("OnLeave", function() GameTooltip:Hide() end)
end)

--内存
local colorme = string.format("%02x%02x%02x", 1*255, 1*255, 1*255)
local Total, tTotal, Mem, MEMORY_TEXT, Memory, maxAddOns, statColor
local function formatMem(memory, color)
	if color then
		statColor = { "|cff"..colorme, "|r" }
	else
		statColor = { "", "" }
	end

	local mult = 10^1
	if memory > 999 then
		local mem = floor((memory/1024) * mult + 0.5) / mult
		if mem % 1 == 0 then
			return mem..string.format(".0 %smb%s", unpack(statColor))
		else
			return mem..string.format(" %smb%s", unpack(statColor))
		end
	else
		local mem = floor(memory * mult + 0.5) / mult
		if mem % 1 == 0 then
			return mem..string.format(".0 %skb%s", unpack(statColor))
		else
			return mem..string.format(" %skb%s", unpack(statColor))
		end
	end
end

local function RefreshMem()
	Memory = {}
	UpdateAddOnMemoryUsage()
	Total = 0
	for i = 1, C_AddOns.GetNumAddOns() do
		Mem = GetAddOnMemoryUsage(i)
		Memory[i] = { select(2, C_AddOns.GetAddOnInfo(i)), Mem, C_AddOns.IsAddOnLoaded(i) }
		Total = Total + Mem
	end

	MEMORY_TEXT = formatMem(Total, true)
	table.sort(Memory, function(a, b)
		if a and b then
			return a[2] > b[2]
		end
	end)
	return Total
end

local function RefreshText()
	UpdateAddOnMemoryUsage()
	tTotal = 0
	for i = 1, C_AddOns.GetNumAddOns() do
		local tMem = GetAddOnMemoryUsage(i)
		tTotal = tTotal + tMem
	end
end

local function formatTotal(Total)
	if Total >= 1024 then
		return format("%.1f"..Colored.."mb|r" or "%.1fmb", Total / 1024)
	else
		return format("%.1f"..Colored.."kb|r" or "%.1fkb", Total)
	end
end

addon:SetScript("OnMouseDown", function(self, btn)
	if btn == "LeftButton" then
		RefreshMem()
		local before = gcinfo()
		collectgarbage("collect")
		RefreshMem()
		print(format("|cff66C6FF%s:|r %s","释放內存",formatMem(before - gcinfo())))
	end
	self:GetScript("OnEnter")(self)
	RefreshText()
end)
addon:SetScript("OnEnter", function(self)
	RefreshMem()
	GameTooltip:SetOwner(fps, "ANCHOR_TOP", 0, 6);
	GameTooltip:ClearAllPoints()
	GameTooltip:SetPoint("BOTTOM", fps, "TOP", 0, 1)
	GameTooltip:ClearLines()
	GameTooltip:AddDoubleLine(format("%s:",ADDONS),formatMem(Total), 0, 0.6, 1, .6, .8, 1)
	GameTooltip:AddLine(" ")

	for i = 1, #Memory do
		if Memory[i][3] then
			local color = Memory[i][2] <= 102.4 and {0,1} -- 0 - 100
			or Memory[i][2] <= 512 and {0.75,1} -- 100 - 512
			or Memory[i][2] <= 1024 and {1,1} -- 512 - 1mb
			or Memory[i][2] <= 2560 and {1,0.75} -- 1mb - 2.5mb
			or Memory[i][2] <= 5120 and {1,0.5} -- 2.5mb - 5mb
			or {1,0.1} -- 5mb +
			GameTooltip:AddDoubleLine(Memory[i][1], formatMem(Memory[i][2], false), 1, 1, 1, color[1], color[2], 0)						
		end
	end

	local more, moreMem = 0, 0
	GameTooltip:AddLine(" ")
	GameTooltip:AddDoubleLine("系统插件内存:",formatMem(gcinfo() - Total),.6,.8,1,1,1,1)
	GameTooltip:AddDoubleLine("总内存使用:",formatMem(collectgarbage'count'),.6,.8,1,1,1,1)
	GameTooltip:AddDoubleLine(" ","--------------",1,1,1,0.5,0.5,0.5)
	GameTooltip:Show()
	addon:SetScript("OnLeave", function() GameTooltip:Hide() end)
end)
local addonupdate = C_Timer.NewTicker(5, function()
	RefreshText()
	addon:SetText(formatTotal(tTotal))
end)
-----帧数
local function colorlatency(latency)
	if latency < 300 then
		return "|cff0CD809"..latency
	elseif (latency >= 300 and latency < 500) then
		return "|cffE8DA0F"..latency
	else
		return "|cffD80909"..latency
	end
end

local fpscolor
local latencycolor


fps:SetScript("OnEnter", function(self)
	local _, _, latencyHome, latencyWorld = GetNetStats()
	GameTooltip:SetOwner(fps, "ANCHOR_TOP", 0, 6);
	GameTooltip:ClearAllPoints()
	GameTooltip:SetPoint("BOTTOM", fps, "TOP", 0, 1)
	GameTooltip:ClearLines()
	GameTooltip:AddDoubleLine("延迟:",format("%s%s(%s)/%s%s(%s)",colorlatency(latencyHome).."|r","Ms","本地",colorlatency(latencyWorld).."|r","Ms",CHANNEL_CATEGORY_WORLD),.6, .8, 1, 1, 1, 1)
	GameTooltip:Show()
end)
fps:SetScript("OnLeave", function() GameTooltip:Hide() end)
local fpsupdate = C_Timer.NewTicker(1, function()
	local _, _, latencyHome, latencyWorld = GetNetStats()
	local lat = math.max(latencyHome, latencyWorld)
	if floor(GetFramerate()) >= 30 then
		fpscolor = "|cff0CD809"
	elseif (floor(GetFramerate()) > 15 and floor(GetFramerate()) < 30) then
		fpscolor = "|cffE8DA0F"
	else
		fpscolor = "|cffD80909"
	end
	fps:SetText(fpscolor..floor(GetFramerate()).."|r".." Fps "..colorlatency(lat).."|r".."Ms")
end)

end)