InvenSimulatorUI.sideTab = {}

local _G = _G
local pairs = _G.pairs
local ipairs = _G.ipairs
local select = _G.select
local GetSpellInfo = _G.GetSpellInfo
local visibleCount

local function tabOnEnter(self)
	if type(self.tooltip) == "number" then
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		GameTooltip:SetHyperlink("spell:"..self.tooltip)
		GameTooltip:Show()
	elseif self.tooltip then
		GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		GameTooltip:AddLine(self.tooltip)
		GameTooltip:Show()
	else
		GameTooltip:Hide()
	end
end

local function createMenu()
	InvenSimulatorUI.sideTab.menu = CreateFrame("Frame", "InvenSimulatorSideTabMenu", InvenSimulatorUI)
	InvenSimulatorUI.sideTab.menu:EnableMouse(true)
	InvenSimulatorUI.sideTab.menu:SetToplevel(true)
	InvenSimulatorUI.sideTab.menu:SetBackdrop({
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = 1, tileSize = 16, edgeSize = 16,
		insets = { left = 5, right = 5, top = 5, bottom = 5 },
	})
	InvenSimulatorUI.sideTab.menu:SetBackdropColor(0, 0, 0)
	InvenSimulatorUI.sideTab.menu:SetBackdropBorderColor(1, 1, 1)
	InvenSimulatorUI.sideTab.menu:SetWidth(48)
	InvenSimulatorUI.sideTab.menu.buttons = {}
	InvenSimulatorUI.sideTab.menu:SetScript("OnHide", function(self) self.parent = nil end)
	InvenSimulatorUI.sideTab.menu:SetScript("OnUpdate", function(self)
		if IsMouseButtonDown() then
			self.focus = GetMouseFocus()
			self.focusName = self.focus:GetName()
			if not(self.focusName and self.focusName:find("^InvenSimulatorSideTab")) then
				self:Hide()
			end
		end
	end)
end

local function menuButtonOnClick(self)
	if self:GetParent().parent.tooltip ~= self.tooltip then
		if type(self.tooltip) == "number" then
			InvenSimulatorUI.db.stance[self:GetParent().parent:GetID()] = self.tooltip
		else
			InvenSimulatorUI.db.stance[self:GetParent().parent:GetID()] = nil
		end
		InvenSimulatorUI:Calculate()
	else
		self:GetParent():Hide()
	end
end

local function createMenuButton(id)
	InvenSimulatorUI.sideTab.menu.buttons[id] = CreateFrame("Button", "InvenSimulatorSideTabMenu"..id, InvenSimulatorUI.sideTab.menu)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetID(id)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetWidth(32)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetHeight(32)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetNormalTexture(InvenSimulatorUI.playerTexture)
	InvenSimulatorUI.sideTab.menu.buttons[id]:GetNormalTexture():SetTexCoord(0.07, 0.93, 0.07, 0.93)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
	InvenSimulatorUI.sideTab.menu.buttons[id].border = InvenSimulatorUI.sideTab.menu.buttons[id]:CreateTexture(nil, "OVERLAY")
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetTexture("Interface\\Buttons\\UI-ActionButton-Border")
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetVertexColor(0, 1, 0)
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetBlendMode("ADD")
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetAlpha(0.75)
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetWidth(64)
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetHeight(64)
	InvenSimulatorUI.sideTab.menu.buttons[id].border:SetPoint("CENTER", 0, 1)
	if id == 1 then
		InvenSimulatorUI.sideTab.menu.buttons[id]:SetPoint("TOP", InvenSimulatorUI.sideTab.menu, "TOP", 0, -8)
	else
		InvenSimulatorUI.sideTab.menu.buttons[id]:SetPoint("TOP", InvenSimulatorUI.sideTab.menu.buttons[id - 1], "BOTTOM", 0, -2)
	end
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetScript("OnClick", menuButtonOnClick)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetScript("OnEnter", tabOnEnter)
	InvenSimulatorUI.sideTab.menu.buttons[id]:SetScript("OnLeave", GameTooltip_Hide)
end

