local _,ns = ...

--创建主框体
local UnitStatusBar = CreateFrame("Frame","UnitStatusBar")
UnitStatusBar:SetSize(150, 20)
UnitStatusBar:SetPoint("CENTER", UIParent, "CENTER", 0, -80)

-- 背景
local bg = UnitStatusBar:CreateTexture(nil, "BACKGROUND")
bg:SetTexture(130937)
bg:SetAllPoints(true)
bg:SetVertexColor(0.3, 0.3, 0.3, 0.5)

-- 边框
UnitStatusBar.border = CreateFrame("Frame", nil, UnitStatusBar, BackdropTemplateMixin and "BackdropTemplate")
UnitStatusBar.border:SetPoint("TOPLEFT", UnitStatusBar, "TOPLEFT", -1, 1)
UnitStatusBar.border:SetPoint("BOTTOMRIGHT", UnitStatusBar, "BOTTOMRIGHT", 1, -1)
UnitStatusBar.border:SetBackdrop{edgeFile = 'Interface\\Buttons\\WHITE8x8',edgeSize = 2}
UnitStatusBar.border:SetBackdropBorderColor(0, 0, 0, 0.7)

-- 创建血条
local healthBar = CreateFrame("StatusBar", nil, UnitStatusBar)
healthBar:SetSize(UnitStatusBar:GetWidth(), UnitStatusBar:GetHeight()/2+2) -- 血条大小
healthBar:SetPoint("TOP", UnitStatusBar, "TOP", 0, 0) -- 血条位置
healthBar:SetStatusBarTexture("Interface\\AddOns\\AddUI\\UI\\media\\AD-TargetingFrame") -- 血条材质
healthBar:GetStatusBarTexture():SetHorizTile(false)
healthBar:SetStatusBarColor(0, 1, 0, 1)

-- 更新血条
local function UpdateHealthBar(self, event, unit)
    if unit == "player" or event == "PLAYER_ENTERING_WORLD" then
        local health = UnitHealth("player") -- 获取玩家当前血量
        local maxHealth = UnitHealthMax("player") -- 获取玩家最大血量
        healthBar:SetMinMaxValues(0, maxHealth) -- 设置血量范围
        healthBar:SetValue(health) -- 设置当前血量
    end
end

-- 注册事件
healthBar:RegisterEvent("UNIT_HEALTH")
healthBar:RegisterEvent("UNIT_ABSORB_AMOUNT_CHANGED")
healthBar:RegisterEvent("PLAYER_ENTERING_WORLD")
healthBar:SetScript("OnEvent", UpdateHealthBar)

-- 创建能量条
local PowerBar = CreateFrame("StatusBar", nil, UnitStatusBar)
PowerBar:SetSize(UnitStatusBar:GetWidth(), UnitStatusBar:GetHeight()/2-2) -- 血条大小
PowerBar:SetPoint("BOTTOM", UnitStatusBar, "BOTTOM", 0, 0) -- 血条位置
PowerBar:SetStatusBarTexture("Interface\\AddOns\\AddUI\\UI\\media\\AD-TargetingFrame")
PowerBar:GetStatusBarTexture():SetHorizTile(false)
-- 显示能量文本
local PowerBarText = PowerBar:CreateFontString(nil, "OVERLAY")
PowerBarText:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
PowerBarText:SetPoint("CENTER", PowerBar, "CENTER", 0, 0)
-- 更新能量
local function UpdatePowerBar(self, event, unit)
    if unit == "player" or event == "PLAYER_ENTERING_WORLD" then
		local powerType, powerToken, altR, altG, altB = UnitPowerType("player");
		if powerToken then
			local color = PowerBarColor[powerType]
			PowerBar:SetStatusBarColor(color.r, color.g, color.b, 1)
		end
        local Power = UnitPower("player") -- 获取玩家当前血量
        local maxPower = UnitPowerMax("player") -- 获取玩家最大血量
        PowerBar:SetMinMaxValues(0, maxPower) -- 设置血量范围
        PowerBar:SetValue(Power) -- 设置当前血量
		PowerBarText:SetText(Power<1000 and Power or "")
		
		
		--特殊能量
		if GetCVarBool("nameplateResourceOnTarget") then return end
		if MageArcaneChargesFrame then--奥数充能
			MageArcaneChargesFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 53)
		end
		if PaladinPowerBarFrame then--骑士圣能
			PaladinPowerBarFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 53)
		end
		if DruidComboPointBarFrame then--猫德
			DruidComboPointBarFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 53)
		end
		if EssencePlayerFrame then--龙人
			EssencePlayerFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 53)
		end
		if RogueComboPointBarFrame then--盗贼连击点
			RogueComboPointBarFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 53)
		end
		if RuneFrame then--DK符文
			RuneFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 53)
		end
		if WarlockPowerFrame then--术士
			WarlockPowerFrame:SetPoint("TOP", UnitStatusBar, "BOTTOM", 0, 55)
		end		
    end
end

-- 注册事件
PowerBar:RegisterEvent("UNIT_POWER_UPDATE")
PowerBar:RegisterEvent("PLAYER_ENTERING_WORLD")
PowerBar:SetScript("OnEvent", UpdatePowerBar)
