local _G = _G
local ipairs = _G.ipairs
local select = _G.select
local UnitBuff = _G.UnitBuff
local GetSpellInfo = _G.GetSpellInfo
local IRF3 = _G[...]
local buffList, buffIcon, buffFriends, texture
local spells = {}
local delimiter = ","

local function setIcon(icon, texture)
	if texture then
		icon:SetSize(IRF3.db.units.buffIconSize, IRF3.db.units.buffIconSize)
		icon:SetTexture(texture)
		icon:Show()
	else
		icon:SetSize(0.001, 0.001)
		icon:Hide()
	end
end

local function checkBuff(unit, name)
	spells[1], spells[2], spells[3] = delimiter:split(name)
	for _, spell in ipairs(spells) do
		name = select(3, UnitBuff(unit, spell))
		if name then
			return name
		end
	end
	return nil
end

local function updateBuff(self)
	if self.isOffline or self.isGhost or self.isDead then
		for i = 1, #InvenRaidFrames3CharDB.classBuff do
			setIcon(self["buffIcon"..i])
		end
	else
		for i, use in ipairs(InvenRaidFrames3CharDB.classBuff) do
			if buffFriends[i] and use ~= 0 then
				texture = checkBuff(self.displayedUnit, buffFriends[i])
				if texture then
					setIcon(self["buffIcon"..i], use == 2 and buffIcon[i])
				else
					setIcon(self["buffIcon"..i], use == 1 and buffIcon[i])
				end
			else
				setIcon(self["buffIcon"..i])
			end
		end
	end
end

local function updateBuffPaladin(self)
	if InvenRaidFrames3CharDB.classBuff[1] ~= 0 and not(self.isOffline or self.isGhost or self.isDead) then

	else
		for i = 1, #InvenRaidFrames3CharDB.classBuff do
			setIcon(self["buffIcon"..i])
		end
	end
end

function InvenRaidFrames3Member_UpdateBuffs()

end

local check = { [0] = 0, [1] = 1, [2] = 2 }
local function setting(b1, b2)
	if type(InvenRaidFrames3CharDB.classBuff) == "table" then
		for p in pairs(InvenRaidFrames3CharDB.classBuff) do
			if not((p == 1 and b1) or (p == 2 and b2)) then
				InvenRaidFrames3CharDB.classBuff[p] = nil
			end
		end
		InvenRaidFrames3CharDB.classBuff[1] = InvenRaidFrames3CharDB.classBuff[1] or b1
		InvenRaidFrames3CharDB.classBuff[2] = InvenRaidFrames3CharDB.classBuff[2] or b2
	else
		InvenRaidFrames3CharDB.classBuff = { b1, b2 }
	end
end

local function setBuffFriends()
	if buffList then
		buffFriends = buffFriends or {}
		for i = 1, 2 do
			if buffList[i] then
				if buffFriends[i] then
					buffFriends[i] = buffList[i]..","..buffFriends[i]
				else
					buffFriends[i] = buffList[i]
				end
			else
				buffFriends[i] = nil
			end
		end
	end
end

function IRF3:SetupClassBuff()
	self.SetupClassBuff = nil
	if self.playerClass == "PRIEST" then
		setting(1, 0)
		buffList, buffIcon = {}, {}
		buffList[1], _, buffIcon[1] = GetSpellInfo(21562)
		buffList[2], _, buffIcon[2] = GetSpellInfo(27683)
		InvenRaidFrames3Member_UpdateBuffs = updateBuff
		setBuffFriends()
	elseif self.playerClass == "MAGE" then
		setting(1)
		buffList, buffIcon = {}, {}
		buffList[1], _, buffIcon[1] = GetSpellInfo(1459)
		buffList[1] = buffList[1]..","..GetSpellInfo(61316)
		InvenRaidFrames3Member_UpdateBuffs = updateBuff
		setBuffFriends()
	elseif self.playerClass == "WARLOCK" then
		setting(2, 0)
		buffList, buffIcon = {}, {}
		buffList[1], _, buffIcon[1] = GetSpellInfo(20707)
		buffList[2], _, buffIcon[2] = GetSpellInfo(5697)
		InvenRaidFrames3Member_UpdateBuffs = updateBuff
		setBuffFriends()
	elseif self.playerClass == "DRUID" then
		setting(1)
		buffList, buffIcon = {}, {}
		buffList[1], _, buffIcon[1] = GetSpellInfo(1126)
		--buffFriends[1] = GetSpellInfo(20217)
		InvenRaidFrames3Member_UpdateBuffs = updateBuff
		setBuffFriends()
	elseif self.playerClass == "PALADIN" then
		setting(1)
		buffList, buffIcon = {}, {}
		buffList[1], _, buffIcon[1] = GetSpellInfo(19740)
		buffList[1] = buffList[1]..","..GetSpellInfo(20217)
		InvenRaidFrames3Member_UpdateBuffs = updateBuff
		setBuffFriends()
	else
		InvenRaidFrames3CharDB.classBuff = nil
	end

	check, setting, setBuffFriends = nil
end

function IRF3:GetBuff()
	return buffList, buffIcon
end