-- 예상 치유량 표시 모듈
local HealComm = LibStub("LibHealComm-4.0", true)

if not HealComm then
	function IRF2:UpdateUnitHeal() end
	IRF2.IncomingHeals = IRF2.UpdateUnitHeal
	return
end

local IRF2 = InvenRaidFrames2

local _G = _G
local pairs = _G.pairs
local UnitGUID = _G.UnitGUID
local GetNumRaidMembers = _G.GetNumRaidMembers

local groupGUID = LibStub("LibGroupGUIDs-1.0")

local handler = {}
local units, pguid, guid, unit, pos = {}
local playerHeal, otherHeal, totalHeal, healMod

local function createHealBar(object)
	if object.heal then return end
	object.heal = CreateFrame("StatusBar", nil, object.health)
	object.heal:SetStatusBarTexture(object.health:GetStatusBarTexture():GetTexture())
	object.heal:SetAllPoints(object.health)
	object.heal:SetFrameLevel(2)
	object.heal:SetAlpha(IRF2.db.heal.barAlpha)
	object.heal:SetOrientation(IRF2.db.layout.healthOrientation == "세로" and "VERTICAL" or "HORIZONTAL")
	object.heal.iconFrame = CreateFrame("Frame", nil, object)
	object.heal.iconFrame:SetAllPoints(object)
	object.heal.iconFrame:SetFrameLevel(IRF2.db.frameLevel.healIcon)
	object.heal.icon = object.heal.iconFrame:CreateTexture(nil, "OVERLAY")
	object.heal.icon:SetWidth(10)
	object.heal.icon:SetHeight(10)
	object.heal.icon:SetTexture("Interface\\ChatFrame\\ChatFrameColorSwatch")
	pos = IRF2:GetFramePoint(IRF2.db.heal.pos)
	object.heal.icon:SetPoint(pos[1], object, pos[1], pos[2], pos[3])
end

function handler:SetHeal(object, id)
	if object then
		if IRF2.db.heal.use and handler.registered then
			if object.guid and object.guid == id then
				healMod =  HealComm:GetHealModifier(id) or 1
				playerHeal = (HealComm:GetHealAmount(id, HealComm.DIRECT_HEALS, nil, pguid) or 0) * healMod
				otherHeal = (HealComm:GetOthersHealAmount(id, HealComm.DIRECT_HEALS) or 0) * healMod
				if IRF2.db.heal.showMyHeal then
					totalHeal = otherHeal + playerHeal
				else
					totalHeal = otherHeal
				end
				if totalHeal > 0 then
					createHealBar(object)
					object.heal:SetStatusBarColor(object.health:GetStatusBarColor())
					object.heal:SetMinMaxValues(object.health:GetMinMaxValues())
					object.heal:SetValue(object.health:GetValue() + totalHeal)
					object.heal:Show()
					if IRF2.db.heal.showIcon then
						if IRF2.db.heal.myIcon and playerHeal > 0 then
							object.heal.icon:SetVertexColor(unpack(IRF2.db.heal.myHealColor))
							object.heal.icon:Show()
						elseif IRF2.db.heal.otherIcon and otherHeal > 0 then
							object.heal.icon:SetVertexColor(unpack(IRF2.db.heal.otherHealColor))
							object.heal.icon:Show()
						else
							object.heal.icon:Hide()
						end
					else
						object.heal.icon:Hide()
					end
				elseif object.heal then
					object.heal:Hide()
					object.heal.icon:Hide()
				end
			end
		elseif object.heal then
			object.heal:Hide()
			object.heal.icon:Hide()
		end
	end
end

function handler:HealUpdated(_, caster, _, healType, _, ...)
	if healType == HealComm.DIRECT_HEALS then
		pguid = pguid or UnitGUID("player")
		for i = 1, select("#", ...) do
			guid = select(i, ...)
			groupGUID:GUID2Unit(guid, units)
			for _, unit in pairs(units) do
				handler:SetHeal(IRF2.units[unit], guid)
			end
		end
	end
end

function handler:ModifierChange(_, id)
	pguid = pguid or UnitGUID("player")
	groupGUID:GUID2Unit(id, units)
	for _, unit in pairs(units) do
		handler:SetHeal(IRF2.units[unit], guid)
	end
end

function IRF2:UpdateUnitHeal(object)
	handler:SetHeal(object, object.guid)
end

function IRF2:IncomingHeals()
	for _, object in pairs(IRF2.units) do
		self:UpdateUnitHeal(object)
	end
end

local function healMod(running)
	if running then
		handler.registered = true
		HealComm.RegisterCallback(handler, "HealComm_HealStarted", "HealUpdated")
		HealComm.RegisterCallback(handler, "HealComm_HealUpdated", "HealUpdated")
		HealComm.RegisterCallback(handler, "HealComm_HealDelayed", "HealUpdated")
		HealComm.RegisterCallback(handler, "HealComm_HealStopped", "HealUpdated")
		HealComm.RegisterCallback(handler, "HealComm_ModifierChanged", "ModifierChange")
		IRF2:IncomingHeals()
	else
		handler.registered = nil
		HealComm.UnregisterCallback(handler, "HealComm_HealStarted")
		HealComm.UnregisterCallback(handler, "HealComm_HealUpdated")
		HealComm.UnregisterCallback(handler, "HealComm_HealDelayed")
		HealComm.UnregisterCallback(handler, "HealComm_HealStopped")
		HealComm.UnregisterCallback(handler, "HealComm_ModifierChanged")
	end
end

IRF2:RegisterShownModules("Heal", healMod)