local _,ns = ...

--判断是否是秘密值
function ns.MM(value)
	if issecretvalue(value) or issecrettable(value) then
		return true
	else
		return false
	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, ...)
			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

--编辑模式拖动位置
function ns.AddEdit(frame)
    -- 自动生成数据库键名
    local dbName = frame:GetName() and frame:GetName().."_Edit"
	if not dbName then
		print("编辑模式拖动位置功能没有找到框体名")
		return
	end
	if not EditModeManagerFrame then
		print("编辑模式拖动位置功能没有找到编辑模式")
		return
	end
    frame:SetClampedToScreen(true)  --限制拖动范围
	frame:SetClampRectInsets(30, -30, -30, 30)  --允许拖出屏幕30像素左右上下
	-- 创建背景框
    local bg = frame:CreateTexture(nil, "BACKGROUND")
    bg:SetPoint("TOPLEFT", frame, "TOPLEFT", -5, 5)
	bg:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 5, -5)
    bg:SetAtlas("editmode-actionbar-highlight-nineslice-center")

    bg:Hide()
    
    -- 加载保存的位置
    if AddUIDB and AddUIDB[dbName] then
		frame:ClearAllPoints()
        frame:SetPoint(unpack(AddUIDB[dbName]))
    end
	
    
    -- 编辑模式切换函数
    local function EnterEditMode()
        bg:Show()
        frame:SetMovable(true)
        frame:EnableMouse(true)
        frame:RegisterForDrag("LeftButton")
        frame:SetScript("OnDragStart", frame.StartMoving)
        frame:SetScript("OnDragStop", function()
            frame:StopMovingOrSizing()
            if not AddUIDB then AddUIDB = {} end
            local left, bottom = frame:GetLeft(), frame:GetBottom()
			AddUIDB[dbName] = {"BOTTOMLEFT", "UIParent", "BOTTOMLEFT", left, bottom}
        end)
    end
    
    local function LeaveEditMode()
        bg:Hide()
        frame:SetMovable(false)
        frame:EnableMouse(false)
        frame:SetScript("OnDragStart", nil)
        frame:SetScript("OnDragStop", nil)
    end
    
    -- 注册编辑模式事件
    EditModeManagerFrame:HookScript("OnShow", EnterEditMode)
    EditModeManagerFrame:HookScript("OnHide", LeaveEditMode)
    
    -- 初始检查
    if EditModeManagerFrame:IsShown() then
        EnterEditMode()
    end
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)
	return AbbreviateNumbers(numbers)
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

