-- 어그로 체크
local Aggro = LibStub("LibAggro-1.0", true)
if not Aggro then
	function IRF2:GetAggro() return nil end
	return
end

local IRF2 = InvenRaidFrames2

local _G = _G
local pairs = _G.pairs
local select = _G.select
local GetNumPartyMembers = _G.GetNumPartyMembers
local GetNumRaidMembers = _G.GetNumRaidMembers
local au, vau

local SM = LibStub("LibSharedMedia-3.0")

local function updateaggro(unit, value)
	if IRF2.units[unit] then
		IRF2.units[unit].hasAggro = value
		IRF2.units[unit]:UpdateText()
		if IRF2.db.outline == "어그로" then
			IRF2.units[unit]:UpdateOutline()
		end
	end
	if unit == "player" and (InvenRaidFrames2CharDB.aggroSound == "항상" or (InvenRaidFrames2CharDB.aggroSound == "파티/공격대" and (GetNumPartyMembers() > 0 or GetNumRaidMembers() > 0)) or (InvenRaidFrames2CharDB.aggroSound == "공격대" and GetNumRaidMembers() > 0)) then
		if value then
			if InvenRaidFrames2CharDB.sound.aggroGain ~= "None" then
				PlaySoundFile(SM:Fetch("sound", InvenRaidFrames2CharDB.sound.aggroGain))
			end
		elseif InvenRaidFrames2CharDB.sound.aggroLost ~= "None" then
			PlaySoundFile(SM:Fetch("sound", InvenRaidFrames2CharDB.sound.aggroLost))
		end
	end
end

local function aggrofunc(aggro, guid, ...)
	aggro = aggro and true or false
	for i = 1, select("#", ...) do
		au = select(i, ...)
		if au == "pet" or au == "vehicle" then
			if IRF2.units.player and IRF2.units.player.vehicleUnit == "pet" then
				updateaggro("player", aggro)
				if IRF2.units[au] then
					updateaggro(au, aggro)
				end
			else
				updateaggro(au, aggro)
			end
		elseif au:find("pet") then
			vau = au:gsub("pet", "")
			if IRF2.units[vau] and IRF2.units[vau].vehicleUnit == au then
				updateaggro(vau, aggro)
				if IRF2.units[au] then
					updateaggro(au, aggro)
				end
			else
				updateaggro(au, aggro)
			end
		else
			updateaggro(au, aggro)
		end
	end
end

local vehicleCheck = CreateFrame("Frame")
vehicleCheck:SetScript("OnEvent", function(self, event, unit)
	if IRF2.units[unit] then
		if UnitHasVehicleUI(unit) then
			self.unit = IRF2:GetVehicleUnit(unit) or unit
		else
			self.unit = unit
		end
		self.value = (self.unit and Aggro:UnitHasAggro(self.unit) and true or false) or nil
		if self.value ~= IRF2.units[unit].hasAggro then
			IRF2.units[unit]:UpdateText()
			if IRF2.db.outline == "어그로" then
				IRF2.units[unit]:UpdateOutline()
			end
		end
	end
end)

local function aggroMod(running)
	if running then
		vehicleCheck:RegisterEvent("UNIT_ENTERED_VEHICLE")
		vehicleCheck:RegisterEvent("UNIT_EXITED_VEHICLE")
		Aggro:Register(aggrofunc)
		for unit, object in pairs(IRF2.units) do
			object.hasAggro = Aggro:UnitHasAggro(object.vehicleUnit or unit) and true or false
			IRF2.units[unit]:UpdateText()
			if IRF2.db.outline == "어그로" then
				IRF2.units[unit]:UpdateOutline()
			end
		end
	else
		vehicleCheck:UnregisterAllEvents()
		Aggro:Unregister(aggrofunc)
	end
end

IRF2:RegisterShownModules("Aggro", aggroMod)

function IRF2:GetAggro(unit)
	return Aggro:UnitHasAggro(unit) and true or false
end