local _,ns = ...
--修改自https://nga.178.com/read.php?tid=12269817&_fp=2
--只保留一个框体显示
local fontSize = ns.fontSize
local fadeTime = ns.fadeTime
local Fx,Fy = ns.Fx, ns.Fy

local enable = ns.enable
local showAA = ns.showAA
local showABS = ns.showABS
local showDOT = ns.showDOT
local showHOT = ns.showHOT

local eventFilter = {
	["SWING_DAMAGE"] = {suffix = "DAMAGE", index = 12, iconType = "swing", autoAttack = true},
	["RANGE_DAMAGE"] = {suffix = "DAMAGE", index = 15, iconType = "range"},
	["SPELL_DAMAGE"] = {suffix = "DAMAGE", index = 15, iconType = "spell"},
	["SPELL_PERIODIC_DAMAGE"] = {suffix = "DAMAGE", index = 15, iconType = "spell", isPeriod = true},

	["SPELL_HEAL"] = {suffix = "HEAL", index = 15, iconType = "spell"},
	["SPELL_PERIODIC_HEAL"] = {suffix = "HEAL", index = 15, iconType = "spell", isPeriod = true},

	["SWING_MISSED"] = {suffix = "MISS", index = 12, iconType = "swing"},
	["RANGE_MISSED"] = {suffix = "MISS", index = 15, iconType = "range"},
	["SPELL_MISSED"] = {suffix = "MISS", index = 15, iconType = "spell"},

	["SPELL_DISPEL"] = {suffix = "FAILED", index = 15, iconType = "spell", msg = ACTION_SPELL_DISPEL},
	["SPELL_STOLEN"] = {suffix = "FAILED", index = 15, iconType = "spell", msg = ACTION_SPELL_STOLEN},
	["SPELL_INTERRUPT"] = {suffix = "FAILED", index = 15, iconType = "spell", msg = ACTION_SPELL_INTERRUPT},

	["SPELL_AURA_APPLIED"] = {suffix = "ABSORB_ADDED", iconType = "spell"},	--吸收盾
	["SPELL_AURA_REFRESH"] = {suffix = "ABSORB_ADDED", iconType = "spell"},	--吸收盾
	
	["ENVIRONMENTAL_DAMAGE"] = {suffix = "ENVIRONMENT", index = 12, iconType = "env"},
}

local envTexture = {
	["Drowning"] = "spell_shadow_demonbreath",
	["Falling"] = "ability_rogue_quickrecovery",
	["Fatigue"] = "ability_creature_cursed_05",
	["Fire"] = "spell_fire_fire",
	["Lava"] = "ability_rhyolith_lavapool",
	["Slime"] = "inv_misc_slime_02",
}

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
eventFrame:SetScript("OnEvent", function(self, event,...)
	if not enable then return end
	if not AddUIDB.ct then return end
	self[event]()
end)

local function GetFloatingIcon(iconType, spellID, isPet)
	local texture, icon

	if iconType == "spell" then
		texture = C_Spell.GetSpellTexture(spellID)
	elseif iconType == "swing" then
		if isPet then
			texture = PET_ATTACK_TEXTURE
		else
			texture = 132321--C_Spell.GetSpellTexture(6603)
		end
	elseif iconType == "range" then
		texture = 132152--C_Spell.GetSpellTexture(75)
	elseif iconType == "env" then
		texture = "Interface\\Icons\\"..envTexture[spellID]
	end

	if not texture then
		texture = 134400--C_Spell.GetSpellTexture(195112)
	end

	icon = "|Hspell:"..spellID.."|h|T"..texture..":"..fontSize..":"..fontSize..":0:-3:64:64:5:59:5:59|t|h"
	return icon
end

local LightCTFrame = CreateFrame("ScrollingMessageFrame", "LightCTFrame", UIParent)
LightCTFrame:SetSpacing(3)--间距
LightCTFrame:SetMaxLines(8)--行数
LightCTFrame:SetSize(200, 200)--大小
LightCTFrame:SetFadeDuration(0.5)--淡出
LightCTFrame:SetJustifyH("RIGHT")
LightCTFrame:SetTimeVisible(fadeTime)--持续
LightCTFrame:SetFont("fonts\\ARHei.ttf" , fontSize+6, "OUTLINE")
LightCTFrame:SetHyperlinksEnabled(true)
LightCTFrame:SetScript("OnHyperlinkEnter", function(self, linkData, link)
    GameTooltip:SetOwner(self, "ANCHOR_CURSOR_RIGHT",20,-50)
    GameTooltip:SetHyperlink(link)
    GameTooltip:Show()
	self:SetFading(false)
end)

