--local aura_env = aura_env or {}
local CRESTS = {
[0]={minLevel=597,maxLevel=619,shortName="神勇石",color=HEIRLOOM_BLUE_COLOR},
[1]={minLevel=623,maxLevel=645,shortName="风化安德麦纹章",color=UNCOMMON_GREEN_COLOR,achieve=40942},
[2]={minLevel=636,maxLevel=658,shortName="蚀刻安德麦纹章",color=RARE_BLUE_COLOR,achieve=40943},
[3]={minLevel=649,maxLevel=665,shortName="符文安德麦纹章",color=ITEM_EPIC_COLOR,achieve=40944},
[4]={minLevel=662,maxLevel=678,shortName="鎏金安德麦纹章",color=ITEM_LEGENDARY_COLOR},
}

local UPGRADE_BONUSES = {
{id=11942,level=1,max=8,name="探索者",ilvl=597,crest=CRESTS[0]},
{id=11943,level=2,max=8,name="探索者",ilvl=600,crest=CRESTS[0]},
{id=11944,level=3,max=8,name="探索者",ilvl=603,crest=CRESTS[0]},
{id=11945,level=4,max=8,name="探索者",ilvl=606,crest=CRESTS[0]},
{id=11946,level=5,max=8,name="探索者",ilvl=610,crest=CRESTS[0]},
{id=11947,level=6,max=8,name="探索者",ilvl=613,crest=CRESTS[0]},
{id=11948,level=7,max=8,name="探索者",ilvl=616,crest=CRESTS[0]},
{id=11949,level=8,max=8,name="探索者",ilvl=619},
{id=11951,level=1,max=8,name="冒险者",ilvl=610,crest=CRESTS[0]},
{id=11952,level=2,max=8,name="冒险者",ilvl=613,crest=CRESTS[0]},
{id=11953,level=3,max=8,name="冒险者",ilvl=616,crest=CRESTS[0]},
{id=11954,level=4,max=8,name="冒险者",ilvl=619,crest=CRESTS[1]},
{id=11955,level=5,max=8,name="冒险者",ilvl=623,crest=CRESTS[1]},
{id=11956,level=6,max=8,name="冒险者",ilvl=626,crest=CRESTS[1]},
{id=11957,level=7,max=8,name="冒险者",ilvl=629,crest=CRESTS[1]},
{id=11950,level=8,max=8,name="冒险者",ilvl=632},
{id=11969,level=1,max=8,name="老兵",ilvl=623,crest=CRESTS[1]},
{id=11970,level=2,max=8,name="老兵",ilvl=626,crest=CRESTS[1]},
{id=11971,level=3,max=8,name="老兵",ilvl=629,crest=CRESTS[1]},
{id=11972,level=4,max=8,name="老兵",ilvl=632,crest=CRESTS[2]},
{id=11973,level=5,max=8,name="老兵",ilvl=636,crest=CRESTS[2]},
{id=11974,level=6,max=8,name="老兵",ilvl=639,crest=CRESTS[2]},
{id=11975,level=7,max=8,name="老兵",ilvl=642,crest=CRESTS[2]},
{id=11976,level=8,max=8,name="老兵",ilvl=645},
{id=11977,level=1,max=8,name="勇士",ilvl=636,crest=CRESTS[2]},
{id=11978,level=2,max=8,name="勇士",ilvl=639,crest=CRESTS[2]},
{id=11979,level=3,max=8,name="勇士",ilvl=642,crest=CRESTS[2]},
{id=11980,level=4,max=8,name="勇士",ilvl=645,crest=CRESTS[3]},
{id=11981,level=5,max=8,name="勇士",ilvl=649,crest=CRESTS[3]},
{id=11982,level=6,max=8,name="勇士",ilvl=652,crest=CRESTS[3]},
{id=11983,level=7,max=8,name="勇士",ilvl=655,crest=CRESTS[3]},
{id=11984,level=8,max=8,name="勇士",ilvl=658},
{id=11985,level=1,max=6,name="英雄",ilvl=649,crest=CRESTS[3]},
{id=11986,level=2,max=6,name="英雄",ilvl=652,crest=CRESTS[3]},
{id=11987,level=3,max=6,name="英雄",ilvl=655,crest=CRESTS[3]},
{id=11988,level=4,max=6,name="英雄",ilvl=658,crest=CRESTS[4]},
{id=11989,level=5,max=6,name="英雄",ilvl=662,crest=CRESTS[4]},
{id=11990,level=6,max=6,name="英雄",ilvl=665},
{id=11991,level=1,max=6,name="神话",ilvl=662,crest=CRESTS[4]},
{id=11992,level=2,max=6,name="神话",ilvl=665,crest=CRESTS[4]},
{id=11993,level=3,max=6,name="神话",ilvl=668,crest=CRESTS[4]},
{id=11994,level=4,max=6,name="神话",ilvl=671},
{id=11995,level=5,max=6,name="神话",ilvl=675},
{id=11996,level=6,max=6,name="神话",ilvl=678},
}

local UPGRADE_TIERS = {
["探索者"] = {minLevel=597,maxLevel=619,color=ITEM_POOR_COLOR},
["冒险者"] = {minLevel=610,maxLevel=632,color=WHITE_FONT_COLOR},
["老兵"] = {minLevel=623,maxLevel=645,color=UNCOMMON_GREEN_COLOR},
["勇士"] = {minLevel=636,maxLevel=658,color=RARE_BLUE_COLOR},
["英雄"] = {minLevel=649,maxLevel=665,color=ITEM_EPIC_COLOR},
["神话"] = {minLevel=662,maxLevel=678,color=ITEM_LEGENDARY_COLOR},
}

