local IC = InvenCraftInfo2

IC.playerFaction = UnitFactionGroup("player") == "Horde" and 1 or 2
IC.spellFaction = {
	[66034] = 1,
	[66035] = 1,
	[66036] = 1,
	[66037] = 1,
	[66038] = 1,
	[60866] = 1,
	[67130] = 1,
	[67131] = 1,
	[67132] = 1,
	[67133] = 1,
	[67134] = 1,
	[67135] = 1,
	[67136] = 1,
	[67137] = 1,
	[67138] = 1,
	[67139] = 1,
	[67140] = 1,
	[67141] = 1,
	[67142] = 1,
	[67143] = 1,
	[67144] = 1,
	[67145] = 1,
	[67146] = 1,
	[67147] = 1,
	[86654] = 1,
	[62051] = 2,
	[62049] = 2,
	[62050] = 2,
	[62044] = 2,
	[62045] = 2,
	[60867] = 2,
	[67064] = 2,
	[67065] = 2,
	[67066] = 2,
	[67079] = 2,
	[67080] = 2,
	[67081] = 2,
	[67082] = 2,
	[67083] = 2,
	[67084] = 2,
	[67085] = 2,
	[67086] = 2,
	[67087] = 2,
	[67091] = 2,
	[67092] = 2,
	[67093] = 2,
	[67094] = 2,
	[67095] = 2,
	[67096] = 2,
	[89244] = 2,
}

IC.sideTabs = {}

local _G = _G
local select = _G.select

local ignore = {
	[2336] = true,
	[2671] = true,
	[7636] = true,
	[8366] = true,
	[8368] = true,
	[8778] = true,
	[9942] = true,
	[10550] = true,
	[12062] = true,
	[12063] = true,
	[12068] = true,
	[12083] = true,
	[12087] = true,
	[12090] = true,
	[12720] = true,
	[12722] = true,
	[12900] = true,
	[12904] = true,
	[16960] = true,
	[16965] = true,
	[16967] = true,
	[16980] = true,
	[16986] = true,
	[16987] = true,
	[17579] = true,
	[19106] = true,
	[22813] = true,
	[22815] = true,
	[24266] = true,
	[25614] = true,
	[26918] = true,
	[26920] = true,
	[28021] = true,
	[28327] = true,
	[30342] = true,
	[30343] = true,
	[30549] = true,
	[30561] = true,
	[30573] = true,
	[31461] = true,
	[32810] = true,
	[36665] = true,
	[36667] = true,
	[36668] = true,
	[36669] = true,
	[36670] = true,
	[36672] = true,
	[43676] = true,
	[44612] = true,
	[54020] = true,
	[55243] = true,
	[56048] = true,
	[57231] = true,
	[60244] = true,
	[62257] = true,
	[65454] = true,
	[65730] = true,
	[67790] = true,
}

local function ignorefunc(spell)
	if ignore[spell] then
		return nil
	elseif IC.spellFaction[spell] and IC.spellFaction[spell] ~= IC.playerFaction then
		return nil
	else
		return true
	end
end

local checkShown = CreateFrame("Frame")
checkShown:RegisterEvent("TRADE_SKILL_SHOW")
checkShown:SetScript("OnEvent", function(self) self.show = true end)

local function sideTabOnClick(tab)
	IC.linkedID = tab:GetID()
	IC.currentSkillCode = tab.code
	checkShown.show = nil
	IC.tooltip:SetHyperlink(IC.linkCode:format(tab.id, tab.code))
	if not checkShown.show then
		IC.linkedID, IC.currentSkillID, IC.currentSkillCode = nil
		IC:AddMessage("버전 업데이트가 필요합니다.")
		CloseTradeSkill()
		HideUIPanel(TradeSkillFrame)
	end
end

local index, prev = 0

for i = 1, #IC.skillID do
	if IC.needSideTab[i] then
		index = index + 1
		IC.sideTabs[i] = CreateFrame("CheckButton", IC:GetName().."SideTab"..index, TradeSkillFrame, "SpellBookSkillLineTabTemplate")
		IC.sideTabs[i]:SetID(i)
		IC.sideTabs[i]:SetScale(0.9)
		IC.sideTabs[i].tooltip = IC.skillName[i]
		IC.sideTabs[i].id = IC.skillID[i]
		IC.sideTabs[i].code, IC.sideTabs[i].num = IC:GetLinkCode(i, ignorefunc)
		IC.sideTabs[i]:RegisterForClicks("LeftButtonUp")
		IC.sideTabs[i]:SetScript("OnClick", sideTabOnClick)
		IC.sideTabs[i]:SetNormalTexture(select(3, GetSpellInfo(IC.skillID[i])) or "")
		IC.sideTabs[i]:GetNormalTexture():SetTexCoord(0.07, 0.93, 0.07, 0.93)
		IC.sideTabs[i]:SetPoint("TOP", prev, "BOTTOM", 0, -17)
		IC.sideTabs[i]:Show()
		prev = IC.sideTabs[i]
	end
end
IC.sideTabs[1]:ClearAllPoints()
IC.sideTabs[1]:SetPoint("TOPLEFT", TradeSkillFrame, "TOPRIGHT", 2, -38)

if IC.CheckLinkCode then
	IC.ignoreSpells = ignore
end

ignore = CreateFrame("Frame", nil, TradeSkillFrame)
ignore:EnableMouse(true)
ignore:SetFrameLevel(TradeSkillFrame:GetFrameLevel() - 1)
ignore:SetPoint("TOPLEFT", IC.sideTabs[1], 0, 0)
ignore:SetPoint("BOTTOMRIGHT", prev, 0, 0)

ignore, ignorefunc, index, prev = nil

