local _,ns = ...

--事件加载
ns.ADDUISET = function() end
function ns.ADDUIEVENT(event, handler)
    if _G.event == nil then
        _G.event = CreateFrame("Frame")
        _G.event.handler = {}
        _G.event.OnEvent = function(frame, event, ...)
			for key, handler in pairs(_G.event.handler[event]) do
				handler(...)
			end
        end
        _G.event:SetScript("OnEvent", _G.event.OnEvent)
    end
    if _G.event.handler[event] == nil then
        _G.event.handler[event] = {}
        _G.event:RegisterEvent(event)
    end
    table.insert(_G.event.handler[event], handler)
end

--提醒文本
local adc = CreateFrame("Frame", nil, UIParent)
local adcfont = UIParent:CreateFontString("adcfont", "ARTWORK", "GameFontNormalLarge")
adcfont:SetPoint("LEFT", QuickJoinToastButton, "RIGHT", 0, -1)
adcfont:SetFont("fonts\\ARHei.ttf", 16, "OUTLINE")
adcfont:SetVertexColor(0,1,1)
local adcfontbg = UIParent:CreateTexture(nil, "BACKGROUND")
adcfontbg:SetPoint("CENTER", adcfont, "CENTER", 0, 0)
adcfontbg:SetTexture(130937)
adcfontbg:SetVertexColor(0, 0, 0, 0)

function ADCTEXT(text)
	adcfont:SetText(text)
	adcfontbg:SetSize(adcfont:GetStringWidth() + 10, adcfont:GetStringHeight() + 10)
	
	if fadeTimer then
        fadeTimer:Cancel()
        fadeTimer = nil
    end

    adcfont:SetAlpha(0)
    UIFrameFadeIn(adcfont, 0.5, 0, 1)
	UIFrameFadeIn(adcfontbg, 0.5, 0, 1)
	

    fadeTimer = C_Timer.NewTicker(6, function()
        UIFrameFadeOut(adcfont, 1.5, 1, 0)
		UIFrameFadeOut(adcfontbg, 1.5, 1, 0)
        fadeTimer:Cancel()
        fadeTimer = nil
    end, 1)
end

--创建文本
function ns.AddText(frame,size)
	local text = frame:CreateFontString(nil, "ARTWORK")
	text:SetFont(STANDARD_TEXT_FONT, size, 'OUTLINE')
	
	return text
end


--数值简化
function ns.ADDUIWK(numbers)

	local w = (AddUIDB and AddUIDB.hpUnits == 2) and "万" or "w"
	local wknumber = (type(numbers) == "number") and numbers or 0
	if not AddUIDB or (AddUIDB and (AddUIDB.hpUnits == 1 or AddUIDB.hpUnits == 2)) then
		if wknumber >= 1000000000 then
			return string.format("%.1f",wknumber/100000000).."亿"
		elseif wknumber >= 100000000 then
			return string.format("%.2f",wknumber/100000000).."亿"
		elseif wknumber >= 1000000 then
			return string.format("%d",wknumber/10000)..w
		elseif wknumber >= 100000 then
			return string.format("%.1f",wknumber/10000)..w
		elseif wknumber >= 10000 then
			return string.format("%.2f",wknumber/10000)..w
		--elseif wknumber >= 1000 then
		--	return string.format("%.1f",wknumber/1000).."k"
		elseif wknumber == 0 then
			return ""
		else
			return numbers
		end
	end
	if AddUIDB and AddUIDB.hpUnits == 3 then
		if wknumber >= 1000000000 then
			return string.format("%.2f",wknumber/1000000000).."B"	--不知道,我看details是这么写的
		elseif wknumber >= 100000000 then
			return string.format("%d",wknumber/1000000).."M"
		elseif wknumber >= 10000000 then
			return string.format("%.1f",wknumber/1000000).."M"
		elseif wknumber >= 1000000 then
			return string.format("%.2f",wknumber/1000000).."M"
		elseif wknumber >= 100000 then
			return string.format("%d",wknumber/1000).."K"
		elseif wknumber >= 10000 then
			return string.format("%.1f",wknumber/1000).."K"
		elseif wknumber >= 1000 then
			return string.format("%.2f",wknumber/1000).."K"
		elseif wknumber == 0 then
			return ""
		else
			return numbers
		end
	end
end

--百分比功能
function ns.ADDUIBFB(nownumber,maxnumber)
	if not nownumber or not maxnumber then return end
	if tonumber(nownumber) == 0 or tonumber(maxnumber) == 0 then return end
	local text = tonumber(nownumber)/tonumber(maxnumber) * 100
	if text == 100 then
		return format("%d",text).."%"
	elseif text> 10 then
		return format("%.1f",text).."%"
	elseif text > 0 then
		return format("%.2f",text).."%"
	else
		return ""
	end
end

--文本添加职业颜色
function ns.ADDUICOLOR(text,unit)
if not text or not unit then return end
	if UnitIsPlayer(unit) then
		local _,class = UnitClass(unit)
		if not class then return ("|cffFF0000"..text.."|r") end
		local colorStr = RAID_CLASS_COLORS[class].colorStr
		return ("|c"..colorStr..text.."|r")
	elseif UnitReaction(unit, "player") then
		local color = FACTION_BAR_COLORS and FACTION_BAR_COLORS[UnitReaction(unit, "player")]
		return ("|cff%.2x%.2x%.2x"..text.."|r"):format(color.r * 255, color.g * 255, color.b * 255)
	end
end

--获取职业颜色RGB
ns.PS = PlayerCastingBarFrame.SetStatusBarTexture
function ns.ADUnitClassColor(unit)
local Stylecolor
	if UnitIsPlayer(unit) then
		local _, class = UnitClass(unit)
		Stylecolor = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	elseif UnitReaction(unit, "player") then
		Stylecolor = FACTION_BAR_COLORS and FACTION_BAR_COLORS[UnitReaction(unit, "player")]
	end
	if (not UnitIsConnected(unit)) then	
		return 0.5, 0.5, 0.5, 1
	elseif Stylecolor and Stylecolor.r and Stylecolor.g and Stylecolor.b then
		return Stylecolor.r, Stylecolor.g, Stylecolor.b, 1
	else
		return 0.5, 0.5, 0.5, 1
	end
end


ns.TIPTEXTS = {}
local tipsindex = 1
function ns.tips(text)
	table.insert(ns.TIPTEXTS, "|cffFFFFFF"..tipsindex..": "..text.."|r")
	tipsindex = tipsindex + 1
end

