local _,ns = ...
ns.tips("大秘境分数界面鼠标提示优化")--https://ngabbs.com/read.php?tid=42357797
local function ShowDungeonInfoTooltip()
    -- 检测玩家是否正在进行大秘境，并获取当前活跃钥石层数
    local function IsPlayerInActiveDungeon()
        local activeKeystoneLevel = C_ChallengeMode.GetActiveKeystoneInfo()
        if activeKeystoneLevel and activeKeystoneLevel > 0 then
            return activeKeystoneLevel
        end
        return nil
    end
    
    local function HookDungeonIcons()
        if not ChallengesFrame then
            return
        end
        
        for _, icon in ipairs(ChallengesFrame.DungeonIcons) do
            icon:HookScript("OnEnter", function(self)
                    local mapChallengeModeID = self.mapID
                    -- 副本完成时间记录为UTC，需手动调整为当地时间
                    local offset = 8
                    
                    local dungeonName, _, baseTimeLimit = C_ChallengeMode.GetMapUIInfo(mapChallengeModeID)
                    local intimeInfo, overtimeInfo = C_MythicPlus.GetSeasonBestForMap(mapChallengeModeID)
                    
                    -- 检测玩家是否正在进行大秘境
                    local activeKeystoneLevel = IsPlayerInActiveDungeon()
                    
                    -- 计算限时时间（根据情况是否加90秒）
                    local timeLimit = baseTimeLimit
                    if not activeKeystoneLevel and intimeInfo and intimeInfo.level >= 7 then
                        timeLimit = timeLimit --+ 90
                    end
                    
                    GameTooltip:ClearLines()
                    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                    
                    if intimeInfo then
                        GameTooltip:AddLine(dungeonName, 1, 1, 1)
                        
                        local scoreColor = C_ChallengeMode.GetSpecificDungeonOverallScoreRarityColor(intimeInfo.dungeonScore)
                        if scoreColor then
                            GameTooltip:AddLine("评分: |c" .. scoreColor:GenerateHexColor() .. intimeInfo.dungeonScore .. "|r")
                        else
                            GameTooltip:AddLine("评分: " .. intimeInfo.dungeonScore)
                        end
                        
                        GameTooltip:AddLine(" ")
                        GameTooltip:AddLine("最佳记录", 1, 0.75, 0)
                        GameTooltip:AddLine("等级 " .. intimeInfo.level, 1, 1, 1)
                        
                        local completionTimeFormatted = string.format("%02d:%02d", math.floor(intimeInfo.durationSec / 60), intimeInfo.durationSec % 60)
                        local remainingTime = timeLimit - intimeInfo.durationSec
                        local remainingTimeFormatted
                        
                        if remainingTime >= 0 then
                            remainingTimeFormatted = string.format("还剩 %02d:%02d", math.floor(remainingTime / 60), remainingTime % 60)
                        else
                            remainingTimeFormatted = string.format("超时 %02d:%02d", math.abs(math.floor(remainingTime / 60)), math.abs(remainingTime % 60))
                        end
                        
                        GameTooltip:AddLine("时间 " .. completionTimeFormatted .. " (" .. remainingTimeFormatted .. ")", 1, 1, 1)
                        GameTooltip:AddLine(" ")
                        
                        GameTooltip:AddLine("队伍成员", 1, 0.75, 0)
                        for _, member in ipairs(intimeInfo.members) do
                            local classColors = {
                                [1] = {198, 155, 109},  [2] = {244, 140, 186},  [3] = {170, 211, 114},
                                [4] = {255, 244, 104},  [5] = {255, 255, 255},  [6] = {196, 30, 58},
                                [7] = {0, 112, 221},    [8] = {63, 199, 235},   [9] = {135, 136, 238},
                                [10] = {0, 255, 152},   [11] = {255, 124, 10},  [12] = {163, 48, 201},
                                [13] = {51, 147, 127},  [14] = {170, 211, 114}
                            }
                            local color = classColors[member.classID] or {255, 255, 255}
                            local memberName = member.name or "未知"
                            local specNames = {
                                [62] = "奥术", [63] = "火焰", [64] = "冰霜", [65] = "神圣", [66] = "防护", [70] = "惩戒",
                                [71] = "武器", [72] = "狂怒", [73] = "防护", [102] = "平衡", [103] = "野性", [104] = "守护",
                                [105] = "恢复", [250] = "鲜血", [251] = "冰霜", [252] = "邪恶", [253] = "野兽控制",
                                [254] = "射击", [255] = "生存", [256] = "戒律", [257] = "神圣", [258] = "暗影", [259] = "奇袭",
                                [260] = "狂徒", [261] = "敏锐", [262] = "元素", [263] = "增强", [264] = "恢复", [265] = "痛苦",
                                [266] = "恶魔学识", [267] = "毁灭", [268] = "酒仙", [269] = "踏风", [270] = "织雾",
                                [535] = "狂野", [536] = "狡诈", [537] = "坚韧", [577] = "浩劫", [581] = "复仇",
                                [1467] = "湮灭", [1468] = "恩护", [1473] = "增辉"
                            }
                            local specName = specNames[member.specID] or "未知专精"
                            local coloredName = string.format("|cff%02x%02x%02x%s(%s)|r", color[1], color[2], color[3], memberName, specName)
                            GameTooltip:AddLine(coloredName)
                        end
                        
                        GameTooltip:AddLine(" ")
                        local totalTimeFormatted = string.format("%02d:%02d", math.floor(timeLimit / 60), timeLimit % 60)
                        GameTooltip:AddLine("副本时间: " .. totalTimeFormatted, 1, 1, 1)
                        
                        local completionDate = intimeInfo.completionDate
                        if completionDate then
                            local adjustedYear = completionDate.year + 2000
                            local adjustedMonth = completionDate.month + 1
                            local adjustedDay = completionDate.day + 1
                            local adjustedHour = completionDate.hour + offset
                            local adjustedMinute = completionDate.minute
                            
                            if adjustedHour >= 24 then
                                adjustedHour = adjustedHour - 24
                                adjustedDay = adjustedDay + 1
                            end
                            
                            GameTooltip:AddLine(string.format("完成日期: %d/%02d/%02d %02d:%02d", adjustedYear, adjustedMonth, adjustedDay, adjustedHour, adjustedMinute), 1, 1, 1)
                        else
                            GameTooltip:AddLine("日期: 无法获取", 1, 1, 1)
                        end
                    else
                        GameTooltip:AddLine("本赛季尚未有记录", 1, 0.5, 0.5)
                    end
                    
                    GameTooltip:Show()
            end)
            
            icon:HookScript("OnLeave", function()
                    GameTooltip:Hide()
            end)
        end
    end
    
    HookDungeonIcons()
    
    local frame = CreateFrame("Frame")
    frame:RegisterEvent("CHALLENGE_MODE_MAPS_UPDATE")
    frame:SetScript("OnEvent", function()
            HookDungeonIcons()
    end)
    
    C_Timer.NewTicker(600, function()
            HookDungeonIcons()
    end)
end

C_Timer.After(5, ShowDungeonInfoTooltip)