local IC = InvenCraftInfo2

local _G = _G
local ipairs = _G.ipairs
local select = _G.select
local wipe = _G.table.wipe
local GetSpellLink = _G.GetSpellLink

IC.bottomTabs = {}
IC.bottomTabsID = {}

local function tabOnClick(tab)
	IC.skillButton[tab:GetText()]:Click()
end

local function tabPostClick(tab)
	if not TradeSkillFrame:IsShown() then
		IC.skillButton[tab:GetText()]:Click()
	end
end

for i = 1, 5 do
	IC.bottomTabs[i] = CreateFrame("Button", IC:GetName().."Tab"..i, IC, "CharacterFrameTabButtonTemplate")
	IC.bottomTabs[i]:Hide()
	IC.bottomTabs[i]:SetScript("OnClick", tabOnClick)
	IC.bottomTabs[i]:SetScript("PostClick", tabPostClick)
	IC.bottomTabs[i]:RegisterForClicks("LeftButtonUp")
	IC.bottomTabs[i]:SetID(i)
	IC.bottomTabs[i]:SetPoint("LEFT", IC.bottomTabs[i - 1], "RIGHT", -15, 0)
end
IC.bottomTabs[1]:ClearAllPoints()
IC.bottomTabs[1]:SetPoint("BOTTOMLEFT", TradeSkillFrame, "BOTTOMLEFT", 6, -31)

function IC:UpdateBottomTabs()
	self.numTabs = 0
	wipe(IC.bottomTabsID)
	for i, name in ipairs(self.skillName) do
		if GetSpellLink(name) then
			self.numTabs = self.numTabs + 1
			self.bottomTabs[self.numTabs]:SetText(name)
			self.bottomTabs[self.numTabs]:Show()
			PanelTemplates_TabResize(self.bottomTabs[self.numTabs], 0)
			self.bottomTabsID[name] = self.numTabs
		end
	end
	for i = self.numTabs + 1, #self.bottomTabs do
		self.bottomTabs[i]:Hide()
	end
end

IC:UpdateBottomTabs()