local function tabOnClick(self)
	if not InvenSimulatorUI.sideTab.menu then
		createMenu()
	end
	if InvenSimulatorUI.sideTab.menu.parent == self then
		InvenSimulatorUI.sideTab.menu:Hide()
		return
	end
	InvenSimulatorUI.sideTab.menu.parent = self
	InvenSimulatorUI.sideTab.menu:ClearAllPoints()
	InvenSimulatorUI.sideTab.menu:SetPoint("TOPLEFT", self, "TOPRIGHT", 3, 7)
	InvenSimulatorUI:ShowTop(InvenSimulatorUI.sideTab.menu)
	for i = 1, #self.list do
		if not InvenSimulatorUI.sideTab.menu.buttons[i] then
			createMenuButton(i)
		end
		InvenSimulatorUI.sideTab.menu.buttons[i].tooltip = self.list[i]
		if type(self.list[i]) == "number" then
			InvenSimulatorUI.sideTab.menu.buttons[i]:SetNormalTexture(select(3, GetSpellInfo(self.list[i])) or nil)
			if InvenSimulatorUI.db.stance[self:GetID()] == self.list[i] then
				InvenSimulatorUI.sideTab.menu.buttons[i].border:Show()
			else
				InvenSimulatorUI.sideTab.menu.buttons[i].border:Hide()
			end
		else
			InvenSimulatorUI.sideTab.menu.buttons[i]:SetNormalTexture(self.defalutIcon)
			if InvenSimulatorUI.db.stance[self:GetID()] then
				InvenSimulatorUI.sideTab.menu.buttons[i].border:Hide()
			else
				InvenSimulatorUI.sideTab.menu.buttons[i].border:Show()
			end
		end
		InvenSimulatorUI.sideTab.menu.buttons[i]:Show()
		InvenSimulatorUI.sideTab.menu.buttons[i]:SetFrameLevel(InvenSimulatorUI.sideTab.menu:GetFrameLevel() + 1)
	end
	for i = #self.list + 1, #InvenSimulatorUI.sideTab.menu.buttons do
		InvenSimulatorUI.sideTab.menu.buttons[i]:Hide()
	end
	InvenSimulatorUI.sideTab.menu:SetHeight(48 + (#self.list - 1) * 34)
end

function InvenSimulatorUI:RegisterSideTab(id, defalutIcon, num, ...)
	if id == 1 then
		visibleCount = 0
	end
	if self.sideTab.menu then
		self.sideTab.menu:Hide()
	end
	if not self.sideTab[id] then
		self.sideTab[id] = CreateFrame("Button", "InvenSimulatorSideTab"..id, InvenSimulatorUI, "SpellBookSkillLineTabTemplate")
		self.sideTab[id]:SetID(id)
		self.sideTab[id]:SetScript("OnClick", tabOnClick)
		self.sideTab[id]:SetScript("OnEnter", tabOnEnter)
		self.sideTab[id]:SetScript("OnLeave", GameTooltip_Hide)
		self.sideTab[id]:SetNormalTexture(InvenSimulatorUI.playerTexture)
		self.sideTab[id]:GetNormalTexture():SetTexCoord(0.07, 0.93, 0.07, 0.93)
		self.sideTab[id].list = {}
	end
	local numElement, check = 0
	for i = 1, num do
		if select(i, ...) then
			numElement = numElement + 1
			self.sideTab[id].list[numElement] = select(i, ...)
			if self.sideTab[id].list[numElement] == self.db.stance[id] then
				check = true
			elseif type(self.sideTab[id].list[numElement]) == "string" and self.sideTab[id].list[numElement] ~= "인간형" then
				self.sideTab[id].list[numElement] = self.sideTab[id].list[numElement].." 없음"
			end
		end
	end
	for i = numElement + 1, #self.sideTab[id].list do
		self.sideTab[id].list[i] = nil
	end
	if numElement == 0 or numElement == 1 then
		self.sideTab[id]:Hide()
		self.db.stance[id] = nil
		return
	end
	if not check then
		if type(self.sideTab[id].list[1]) == "string" then
			self.db.stance[id] = nil
		else
			if GetNumShapeshiftForms() > 0 then
				local csid = GetShapeshiftForm()
				if csid and csid > 0 then
					local csName = select(2, GetShapeshiftFormInfo(csid))
					for i = 1, numElement do
						if csName == GetSpellInfo(self.sideTab[id].list[i]) then
							csid = true
							self.db.stance[id] = self.sideTab[id].list[i]
							break
						end
					end
					if csid ~= true then
						self.db.stance[id] = self.sideTab[id].list[1]
					end
				else
					self.db.stance[id] = self.sideTab[id].list[1]
				end
			else
				self.db.stance[id] = self.sideTab[id].list[1]
			end
		end
	end
	self.sideTab[id].defalutIcon = defalutIcon or self.playerTexture
	if self.db.stance[id] then
		self.sideTab[id].tooltip = self.db.stance[id]
		self.sideTab[id]:SetNormalTexture(select(3, GetSpellInfo(self.db.stance[id])) or nil)
	else
		self.sideTab[id].tooltip = self.sideTab[id].list[1]
		self.sideTab[id]:SetNormalTexture(self.sideTab[id].defalutIcon)
	end
	self.sideTab[id]:ClearAllPoints()
	self.sideTab[id]:SetPoint("TOPLEFT", InvenSimulatorUI, "TOPRIGHT", 0, -(60 + visibleCount * 49))
	self.sideTab[id]:Show()
	visibleCount = visibleCount + 1
end

function InvenSimulatorUI:HasStance(stance)
	for _, stanceSpell in pairs(self.db.stance) do
		if GetSpellInfo(stanceSpell) == stance then
			return true
		end
	end
	return nil
end