local _,ns = ...

--用整合包还用此插件的直接全部不生效,免得他妈的报错来问我
local function FUCKERROR()
	if C_AddOns.IsAddOnLoaded("ElvUI") then
		print("|cFF00FF55AddUI|r|cffff5900部分功能已被禁用|r,因为你使用了ElvUI插件")
		return true
	end
	if C_AddOns.IsAddOnLoaded("BigFoot") then
		print("|cFF00FF55AddUI|r|cffff5900部分功能已被禁用|r,因为你使用了BigFoot插件")
		return true
	end
	if C_AddOns.IsAddOnLoaded("NDui") then
		print("|cFF00FF55AddUI|r|cffff5900部分功能已被禁用|r,因为你使用了NDui插件")
		return true
	end
end

--事件加载
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, ...)
			if not FUCKERROR() then
				for key, handler in pairs(_G.event.handler[event]) do
					handler(...)
				end
			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

--创建文本
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/1000000).."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

