-- 시전바 모듈
local IRF2 = InvenRaidFrames2

local _G = _G
local unpack = _G.unpack
local select = _G.select
local GetTime = _G.GetTime
local UnitCastingInfo = _G.UnitCastingInfo
local UnitChannelInfo = _G.UnitChannelInfo

local firstRun, startTime, endTime, isChannel = true
local handler = CreateFrame("Frame")

local function GetCastingInfo(unit)
	startTime, endTime = select(5, UnitCastingInfo(unit))
	if startTime then
		return startTime / 1000, endTime / 1000, nil
	else
		startTime, endTime = select(5, UnitChannelInfo(unit))
		if startTime then
			return startTime / 1000, endTime / 1000, true
		end
	end
	return nil, nil, nil
end

local function castingBarOnUpdate(object)
	if object.isChannel then
		object:SetValue(object.endTime - GetTime() + object.startTime)
	else
		object:SetValue(GetTime())
	end
end

local function createCastingBar(object)
	if not object.castingBar then
		object.castingBar = CreateFrame("StatusBar", nil, object)
		object.castingBar:SetFrameLevel(IRF2.db.frameLevel.castingBar)
		object.castingBar:Hide()
		object.castingBar:SetScript("OnUpdate", castingBarOnUpdate)
		object.castingBar:SetBackdrop(object:GetBackdrop())
		object.castingBar:SetBackdropColor(0, 0, 0, 0.1)
		object.castingBar:SetStatusBarTexture(object.health:GetStatusBarTexture():GetTexture())
		object.castingBar:SetStatusBarColor(unpack(IRF2.db.castingBar.color))
		if IRF2.db.castingBar.pos == "상단" then
			object.castingBar:SetPoint("TOPLEFT", object, "TOPLEFT", 0, 0)
			object.castingBar:SetPoint("BOTTOMRIGHT", object, "TOPRIGHT", 0, -IRF2.db.castingBar.height)
			object.castingBar:SetOrientation("HORIZONTAL")
		elseif IRF2.db.castingBar.pos == "하단" then
			object.castingBar:SetPoint("TOPLEFT", object, "BOTTOMLEFT", 0, IRF2.db.castingBar.height)
			object.castingBar:SetPoint("BOTTOMRIGHT", object, "BOTTOMRIGHT", 0, 0)
			object.castingBar:SetOrientation("HORIZONTAL")
		elseif IRF2.db.castingBar.pos == "좌측" then
			object.castingBar:SetPoint("TOPLEFT", object, "TOPLEFT", 0, 0)
			object.castingBar:SetPoint("BOTTOMRIGHT", object, "BOTTOMLEFT", IRF2.db.castingBar.height, 0)
			object.castingBar:SetOrientation("VERTICAL")
		elseif IRF2.db.castingBar.pos == "우측" then
			object.castingBar:SetPoint("TOPLEFT", object, "TOPRIGHT", -IRF2.db.castingBar.height, 0)
			object.castingBar:SetPoint("BOTTOMRIGHT", object, "BOTTOMRIGHT", 0, 0)
			object.castingBar:SetOrientation("VERTICAL")
		end
	end
end

local function objectCastingUpdate(object)
	if IRF2.db.castingBar.use and object.unit then
		createCastingBar(object)
		object.castingBar.startTime, object.castingBar.endTime, object.castingBar.isChannel = GetCastingInfo(object.vehicleUnit or object.unit)
		if object.castingBar.startTime then
			object.castingBar:SetMinMaxValues(object.castingBar.startTime, object.castingBar.endTime)
			object.castingBar:Show()
		else
			object.castingBar:Hide()
		end
	elseif object.castingBar then
		object.castingBar:Hide()
	end
end

handler:SetScript("OnEvent", function(self, event, unit)
	self.object = IRF2:GetUnitToObject(unit)
	if self.object then
		objectCastingUpdate(self.object)
		if IRF2.units[unit] and IRF2.units[unit] ~= self.object then
			objectCastingUpdate(IRF2.units[unit])
		end
	end
end)

function IRF2:GetCastingBarHandler()
	return handler
end

local function castingBar(object)
	if IRF2.db.castingBar.use and firstRun then
		firstRun = nil
		handler:RegisterEvent("UNIT_SPELLCAST_START")
		handler:RegisterEvent("UNIT_SPELLCAST_STOP")
		handler:RegisterEvent("UNIT_SPELLCAST_DELAYED")
		handler:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START")
		handler:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE")
		handler:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP")
	end
	object.UpdateCastingBar = objectCastingUpdate
	objectCastingUpdate(object)
end

IRF2:RegisterObjectModule("CastingBar", castingBar)