local _, ns = ...
 
local function GetBestkeystoneData()
	return ns['BestkeystoneData'][2]
end
local KEYSTONE_PATTERNS = {
	"(?<=\:)(.*?)(?=\:)",
	"keystone:%d+:(.-):(.-):(.-):(.-):(.-)",
	"item:158923:.-:.-:.-:.-:.-:.-:.-:.-:.-:.-:.-:.-:(.-):(.-):(.-):(.-):(.-):(.-)",
	"item:138019:.-:.-:.-:.-:.-:.-:.-:.-:.-:.-:.-:.-:(.-):(.-):(.-):(.-):(.-):(.-)",
}
--cffa335eeHitem:158923::::::::120:250:8257536:::248:10:9:7:13:16:::h[Mythic Keystone]hr
--cffa335eeHitem:158923::::::::120:250:8257536:::245:16:10:5:13:117:::h[Mythic Keystone]hr 
--cffa335eeHitem:158923::::::::120:250:8257536:::250:10:9:11:2:16:::h[Mythic Keystone]hr
--cffa335eeHitem:158923::::::::120:250:917504:::252:3:10:::h[Mythic Keystone]hr 

local function Gethex(class) 
	local rPerc, gPerc, bPerc, argbHex = GetClassColor(class)
	return argbHex
end


local function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={}
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        table.insert(t, str)
    end
    return t
end
local function getClass(classname)
	for i = 1, GetNumClasses() do
		local className, classFile, classID = GetClassInfo(i)
		if classFile == classname then
			return classID
		end
	end
	return 0
end


