local IC = InvenCraftInfo2

local _G = _G
local pairs = _G.pairs
local select = _G.select
local GetAddOnMetadata = _G.GetAddOnMetadata

local skill_format = "|cff33ccff%s(%s%s)|r"
local reagent_format = "|cff33ccff:|r %s"
local trainer_format = "|cffffaaff"..TRADESKILL_SERVICE_LEARN..":|r %s"
local drop_format = "|cffffaaff"..TRADESKILL_SERVICE_LEARN.."(%s):|r %s"
local craftable_format = "|cff20ff20제조 가능:|r %s"
local use_format = "|cff1e90ff"..USE..":|r %s"
local factionOnly_format = ", "..RACE_CLASS_ONLY
local delimiter, endl = ",", "\n"
local spells, skill, req, racefaction = {}
local tocCnt

local function checkLoadDB(toc, id)
	if IC.GetLinkCode then
		return true
	elseif (IC.db.title or IC.db.reagent or IC.db.craftable or IC.db.drop or IC.db.use) and GetAddOnMetadata("InvenCraftInfo2_UI", toc..id) then
		return IC:LoadDB()
	else
		return nil
	end
end

local function addTitle(tooltip, id, reagent)
	if id then
		skill, req, faction = IC:GetRequire(id)
		if skill then
			skill = skill_format:format(IC.skillName[skill], req > 0 and req or "???", faction and factionOnly_format:format(faction) or "")
			reagent = reagent and IC:GetReagent(id)
			if reagent then
				skill = skill..reagent_format:format(reagent)
			end
			tooltip:AddLine(skill, 1, 1, 1, skill:find(","))
		end
	end
end

local function setSpellTooltip(tooltip, id, title, reagent, craftable, drop, faction)
	if title then
		addTitle(tooltip, id, reagent)
	end
	if drop then
		drop = IC:IsTrainerSpell(id)
		if drop then
			tooltip:AddLine(trainer_format:format(MINIMAP_TRACKING_TRAINER_PROFESSION), 1, 1, 1, nil)
		else
			drop = IC:GetDiscoveryText(id)
			if drop then
				tooltip:AddLine(drop_format:format("발견", drop), 1, 1, 1, drop:find(","))
			end
			drop = IC:GetDropText(id)
			if drop then
				tooltip:AddLine(drop_format:format(LOOT, drop), 1, 1, 1, drop:find(","))
			end
			drop = IC:GetSellText(id)
			if drop then
				tooltip:AddLine(drop_format:format(TUTORIAL_TITLE20, drop), 1, 1, 1, drop:find(","))
			end
			if faction and not(drop and drop:find("만 구입 가능$")) then
				faction = IC:GetFactionText(id)
				if faction then
					tooltip:AddLine(drop_format:format(REPUTATION, faction), 1, 1, 1, faction:find(","))
				end
			end
			drop = IC:GetQuestText(id)
			if drop then
				tooltip:AddLine(drop_format:format(QUESTS_LABEL, drop), 1, 1, 1, drop:find(","))
			end
		end
	end
	if craftable then
		craftable = IC:HasSkillText(id)
		if craftable then
			tooltip:AddLine(craftable_format:format(craftable), nil, nil, nil, craftable:find(","))
		end
	end
end

local function setItemTooltip(tooltip, id, title, reagent, craftable, drop, use)
	spells[1] = IC:GetRecipeSkillID(id)
	if spells[1] then
		-- 도안
		setSpellTooltip(tooltip, spells[1], true, nil, craftable, drop, nil)
		spells[1] = nil
	else
		-- 완성품
		spells[1], spells[2], spells[3], spells[4], spells[5], spells[6], spells[7], spells[8], spells[9], spells[10] = IC:GetItemSkillID(id)
		for p, v in ipairs(spells) do
			setSpellTooltip(tooltip, v, title, reagent, craftable, drop, true)
			spells[p] = nil
		end
	end
	if use then
		use = IC:GetItemUse(id)
		if use then
			tooltip:AddLine(use_format:format(use), nil, nil, nil, 1)
		end
	end
end

function IC:ProcessTooltip(tooltip, name, link, id)
	if id < 0 then
		if checkLoadDB("X-Color-Spell-", -id) then
			setSpellTooltip(tooltip, -id, self.db.title, nil, self.db.craftable, self.db.drop, self.db.use, nil)
		end
	elseif id > 0 then
		if checkLoadDB("X-Color-Item-", id) then
			setItemTooltip(tooltip, id, self.db.title, self.db.reagent, self.db.craftable, self.db.drop, self.db.use, self.db.drop)
		end
	end
end