-- 전투 준비 확인 모듈
local IRF2 = InvenRaidFrames2

local _G = _G
local pairs = _G.pairs
local UnitName = _G.UnitName
local UnitIsUnit = _G.UnitIsUnit
local UnitIsPlayer = _G.UnitIsPlayer
local partyMembers = { "player", "party1", "party2", "party3", "party4" }

local readyCheckFrame = CreateFrame("Frame")
readyCheckFrame:SetScript("OnEvent", function(self, event, ...) self[event](self, ...) end)

function readyCheckFrame:READY_CHECK(name)
	-- 누군가가 전투 준비 확인을 했을 때 처리 시작
	PlaySound("ReadyCheck")
	readyCheckFrame.timer = nil
	readyCheckFrame.delay = nil
	readyCheckFrame.fadeOut = nil
	readyCheckFrame.startFadeOut = nil
	readyCheckFrame:SetScript("OnUpdate", nil)
	if IRF2:IsReadyCheckable() then
		self.isReadyCheck = true
		for unit, object in pairs(IRF2.units) do
			if object.readyCheck then
				object.readyCheck.icon:SetTexture(READY_CHECK_WAITING_TEXTURE)
				object.readyCheck.state = "waiting"
				object.readyCheck:SetAlpha(1)
				object.readyCheck:Show()
			end
		end
		if GetNumRaidMembers() == 0 and UnitName("player") == name then
			self:READY_CHECK_CONFIRM("player", true)
		elseif IRF2.names[name] then
			for _, object in pairs(IRF2.names[name]) do
				if UnitIsPlayer(object.unit) then
					self:READY_CHECK_CONFIRM(object.unit, true)
				end
			end
		end
		-- 파티/공격대원 변경 및 전투 시작시 전투 준비 프레임 숨김을 위한 이벤트 등록
		self:RegisterEvent("RAID_ROSTER_UPDATE")
		self:RegisterEvent("PARTY_MEMBERS_CHANGED")
		self:RegisterEvent("PLAYER_ENTERING_WORLD")
		self:RegisterEvent("PLAYER_REGEN_DISABLED")
	end
end

local function setConfirm(unit, confirm)
	-- 전투 준비 예/아니오에 따른 텍스쳐 변경
	if IRF2.units[unit] and IRF2.units[unit].readyCheck then
		if confirm then
			IRF2.units[unit].readyCheck.icon:SetTexture(READY_CHECK_READY_TEXTURE)
			IRF2.units[unit].readyCheck.state = "ready"
		else
			IRF2.units[unit].readyCheck.icon:SetTexture(READY_CHECK_NOT_READY_TEXTURE)
			IRF2.units[unit].readyCheck.state = "notready"
		end
	end
end

function readyCheckFrame:READY_CHECK_CONFIRM(unit, confirm)
	-- 전투 준비 예/아니오 버튼 클릭 처리
	if GetNumRaidMembers() > 0 and IRF2:GetPartyHeaderShown() then
		for _, party in ipairs(partyMembers) do
			if UnitIsUnit(unit, party) then
				setConfirm(party, confirm)
				break
			end
		end
	end
	setConfirm(unit, confirm)
end

function readyCheckFrame:READY_CHECK_FINISHED()
	-- 전투 준비 체크 완료
	for unit, object in pairs(IRF2.units) do
		if object.readyCheck and object.readyCheck.state == "waiting" then
			object.readyCheck.icon:SetTexture(READY_CHECK_AFK_TEXTURE)
			object.readyCheck.state = "afk"
		end
	end
	self.timer = 0
	self.delay = 10
	self.fadeOut = nil
	self.startFadeOut = nil
	self:SetScript("OnUpdate", self.OnUpdate)
end

local alpha

function readyCheckFrame:OnUpdate(timer)
	-- 전투 준비 체크 완료 후 10초간 대기뒤 2초동안 서서히 사라짐 효과 부여
	self.timer = self.timer + timer
	if self.fadeOut then
		if self.timer < self.fadeOut then
			alpha = (self.fadeOut - self.timer) / 2
			for unit, object in pairs(IRF2.units) do
				if object.readyCheck then
					object.readyCheck:SetAlpha(alpha)
				end
			end
		else
			self:RAID_ROSTER_UPDATE()
		end
	elseif self.delay and self.timer > self.delay then
		self.delay = nil
		self.startFadeOut = self.timer
		self.fadeOut = self.timer + 2
	end
end

function readyCheckFrame:RAID_ROSTER_UPDATE()
	-- 파티/공격대원 변경 및 전투 시작시 전투 준비 프레임 숨김을 처리
	if self.isReadyCheck then
		self:SetScript("OnUpdate", nil)
		self.isReadyCheck = false
		for _, object in pairs(IRF2.objects) do
			if object.readyCheck then
				object.readyCheck.state = nil
				object.readyCheck:Hide()
			end
		end
	end
	self:UnregisterEvent("RAID_ROSTER_UPDATE")
	self:UnregisterEvent("PARTY_MEMBERS_CHANGED")
	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	self:UnregisterEvent("PLAYER_REGEN_DISABLED")
end

readyCheckFrame.PARTY_MEMBERS_CHANGED = readyCheckFrame.RAID_ROSTER_UPDATE
readyCheckFrame.PLAYER_ENTERING_WORLD = readyCheckFrame.RAID_ROSTER_UPDATE
readyCheckFrame.PLAYER_REGEN_DISABLED = readyCheckFrame.RAID_ROSTER_UPDATE

local function CreateReadyCheckFrame(object)
	-- 유닛 프레임에 전투 준비를 위한 프레임 생성
	if object:GetParent() ~= IRF2.group.pet then
		object.readyCheck = CreateFrame("Frame", nil, object)
		object.readyCheck:SetFrameLevel(IRF2.db.frameLevel.readyCheck)
		object.readyCheck:SetPoint("CENTER", object, "CENTER")
		object.readyCheck:SetWidth(20)
		object.readyCheck:SetHeight(20)
		object.readyCheck.icon = object.readyCheck:CreateTexture(nil, "OVERLAY")
		object.readyCheck.icon:SetAllPoints(object.readyCheck)
		object.readyCheck:Hide()
		if not readyCheckFrame.initEvent then
			readyCheckFrame.initEvent = true
			readyCheckFrame:RegisterEvent("READY_CHECK")
			readyCheckFrame:RegisterEvent("READY_CHECK_CONFIRM")
			readyCheckFrame:RegisterEvent("READY_CHECK_FINISHED")
		end
	end
end

IRF2:RegisterObjectModule("ReadyCheck", CreateReadyCheckFrame)