local _,ns = ...
ns.tips("BUFF栏左侧取消光环按钮")
local auraid = {
["操控时间"] = true,
["寒冰屏障"] = true,
["保护祝福"] = true,
["牺牲之手"] = true,
["圣盾术"] = true,
["相位潜行"] = true,

}

local needcancel = [[
/cancelAura 操控时间
/cancelAura 寒冰屏障
/cancelAura 保护祝福
/cancelAura 牺牲之手
/cancelAura 圣盾术
/cancelAura 相位潜行
]]

local function CreateFrames()
	if ADDUIcancelAuraButton then return end
	--创建框架
	local ADDUIcancelAuraButton = CreateFrame("Frame", "ADDUIcancelAuraButton", BuffFrame);
	ADDUIcancelAuraButton:SetSize(60, 60)
	ADDUIcancelAuraButton:SetPoint("TOPRIGHT",BuffFrame.Selection,"TOPLEFT",0,0); 	
	--创建背景
	ADDUIcancelAuraButton.Background = ADDUIcancelAuraButton:CreateTexture(nil, "BACKGROUND")
	ADDUIcancelAuraButton.Background:SetTexture(130937)
	ADDUIcancelAuraButton.Background:SetAllPoints(ADDUIcancelAuraButton)
	ADDUIcancelAuraButton.Background:SetColorTexture(0, 0, 0, 0)
	ADDUIcancelAuraButton.tiptext = ADDUIcancelAuraButton:CreateFontString(nil, "BACKGROUND")
	ADDUIcancelAuraButton.tiptext:SetFont(SystemFont_Outline_Small:GetFont(), 20, "OUTLINE")
	ADDUIcancelAuraButton.tiptext:SetPoint("CENTER", ADDUIcancelAuraButton, "CENTER", 0, 0)
	ADDUIcancelAuraButton.tiptext:SetText("取消\n光环")
	ADDUIcancelAuraButton.tiptext:SetTextColor(0.5,0.5,0.5)
	ADDUIcancelAuraButton.tiptext:Hide()
	--取消按钮
	ADDUIcancelAuraButton.ADcancelAuraButton = CreateFrame("CheckButton", "ADcancelAuraButton", ADDUIcancelAuraButton, "SecureActionButtonTemplate")
	ADDUIcancelAuraButton.ADcancelAuraButton:SetAttribute("type", "macro")
	ADDUIcancelAuraButton.ADcancelAuraButton:SetAttribute("macrotext", needcancel)
	ADDUIcancelAuraButton.ADcancelAuraButton:SetAllPoints(ADDUIcancelAuraButton)
	ADDUIcancelAuraButton.ADcancelAuraButton:RegisterForClicks("AnyDown", "AnyUp")
	ADDUIcancelAuraButton.ADcancelAuraButton:SetScript("OnEnter", function(self)
		ADDUIcancelAuraButton.Background:SetColorTexture(0, 0, 0, .5)
		ADDUIcancelAuraButton.tiptext:Show()
	end)
	ADDUIcancelAuraButton.ADcancelAuraButton:SetScript("OnLeave", function(self)
		ADDUIcancelAuraButton.Background:SetColorTexture(0, 0, 0, 0)
		ADDUIcancelAuraButton.tiptext:Hide()
	end)
	--图标材质
	ADDUIcancelAuraButton.T = ADDUIcancelAuraButton:CreateTexture()
	ADDUIcancelAuraButton.T:SetAllPoints(ADDUIcancelAuraButton)
	ADDUIcancelAuraButton.T:Hide()
	--操控时间的百分比
	ADDUIcancelAuraButton.text = ADDUIcancelAuraButton:CreateFontString(nil)
	ADDUIcancelAuraButton.text:SetFont(SystemFont_Outline_Small:GetFont(), 30, "OUTLINE")
	ADDUIcancelAuraButton.text:SetPoint("TOP", ADDUIcancelAuraButton, "BOTTOM", 0, 0)
	ADDUIcancelAuraButton.text:Hide()
	--冷却时间
	ADDUIcancelAuraButton.Cooldown = CreateFrame("Cooldown", nil, ADDUIcancelAuraButton, "CooldownFrameTemplate")
	ADDUIcancelAuraButton.Cooldown:SetAllPoints(ADDUIcancelAuraButton.T)
	ADDUIcancelAuraButton.Cooldown:SetHideCountdownNumbers(false)
	ADDUIcancelAuraButton.Cooldown:SetUseCircularEdge(true)
	ADDUIcancelAuraButton.Cooldown:SetReverse(true)
	local regon = ADDUIcancelAuraButton.Cooldown:GetRegions()
	if regon.GetText then 
		regon:SetFont(STANDARD_TEXT_FONT, 30, "OUTLINE")
	end
end
--获取光环API
local function HELPAURA()
	local id,icon,count,dispelType,duration,expirationTime,source
	AuraUtil.ForEachAura("player", "HELPFUL", nil, function(...)
		local name1,icon1,count1,dispelType1,duration1,expirationTime1,source1,_,_,spellid = ...
		if auraid[spellid] or auraid[name1] then
			id,icon,count,dispelType,duration,expirationTime,source = spellid,icon1,count1,dispelType1,duration1,expirationTime1,source1
		end
	end)
	return id,icon,count,dispelType,duration,expirationTime,source
end
--设置光环
local function MYAURA(per)	
	local id,icon,count,dispelType,duration,expirationTime,source = HELPAURA()
	if id == 342246 then
		source = per
		ADDUIcancelAuraButton.text:Show()
		ADDUIcancelAuraButton.text:SetText(source)
	else
		ADDUIcancelAuraButton.text:Hide()
	end
	if id then
		ADDUIcancelAuraButton.T:Show()
		ADDUIcancelAuraButton.T:SetTexture(icon)
		ADDUIcancelAuraButton.Cooldown:SetCooldown(expirationTime-duration,duration)
		if LibStub and LibStub("LibCustomGlow-1.0", true) then
			LibStub("LibCustomGlow-1.0").ButtonGlow_Start(ADDUIcancelAuraButton,{1, 1, 1, 1},0.125)
		end
	else
		ADDUIcancelAuraButton.T:SetTexture(nil)
		ADDUIcancelAuraButton.text:Hide()
		ADDUIcancelAuraButton.Cooldown:SetCooldown(0,0)
		if LibStub and LibStub("LibCustomGlow-1.0", true) then
			LibStub("LibCustomGlow-1.0").ButtonGlow_Stop(ADDUIcancelAuraButton)
		end
	end

end
local per = ""
local T=CreateFrame("frame")
T:RegisterEvent('UNIT_AURA')
T:RegisterEvent('PLAYER_ENTERING_WORLD')
T:RegisterEvent('UNIT_SPELLCAST_SUCCEEDED')
T:SetScript("OnEvent",function(_,event,unit,_,spellId)
	if event == "PLAYER_ENTERING_WORLD" then 
		CreateFrames()
	end
	if unit ~= "player" then return end
	if event == 'UNIT_SPELLCAST_SUCCEEDED' and spellId == 342247 then
		per = format("%.0f",UnitHealth("player")/UnitHealthMax("player")*100) .."%"
	end
	if event == 'UNIT_AURA' then
		MYAURA(per)
	end
end)



