-- 주문 타이머 모듈
local IRF2 = InvenRaidFrames2

local _G = _G
local pairs = _G.pairs
local GetRaidTargetIndex = _G.GetRaidTargetIndex
local SetRaidTargetIconTexture = _G.SetRaidTargetIconTexture

local id, pos, raidTargetUpdate, targetWatch
local firstRun = true
local reversePos = { TOPLEFT = "TOPRIGHT", TOPRIGHT = "TOPLEFT", LEFT = "RIGHT", RIGHT = "LEFT", BOTTOMLEFT = "BOTTOMRIGHT", BOTTOMRIGHT = "BOTTOMLEFT" }
local centerPos = { CENTER = "RIGHT", TOP = "TOPRIGHT", BOTTOM = "BOTTOMRIGHT" }

local function updatePosition(object)
	object.raidIcon:ClearAllPoints()
	object.raidIcon2:ClearAllPoints()
	pos = IRF2:GetFramePoint(IRF2.db.raidIcon.pos)[1]
	if IRF2.db.raidIcon.showTarget then
		if centerPos[pos] then
			object.raidIcon:SetPoint(centerPos[pos], object, pos)
			object.raidIcon2:SetPoint(reversePos[centerPos[pos]], object, pos)
		else
			object.raidIcon:SetPoint(pos, object, pos)
			object.raidIcon2:SetPoint(pos, object.raidIcon, reversePos[pos])
		end
	else
		object.raidIcon:SetPoint(pos, object, pos)
	end
end

local function raidIcon(object)
	object.raidIconFrame = CreateFrame("Frame", nil, object)
	object.raidIconFrame:SetFrameLevel(IRF2.db.frameLevel.raidIcon)
	object.raidIconFrame:SetAllPoints(object)
	object.raidIcon = object.raidIconFrame:CreateTexture(nil, "OVERLAY")
	object.raidIcon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
	object.raidIcon:SetWidth(IRF2.db.raidIcon.size)
	object.raidIcon:SetHeight(IRF2.db.raidIcon.size)
	object.raidIcon:Hide()
	object.raidIcon2 = object.raidIconFrame:CreateTexture(nil, "OVERLAY")
	object.raidIcon2:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons")
	object.raidIcon2:SetWidth(IRF2.db.raidIcon.size)
	object.raidIcon2:SetHeight(IRF2.db.raidIcon.size)
	object.raidIcon2:Hide()
	object.UpdateRaidIconPosition = updatePosition
	object.RAID_TARGET_UPDATE = raidTargetUpdate
	object:RegisterEvent("RAID_TARGET_UPDATE")
	object:UpdateRaidIconPosition()
	if firstRun then
		firstRun = nil
		IRF2:UseUnitTargetIcon()
	end
end

function raidTargetUpdate(self)
	if IRF2.db.raidIcon.use and self.unit then
		id = GetRaidTargetIndex(self.vehicleUnit or self.unit)
		if id and IRF2.db.raidIcon.filter[id] then
			SetRaidTargetIconTexture(self.raidIcon, id)
			self.raidIcon:Show()
		else
			self.raidIcon:Hide()
		end
	else
		self.raidIcon:Hide()
		self.raidIcon2:Hide()
	end
	if IRF2.db.outline == "전술 목표" then
		self:UpdateOutline()
	end
end

IRF2:RegisterObjectModule("RaidIcon", raidIcon)

local function createTargetWatch()
	targetWatch = CreateFrame("Frame")
	targetWatch:Hide()
	targetWatch.timer = 0
	targetWatch:SetScript("OnUpdate", function(self, timer)
		self.timer = self.timer + timer
		if self.timer > IRF2.db.updateTime then
			self.timer = 0
			if IRF2.db.raidIcon.showTarget then
				for unit, object in pairs(IRF2.units) do
					if object.targetUnit and UnitExists(object.targetUnit) then
						id = GetRaidTargetIndex(object.targetUnit)
						if id and IRF2.db.raidIcon.filter[id] then
							SetRaidTargetIconTexture(object.raidIcon2, id)
							object.raidIcon2:Show()
						else
							object.raidIcon2:Hide()
						end
					else
						object.raidIcon2:Hide()
					end
				end
			else
				self:Hide()
			end
		end
	end)
	targetWatch:SetScript("OnHide", function(self)
		for _, object in pairs(IRF2.objects) do
			if object.raidIcon2 then
				object.raidIcon2:Hide()
			end
		end
	end)
end

function IRF2:UseUnitTargetIcon()
	if IRF2.db.raidIcon.showTarget then
		if not targetWatch then
			createTargetWatch()
		end
		targetWatch:Show()
	elseif targetWatch then
		targetWatch:Hide()
	end
end