local SetValueToKey = function(list, field, valuesArray)
local newList = {}
for _,v in ipairs(list) do
local key = v
local data = true
if field then
key = v[field]
end
if tonumber(key) then
key = tonumber(key)
end
if valuesArray then
data = v
end
newList[key]=data
end
return newList
end
UPGRADE_BONUSES = SetValueToKey(UPGRADE_BONUSES, "id", true)

local localePatterns = {
["zhCN"] = "(.+)(%d+)/(%d+)$",
}

local GetRankInfoByLocale = function(text, locale)
local tier,current,total
if locale:find("zh") then
return text:match("([%u%l]+)(%d+)/(%d+)")
else
if locale == "ruRU" or locale == "koKR" then
_, tier, current, total = text:match(localePatterns[locale])
else
tier, current, total = text:match(localePatterns[locale] or localePatterns.enUS)
end
end
return tier, current, total
end

-- Tries to get rank info from the tooltip text
-- by comparing current rank and ilvl in UPGRADE_BONUSES
local GetRankInfo = function(item, text, locale)
local tier, current, total, bonusID = GetRankInfoByLocale(text, locale)
if tier and current and total and not UPGRADE_TIERS[tier] then --non-english text
for id, v in pairs(UPGRADE_BONUSES) do
if v.level == tonumber(current)
and v.max == tonumber(total)
and v.ilvl == item:GetCurrentItemLevel() then
tier = v.name
current = v.level
total = v.max
bonusID = v.id
end
end
end
return tier, current, total, bonusID
end

-- process tooltip data
local function ProcessTooltip(tooltip, data)
local itemName, itemLink = TooltipUtil.GetDisplayedItem(tooltip)
if not itemLink then return end

--Create ItemMixin from itemLink
local item = Item:CreateFromItemLink(itemLink)
if item:IsItemEmpty() then return end
local itemName, itemLink, itemLevel = item:GetItemName(), item:GetItemLink(), item:GetCurrentItemLevel()
if itemLevel < 597 then return end

--Create bonusIDs table from item
local itemLinkValues = StringSplitIntoTable(":", itemLink)
local numBonusIDs = itemLinkValues[14]
if not tonumber(numBonusIDs) then return end
local bonusIDs = {unpack(itemLinkValues, 15, 15+numBonusIDs-1)}
bonusIDs = SetValueToKey(bonusIDs)

--Match bonusID to UPGRADE_BONUSES
local bonusID = false
for id in pairs(bonusIDs) do
if UPGRADE_BONUSES[id] then
bonusID = id
end
end

--Loop over current tooltip lines
for i = 1, tooltip:NumLines() do
    local line
    -- 检查 tooltip 是否为 FishingBuddy 的自定义 Tooltip
    if tooltip:GetName():find("FishLibTooltip") then
        -- 针对 FishingBuddy 的 Tooltip 进行特殊处理
        line = _G["FishLibTooltipTextLeft"..i]
    else
        -- 标准 Tooltip 的处理方式
        line = _G[tooltip:GetName().."TextLeft"..i]
    end

    -- 检查 line 是否为 nil
    if line then
        local text = line:GetText()

        if text and text:match(localePatterns[GetLocale()]) then
            --Check if item is a Seasonal/Upgradable item
            local tier,current,total
            if bonusID then
                tier = UPGRADE_BONUSES[bonusID].name
                current = UPGRADE_BONUSES[bonusID].level
                total = UPGRADE_BONUSES[bonusID].max
            else
                tier,current,total,bonusID = GetRankInfo(item, text, GetLocale())
            end
            if not tier or not current or not total then return end
            if tier == "Awakened" and itemLevel >= 532 then tier = "Awakened+" end
            if tier and not UPGRADE_TIERS[tier] then return end

            --Modify Upgrade Rank tooltip line
            local minLevel = UPGRADE_TIERS[tier].minLevel
            local maxLevel = UPGRADE_TIERS[tier].maxLevel
            if minLevel and maxLevel and itemLevel >= minLevel and itemLevel <= maxLevel then
                local tierHexColorMarkup = ""
                local rangeHexColorMarkup = ""

                --if aura_env.config.colorRange then
                --rangeHexColorMarkup = CreateColor(
                --unpack(aura_env.config.ilvlRangeColor)
                --):GenerateHexColorMarkup()
                --end
                --if aura_env.config.colorRank then
                tierHexColorMarkup = UPGRADE_TIERS[tier].color:GenerateHexColorMarkup()
                --end

                local newLineText = string.format("%s%d/%d %s|r %s(%d-%d)|r",
                    tierHexColorMarkup, current, total, tier,
                    rangeHexColorMarkup, minLevel, maxLevel
                )
                line:SetText(newLineText)
                line:Show()
            end

            --Add Crest required to upgrade
            if bonusID and UPGRADE_BONUSES[bonusID].crest then  -- and aura_env.config.showUpgradeCurrency
                local crest = UPGRADE_BONUSES[bonusID].crest
                if crest then
                    local crestName = crest.shortName
                    local crestName_colored = crest.color:WrapTextInColorCode(crestName)
                    local achieve = crest.achieve and select(13, GetAchievementInfo(crest.achieve))
                    local rightLineText = "|A:2329:20:20:1:-1|a"..(not achieve and crestName_colored or "")
                    local rightLine
                    if tooltip:GetName():find("FishLibTooltip") then
                        rightLine = _G["FishLibTooltipTextRight"..i]
                    else
                        rightLine = _G[tooltip:GetName().."TextRight"..i]
                    end
                    rightLine:SetText(rightLineText)
                    rightLine:Show()
                end
            end
        end
    end
end
end
local function OnTooltipSetItem(tooltip, data)
ProcessTooltip(tooltip, data)
end
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Item, OnTooltipSetItem)