local function OnSetItem(tooltip)
	local CreatedPlus10Days = ns['BestkeystoneData'][1][1] + 907200
	local now = time()

	
	local BestkeystoneData = GetBestkeystoneData()
	
	local _, link = tooltip:GetItem()
	local _link = link:gsub("|", "")
	
	local s = split(_link,':')
	local linkinfo = {}
	for i = 1, #s do
		local g = split(s[i],'h')[1]
		linkinfo[#linkinfo + 1] = g
	end
	
	
	local _, itemid, dungeon, lvl, a1, a2, a3, a4 = {}
	if linkinfo[1] == 'cffa335eeHkeystone' then
		_, itemid, dungeon, lvl, a1, a2, a3, a4 = linkinfo[1], linkinfo[2], linkinfo[3], linkinfo[4], linkinfo[5], linkinfo[6], linkinfo[7], linkinfo[8]
	else
		itemid, dungeon, lvl, a1, a2, a3, a4 = linkinfo[2], linkinfo[6], linkinfo[7], linkinfo[8], linkinfo[9], linkinfo[10], linkinfo[11]
	end
	
	if type(link) ~= "string" then return end
	if CreatedPlus10Days > now then
		for week = 1, #BestkeystoneData do
			local data = BestkeystoneData[week]
		
			local saffixes = data[1]
			if tonumber(saffixes[1]) == tonumber(a1) and tonumber(saffixes[2]) == tonumber(a2) then
			--Current week
				tooltip:AddLine(" ")	
				local dungeons = data[2]
				for	i = 1, #dungeons do 
					if tonumber(dungeons[i][1]) == tonumber(dungeon) then
						tooltip:AddDoubleLine("|cFFFFFFFF" .. "Dungeon rank: ", "|cFFFFFFFF" .. i)	
						tooltip:AddDoubleLine("|cFFFFFFFF" .."Dungeon success rate: ", "|cFFFFFFFF" ..dungeons[i][2] .. "%")	
					end
				end
				tooltip:AddLine(" ")	
	
				local topDpsClasses = ''
				for i = 1, 3 do
					local class_id = data[3][i]
					local className, classFile, classID = GetClassInfo(class_id)
					
					topDpsClasses = topDpsClasses .. "|c" .. Gethex(classFile) ..className .. "|r,"		
				end
				--tooltip:AddLine("|cFFFFFFFF" .."Top DPS Class: |r" .. topDpsClasses)	
				
				
				
				local healers = {}
				local damagers = {}
				local tanks = {}
				
				for i = 1, #data[4] do
					local spec_id = data[4][i]
					local id, name, description, icon, role, class = GetSpecializationInfoByID(spec_id)			
	
					if role == 'DAMAGER' then
						damagers[#damagers + 1] = id
					elseif role == 'HEALER' then
						healers[#healers + 1] = id
					elseif role == 'TANK' then
						tanks[#tanks + 1] = id
					end
				end
				
				if tanks[1] then 
					local id, name, description, icon, role, class = GetSpecializationInfoByID(tanks[1])
					local classid = getClass(class)
					local className, classFile, _ = GetClassInfo(classid)
	
					tooltip:AddDoubleLine("|cFFFFFFFF" .. "Top Tank:", "|c".. Gethex(classFile) .. name .. " " .. className .. "|r")	
				end
				
				if healers[1] then 
					local id, name, description, icon, role, class = GetSpecializationInfoByID(healers[1])
					local className, classFile, _ = GetClassInfo(getClass(class))
					
					tooltip:AddDoubleLine("|cFFFFFFFF" .. "Top Healer:", "|c" .. Gethex(classFile) .. name .. " " .. className)	
				end
				
				if damagers[1] and damagers[2] and damagers[3] then 
					local id1, name1, description1, icon1, role1, class1 = GetSpecializationInfoByID(damagers[1])
					local id2, name2, description2, icon2, role2, class2 = GetSpecializationInfoByID(damagers[2])
					local id3, name3, description3, icon3, role3, class3 = GetSpecializationInfoByID(damagers[3])
					
					local className1, classFile1, _ = GetClassInfo(getClass(class1))
					local className2, classFile2, _ = GetClassInfo(getClass(class2))
					local className3, classFile3, _ = GetClassInfo(getClass(class3))
					
					--tooltip:AddDoubleLine("|cFFFFFFFF" .. "Top DPS Spec:", "|c".. Gethex(classFile1) .. name1 .. "|r,|c".. Gethex(classFile2).. name2 .. "|r,|c".. Gethex(classFile3).. name3 )	
				end
					
				
				local teamcomp = data[5][1]
				if teamcomp then
					local id1, name1, description1, icon1, role1, class1 = GetSpecializationInfoByID(teamcomp[1])
					local id2, name2, description2, icon2, role2, class2 = GetSpecializationInfoByID(teamcomp[2])
					local id3, name3, description3, icon3, role3, class3 = GetSpecializationInfoByID(teamcomp[3])
					local id4, name4, description4, icon4, role4, class4 = GetSpecializationInfoByID(teamcomp[4])
					local id5, name5, description5, icon5, role5, class5 = GetSpecializationInfoByID(teamcomp[5])
				
					local className1, classFile1, _ = GetClassInfo(getClass(class1))
					local className2, classFile2, _ = GetClassInfo(getClass(class2))
					local className3, classFile3, _ = GetClassInfo(getClass(class3))
					local className4, classFile4, _ = GetClassInfo(getClass(class4))
					local className5, classFile5, _ = GetClassInfo(getClass(class5))
				
						
					tooltip:AddDoubleLine("|cFFFFFFFF" .. "Top team setup:", "|c".. Gethex(classFile1) .. name1 .. "|r,|c".. Gethex(classFile2).. name2 .. "|r,|c".. Gethex(classFile3).. name3 .. "|r,|c".. Gethex(classFile4) .. name4 .. "|r,|c".. Gethex(classFile5) .. name5 )	
				end
				
				local healertankcomp = data[6][1]
				if healertankcomp then
					local id1, name1, description1, icon1, role1, class1 = GetSpecializationInfoByID(healertankcomp[1])
					local id2, name2, description2, icon2, role2, class2 = GetSpecializationInfoByID(healertankcomp[2])
					
					local className1, classFile1, _ = GetClassInfo(getClass(class1))
					local className2, classFile2, _ = GetClassInfo(getClass(class2))
						
					tooltip:AddDoubleLine("|cFFFFFFFF" .. "Top tank/healer:", "|c".. Gethex(classFile1) .. name1 .. " " .. className1 .. "|r,|c".. Gethex(classFile2).. name2 .. " " .. className2)	
				end
				
				local dpscomp = data[7][1]
				if dpscomp then
					local id1, name1, description1, icon1, role1, class1 = GetSpecializationInfoByID(dpscomp[1])
					local id2, name2, description2, icon2, role2, class2 = GetSpecializationInfoByID(dpscomp[2])
					local id3, name3, description3, icon3, role3, class3 = GetSpecializationInfoByID(dpscomp[3])
						
					local className1, classFile1, _ = GetClassInfo(getClass(class1))
					local className2, classFile2, _ = GetClassInfo(getClass(class2))
					local className3, classFile3, _ = GetClassInfo(getClass(class3))
					
					--tooltip:AddDoubleLine("|cFFFFFFFF" .. "Top dps setup:", "|c" .. Gethex(classFile1) .. name1 .. "|r,|c".. Gethex(classFile2).. name2 .. "|r,|c".. Gethex(classFile3).. name3 .. "|r")	
				end		
				tooltip:AddLine(" ")	
			end
		end
		tooltip:Show()
	else
		tooltip:AddLine(" ")
		tooltip:AddLine("|cffff0000".."Data is outdated, please update addon")	
		tooltip:Show()
	end
end

GameTooltip:HookScript("OnTooltipSetItem", OnSetItem)
ItemRefTooltip:HookScript("OnTooltipSetItem", OnSetItem)
