local _,ns = ...
ns.ADDUIEVENT("PLAYER_LOGIN", function()
if AddUIDB.unitf ==  false  then return end


------------------------------------------------
-- 修改默认头像
------------------------------------------------
-- 目标连击点大小
--ComboFrame:SetScale(1.1)

-- 玩家与目标框架大小
--TargetFrame:SetScale(1.2)
--PlayerFrame:SetScale(1.2)

SetCVar("threatShowNumeric", 0)	 --目标头像显示仇恨
SetCVar("showTargetcastbar", 1)	--显示目标施法条

if AddUIDB.debuff ==  true  then
	SetCVar("noBuffDebuffFilterOnTarget", 1)--显示所有debuff
	hooksecurefunc("TargetFrame_UpdateDebuffAnchor", function(self, buff, index)
		local aura = C_UnitAuras.GetDebuffDataByIndex(self.unit, index)
		if aura.sourceUnit ~= "player" then
			buff.Icon:SetDesaturated(true)
		else
			buff.Icon:SetDesaturated(false)
		end
	end)
else
	SetCVar("noBuffDebuffFilterOnTarget", 0)
end

hooksecurefunc("TargetFrame_UpdateDebuffAnchor", function(self, buff, index)
	local aura = C_UnitAuras.GetDebuffDataByIndex(self.unit, index)
	if not aura then return end
	if (aura.name and aura.duration and aura.duration <= 60 and aura.sourceUnit == "player") then
		buff.Cooldown:SetHideCountdownNumbers(false)
	else
		buff.Cooldown:SetHideCountdownNumbers(true)
	end
end)
hooksecurefunc("TargetFrame_UpdateBuffAnchor", function(self, buff, index)
	local aura = C_UnitAuras.GetBuffDataByIndex(self.unit, index)
	if not aura then return end
	if aura.dispelName == "Magic" then
		buff.Stealable:Show()
	else
		buff.Stealable:Hide()
	end
end)

local hptexture1 = "Interface\\AddOns\\AddUI\\UI\\media\\AD-TargetingFrame"
local hptexture2 = "Interface\\AddOns\\AddUI\\UI\\media\\Raid-Bar-Hp-Fill"
local hptexture3 = "UI-HUD-UnitFrame-Player-PortraitOff-Bar-Health-Status"
local function BOSSHP()
	for i = 1, MAX_BOSS_FRAMES do
		local bossTargetFrame = _G["Boss"..i.."TargetFrame"];--Boss1TargetFrame
		bossTargetFrame.TargetFrameContent.TargetFrameContentMain.Name:SetWidth(150)
		--bossTargetFrame.TargetFrameContent.TargetFrameContentMain.Name:SetVertexColor(1, 1, 0)
		bossTargetFrame.TargetFrameContent.TargetFrameContentMain.Name:SetFont("Fonts\\ARHei.ttf", 12, "")
		bossTargetFrame.TargetFrameContent.TargetFrameContentMain.Name:SetPoint("TOPLEFT",77,-32)
		bossTargetFrame.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar.HealthBarTexture:SetTexture(hptexture1)
		bossTargetFrame.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar:SetStatusBarColor(ns.ADUnitClassColor(bossTargetFrame.unit))
	end
end


--头像材质
local hp = CreateFrame("Frame")
hp:RegisterEvent("PLAYER_ENTERING_WORLD")
hp:RegisterEvent("UNIT_FACTION")
hp:RegisterEvent("INSTANCE_ENCOUNTER_ENGAGE_UNIT")
hp:SetScript("OnEvent", function(self, event,frame)
if event == "PLAYER_ENTERING_WORLD" then 
	
	PlayerFrame.PlayerFrameContent.PlayerFrameContentMain.HealthBarsContainer.HealthBar:SetStatusBarTexture(hptexture1)
	PlayerFrame.PlayerFrameContent.PlayerFrameContentMain.HealthBarsContainer.HealthBar:SetStatusBarColor(ns.ADUnitClassColor("player"))
	
	PlayerFrame.PlayerFrameContent.PlayerFrameContentContextual.AttackIcon:SetPoint("TOPLEFT",PlayerName,"TOPLEFT",-13,13)
	PlayerFrame.PlayerFrameContent.PlayerFrameContentContextual.AttackIcon:SetPoint("BOTTOMRIGHT",PlayerName,"TOPLEFT",0,0)
	PlayerFrame.PlayerFrameContent.PlayerFrameContentContextual.AttackIcon:SetScale(1.2)
	
	TargetFrame.TargetFrameContent.TargetFrameContentMain.Name:SetWidth(150)
	TargetFrame.TargetFrameContent.TargetFrameContentMain.Name:SetPoint("TOPLEFT",40,-25)
	
	TargetFrameToT.HealthBar:SetStatusBarTexture(hptexture2)
	TargetFrameToT:SetFrameStrata("BACKGROUND")
	FocusFrameToT.HealthBar:SetStatusBarTexture(hptexture2)
	FocusFrameToT:SetFrameStrata("BACKGROUND")
	
elseif event == "INSTANCE_ENCOUNTER_ENGAGE_UNIT" then
	BOSSHP()
elseif event == "UNIT_FACTION" then
	if string.match(frame,"boss") then
		BOSSHP()
	end
	if frame == "Target" then
		FrameBar(TargetFrame)
	end
	if frame == "Focus" then
		FrameBar(FocusFrame)
	end
end
end)

local function FrameBar(self)
	if self.TargetFrameContent then
		self.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar:SetStatusBarTexture(hptexture1)
		self.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar:SetStatusBarColor(ns.ADUnitClassColor(self.unit))
	elseif self.HealthBar then
		if not self.HealthBar.SetStatusBarColor then return end
		self.HealthBar:SetStatusBarTexture(hptexture1)
		self.HealthBar:SetStatusBarColor(ns.ADUnitClassColor(self.unit))
	end
end
hooksecurefunc(TargetFrame, "CheckClassification", FrameBar)
hooksecurefunc(FocusFrame, "CheckClassification", FrameBar)
hooksecurefunc(TargetFrameToT, "Update", FrameBar)
hooksecurefunc(FocusFrameToT, "Update", FrameBar)

--目标头像相关
TargetFrameToT:ClearAllPoints()
TargetFrameToT:SetPoint("RIGHT",TargetFrame,"RIGHT",15,-45)
TargetFrameToT.Name:ClearAllPoints()
TargetFrameToT.Name:SetPoint("TOPLEFT",TargetFrameToT,"BOTTOMLEFT",40,10)
TargetFrameSpellBar.TextBorder:SetTexture(nil)
TargetFrameSpellBar.TextBorder = ns.ADDUISET
TargetFrameSpellBar:SetHeight(15)
TargetFrameSpellBar.Icon:SetPoint("RIGHT",TargetFrameSpellBar,"LEFT",-3,0)
TargetFrameSpellBar.Text:ClearAllPoints()
TargetFrameSpellBar.Text:SetPoint("CENTER",TargetFrameSpellBar,"CENTER",0,0)
TargetFrameSpellBar.Text.SetPoint = ns.ADDUISET
TargetFrameSpellBar.Spark:SetHeight(30)		--施法条闪光高度
--焦点头像相关
FocusFrameToT:ClearAllPoints()
FocusFrameToT:SetPoint("RIGHT",FocusFrame,"RIGHT",15,-45)
FocusFrameToT.Name:ClearAllPoints()
FocusFrameToT.Name:SetPoint("TOPLEFT",FocusFrameToT,"BOTTOMLEFT",40,10)
FocusFrameSpellBar.TextBorder:SetTexture(nil)
FocusFrameSpellBar.TextBorder = ns.ADDUISET
FocusFrameSpellBar:SetHeight(15)
FocusFrameSpellBar.Icon:SetPoint("RIGHT",FocusFrameSpellBar,"LEFT",-3,0)
FocusFrameSpellBar.Text:ClearAllPoints()
FocusFrameSpellBar.Text:SetPoint("CENTER",FocusFrameSpellBar,"CENTER",0,0)
FocusFrameSpellBar.Text.SetPoint = ns.ADDUISET
FocusFrameSpellBar.Spark:SetHeight(30)		--施法条闪光高度
--施法条
local function ShowUnitCasting(self)
if not self.maxValue then return end
if not self.Cooldown  then
	self.Cooldown = self:CreateFontString(nil)
    self.Cooldown:SetFont(SystemFont_Outline_Small:GetFont(), 15, "OUTLINE")	--施法时间文字大小
    self.Cooldown:SetJustifyH("RIGHT")
    self.Cooldown:SetPoint("LEFT", self, "RIGHT", 0, 0)
elseif self.maxValue-self.value < 0 then
	self.Cooldown:SetText(" ")
elseif UnitCastingInfo(self.unit) then
	self.Cooldown:SetText(format("%.1f/%.1f",self.maxValue-self.value,self.maxValue))
elseif UnitChannelInfo(self.unit) then
	self.Cooldown:SetText(format("%.1f",self.value,0))
else
	self.Cooldown:SetText(" ")
end
end
TargetFrameSpellBar:HookScript("OnUpdate", ShowUnitCasting)
FocusFrameSpellBar:HookScript("OnUpdate", ShowUnitCasting)

local function ShowbossCasting(self)
if not self.maxValue then return end
if not self.Cooldown  then
	self.Cooldown = self:CreateFontString(nil)
    self.Cooldown:SetFont(SystemFont_Outline_Small:GetFont(), 15, "OUTLINE")	--施法时间文字大小
    self.Cooldown:SetJustifyH("RIGHT")
    self.Cooldown:SetPoint("RIGHT", self.Icon, "LEFT", 0, 0)
elseif self.maxValue-self.value < 0 then
	self.Cooldown:SetText(" ")
elseif UnitCastingInfo(self.unit) then
	self.Cooldown:SetText(format("%.1f/%.1f",self.maxValue - self.value,self.maxValue))
elseif UnitChannelInfo(self.unit) then
	self.Cooldown:SetText(format("%.1f",self.value,0))
else
	self.Cooldown:SetText(" ")
end
end
--boss框体施法条
for i = 1, MAX_BOSS_FRAMES do
	local bossTargetFrameSpellBar = _G["Boss"..i.."TargetFrameSpellBar"];
	bossTargetFrameSpellBar.TextBorder:SetTexture(nil)
	bossTargetFrameSpellBar.TextBorder = ADDUISET
	bossTargetFrameSpellBar:SetHeight(12)
	bossTargetFrameSpellBar.Icon:SetSize(15,15)
	bossTargetFrameSpellBar.Icon:SetPoint("RIGHT",bossTargetFrameSpellBar,"LEFT",-3,0)
	bossTargetFrameSpellBar.Text:ClearAllPoints()
	bossTargetFrameSpellBar.Text:SetPoint("CENTER",bossTargetFrameSpellBar,"CENTER",0,0)
	bossTargetFrameSpellBar.Text.SetPoint = ADDUISET
end



--自己和目标框架吸收盾数值
local playerabsText = PlayerFrame.PlayerFrameContent.PlayerFrameContentMain.HealthBarsContainer.HealthBar:CreateFontString("playerabsorb", "OVERLAY")
local targetabsText = TargetFrame.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar:CreateFontString("targetabsorb", "OVERLAY")
local focusabsText = FocusFrame.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar:CreateFontString("focusabsorb", "OVERLAY")


playerabsText:SetFont("Fonts\\ARHei.ttf", 12, "OUTLINE")
targetabsText:SetFont("Fonts\\ARHei.ttf", 12, "OUTLINE")
focusabsText:SetFont("Fonts\\ARHei.ttf", 12, "OUTLINE")

local C=CreateFrame("frame")
C:RegisterEvent('CVAR_UPDATE')
C:SetScript("OnEvent",function(_,_,msg)
	if GetCVar("statusTextDisplay") == "BOTH" then
		
		targetabsText:SetPoint("RIGHT", TargetFrame.TargetFrameContent.TargetFrameContentMain.HealthBar, "RIGHT",-42,0)
		focusabsText:SetPoint("RIGHT", FocusFrame.TargetFrameContent.TargetFrameContentMain.HealthBar, "RIGHT",-42,0)
	else
		playerabsText:SetPoint("RIGHT", PlayerFrame.PlayerFrameContent.PlayerFrameContentMain.HealthBarsContainer.HealthBar, "RIGHT",-2,0)
		targetabsText:SetPoint("RIGHT", TargetFrame.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar, "RIGHT",-2,0)
		focusabsText:SetPoint("RIGHT", FocusFrame.TargetFrameContent.TargetFrameContentMain.HealthBarsContainer.HealthBar, "RIGHT",-2,0)
		
	end
end)

local hp = CreateFrame("Frame")
hp:RegisterEvent("UNIT_ABSORB_AMOUNT_CHANGED")
hp:RegisterEvent("PLAYER_TARGET_CHANGED")
hp:RegisterEvent("PLAYER_FOCUS_CHANGED")
hp:SetScript("OnEvent", function(self, event,frame)
if frame=="player" then 
local playerabs = UnitGetTotalAbsorbs("player")
playerabsText:SetText(ns.ADDUIWK(playerabs))
end
if event == "PLAYER_TARGET_CHANGED" or frame=="target" then 
local targetabs = UnitGetTotalAbsorbs("target")
targetabsText:SetText(ns.ADDUIWK(targetabs))
end
if event == "PLAYER_FOCUS_CHANGED" or frame == "focus" then 
local focusabs = UnitGetTotalAbsorbs("focus")
focusabsText:SetText(ns.ADDUIWK(focusabs))
end
end)


-- 隐藏头像伤害数字
--------------------------------------------------
PlayerFrame.PlayerFrameContent.PlayerFrameContentMain.HitIndicator.HitText:SetText(nil)
PlayerFrame.PlayerFrameContent.PlayerFrameContentMain.HitIndicator.HitText.SetText = function() end
PetHitIndicator:SetText(nil)
PetHitIndicator.SetText = function() end
PetName:SetFont("fonts\\ARHei.ttf", 12)


end)