LightCTFrame:SetScript("OnHyperlinkLeave", function(self, linkData, link)
    GameTooltip:Hide()
	self:SetFading(true)
end)
LightCTFrame:SetScript("OnHyperlinkClick", function(self, linkData, link)
	local id = string.match(linkData,"%d+")
	if not id then print("未查询到法术ID") return end
	local icon = C_Spell.GetSpellTexture(id)
	local name = C_Spell.GetSpellName(id)
	if not icon or not name then print("未查询到法术") return end
	print("法术ID:|cff00BFFF"..id.."|r,超链接: ".."|cff00BFFF".."|H"..linkData.."|h"..name.."|T"..icon..":20|t".."|h|r")
end)



function eventFrame:PLAYER_LOGIN()
	SetCVar("enableFloatingCombatText", 0)
	
	if AddUIDB.ctp then
		LightCTFrame:SetPoint("BOTTOMRIGHT", TalkingHeadFrame.Selection, "BOTTOMRIGHT", 0, 0)
	else
		LightCTFrame:SetPoint("RIGHT", UIParent, "CENTER", Fx,Fy)
	end
end

function eventFrame:COMBAT_LOG_EVENT_UNFILTERED()
	local icon, text, color, failedColor, inputD, inputH, inputO
	local _, eventType, _, sourceGUID, sourceName, sourceFlags, _, destGUID, destName, destFlags, _, spellID, spellName, school = CombatLogGetCurrentEventInfo()

	local atPlayer = UnitGUID("player") == destGUID

	if atPlayer then
		local value = eventFilter[eventType]
		if value then
		
			if value.suffix == "DAMAGE" then
				local amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing, isOffHand = select(value.index, CombatLogGetCurrentEventInfo())
				if value.autoAttack and not showAA then return end
				if value.isPeriod and not showDOT then return end
				local abstext = absorbed and " |cff00C5CD("..ns.ADDUIWK(absorbed) or ""
				local overkilltext = overkill > 0 and " |cffFFFF00过量"..ns.ADDUIWK(overkill) or ""
				local amount = overkill > 0 and amount + overkill or amount	--总量(过量
				text = "-"..ns.ADDUIWK(amount)..abstext..overkilltext
				inputD = true-- 承受伤害显示
				if text and (critical or crushing) then
					text = "C"..text
				end
			elseif value.suffix == "HEAL" then
				local amount, overhealing, absorbed, critical = select(value.index, CombatLogGetCurrentEventInfo())
				if value.isPeriod and not showHOT then return end
				text = "+"..ns.ADDUIWK(amount)
				inputH = true		-- 承受治疗显示
				if text and critical then
					text = "C"..text
				end
			elseif value.suffix == "ABSORB_ADDED" then
				if not showABS then return end
				local auraInstanceID = C_UnitAuras.GetPlayerAuraBySpellID(spellID) and C_UnitAuras.GetPlayerAuraBySpellID(spellID).auraInstanceID
				if not auraInstanceID then return end
				local tooltipData = C_TooltipInfo.GetUnitBuffByAuraInstanceID("player",auraInstanceID,"HELPFUL")
				if not tooltipData then return end
				for i, line in ipairs(tooltipData.lines) do
					if string.match(line.leftText,COMBAT_TEXT_ABSORB) then 
						local abstext = string.match(line.leftText,"%d+")
						text = "+( "..ns.ADDUIWK(tonumber(abstext))
						inputA = true		-- 吸收盾
						break
					end
				end

			elseif value.suffix == "MISS" then
				local missType, isOffHand, amountMissed = select(value.index, CombatLogGetCurrentEventInfo())
				text = _G["COMBAT_TEXT_"..missType]
				inputD = true		-- 承受伤害MISS（招架、格挡、未命中等）显示
			elseif value.suffix == "ENVIRONMENT" then
				local envType, amount, overkill, school = select(value.index, CombatLogGetCurrentEventInfo())
				text = "-"..ns.ADDUIWK(amount)
				inputD = true			-- 环境伤害
			end
			--color = _G.CombatLog_Color_ColorArrayBySchool(school) or {r = .5, g = .5, b = .5}
			icon = GetFloatingIcon(value.iconType, spellID, isPet)
		end

		if text and icon then
			if inputD then
				if not school or school == 0 or school == 1 then
					LightCTFrame:AddMessage(text..icon, 1, 0, 0)
				else
					LightCTFrame:AddMessage(text..icon, 1, 0, 1)
				end
			elseif inputH then
				LightCTFrame:AddMessage(text..icon, 0, 1, 0)
			elseif inputA then
				LightCTFrame:AddMessage(text..icon, 0, 1, 1)
			end
		end
	end
end