
-- --by:简繁,伤害统计始终简化数值
local NumberData = {
    config = CreateAbbreviateConfig({
        {
            breakpoint = 1e10,--123亿
            abbreviation = "亿",
            significandDivisor = 1e8,
            fractionDivisor = 1,
            abbreviationIsGlobal = false
        },
        {
            breakpoint = 1e9,--12.3亿
            abbreviation = "亿",
            significandDivisor = 1e7,
            fractionDivisor = 10,
            abbreviationIsGlobal = false
        },
        {
            breakpoint = 1e8,--1.23亿
            abbreviation = "亿",
            significandDivisor = 1e6,
            fractionDivisor = 100,
            abbreviationIsGlobal = false
        },
        {
            breakpoint = 1e6,--123万
            abbreviation = "万",
            significandDivisor = 1e4,
            fractionDivisor = 1,
            abbreviationIsGlobal = false
        },
        {
            breakpoint = 1e4,--1.2万
            abbreviation = "万",
            significandDivisor = 1e3,
            fractionDivisor = 10,
            abbreviationIsGlobal = false
        },
        {
            breakpoint = 1,
            abbreviation = "",
            significandDivisor = 1,
            fractionDivisor = 1,
            abbreviationIsGlobal = false
        },
    }),
}
local function GetMainValue(entry)
    if entry.valuePerSecond and entry:ShowsValuePerSecondAsPrimary() then
        return entry.valuePerSecond;
    end

    if entry.value then
        return entry.value;
    end

    return 0;
end
local function GetParentheticalValue(entry)
    if entry.value and entry:ShowsValuePerSecondAsPrimary() then
        return entry.value;
    end

    if entry.valuePerSecond then
        return entry.valuePerSecond;
    end

    return 0;
end
local function GetPercentageValue(entry)
    if entry.value and entry.sessionTotalValue and entry.sessionTotalValue > 0 then
        return entry.value / entry.sessionTotalValue;
    end

    return 0;
end
local function GetEntryValueText(value, parentheticalValue, percentageValue)
    if percentageValue then
        return DAMAGE_METER_ENTRY_FORMAT_COMPLETE:format(AbbreviateNumbers(value,NumberData), AbbreviateNumbers(parentheticalValue,NumberData), Round(percentageValue * 100));
    elseif parentheticalValue then
        return DAMAGE_METER_ENTRY_FORMAT_COMPACT:format(AbbreviateNumbers(value,NumberData), AbbreviateNumbers(parentheticalValue,NumberData));
    else
        return DAMAGE_METER_ENTRY_FORMAT_MINIMAL:format(AbbreviateNumbers(value,NumberData));
    end
end
local numberDisplayTypeFormatters =
{
    [Enum.DamageMeterNumbers.Minimal] = function(entry) return GetEntryValueText(GetMainValue(entry)); end,
    [Enum.DamageMeterNumbers.Compact] = function(entry) return GetEntryValueText(GetMainValue(entry), GetParentheticalValue(entry)); end,
    [Enum.DamageMeterNumbers.Complete] = function(entry) return GetEntryValueText(GetMainValue(entry), GetParentheticalValue(entry), GetPercentageValue(entry)); end,
}

local function GetValueText(self)
    return numberDisplayTypeFormatters[self:GetNumberDisplayType()](self);
end

hooksecurefunc(DamageMeterEntryMixin, "Init", function(self)
    if self.MYHOOK then return end
    self.MYHOOK = true
    hooksecurefunc(self, "UpdateValue", function(bar)
        if bar:GetNumberDisplayType() == Enum.DamageMeterNumbers.Complete then
            return
        end
        local text = GetValueText(bar)
        bar:GetValue():SetText(text);
    end)
end)

--[[ 处理状态条纹理替换的辅助函数
local function ApplyMaterialsToStatusBar(statusBar)
    if statusBar and not statusBar.__MaterialsApplied then
        -- 替换状态条纹理
        statusBar:SetStatusBarTexture("Interface\\AddOns\\OrzUI\\Media\\Modules\\Skada\\YaSkada05.blp")
        
        -- 隐藏边框 - 使用BackgroundEdge
        if statusBar.BackgroundEdge then
            statusBar.BackgroundEdge:Hide()
        end
        
        -- 隐藏背景 - 使用Background
        if statusBar.Background then
            statusBar.Background:Hide()
        end
        
        -- 标记已处理
        statusBar.__MaterialsApplied = true
    end
end

-- 主要纹理替换和边框背景隐藏函数
local function ReplaceDamageMeterTextures()
    -- 检查所有伤害统计窗口（通常最多3个）
    for i = 1, 3 do
        local mainWindow = _G["DamageMeterSessionWindow" .. i]
        
        -- 如果窗口存在
        if mainWindow then
            -- 处理LocalPlayerEntry的状态条
            if mainWindow.LocalPlayerEntry and mainWindow.LocalPlayerEntry.StatusBar then
                ApplyMaterialsToStatusBar(mainWindow.LocalPlayerEntry.StatusBar)
            end
            
            -- 如果窗口有ScrollBox
            if mainWindow.ScrollBox and mainWindow.ScrollBox.ScrollTarget then
                local scrollTarget = mainWindow.ScrollBox.ScrollTarget
                
                -- 遍历滚动目标下的所有子元素（伤害统计条）
                for _, child in ipairs({scrollTarget:GetChildren()}) do
                    -- 如果子元素有StatusBar
                    if child.StatusBar then
                        ApplyMaterialsToStatusBar(child.StatusBar)
                    end
                end
            end
        end
    end
end

-- 创建定时器定期执行替换
local timer = C_Timer.NewTicker(1, function()
    ReplaceDamageMeterTextures()
end)

-- 玩家登录时立即执行一次
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:SetScript("OnEvent", function()
    C_Timer.After(1, function()
        ReplaceDamageMeterTextures()
    end)
end)

-- 创建框架定位管理器
local frameManager = CreateFrame("Frame", "DamageMeterFrameManager", UIParent)
frameManager:RegisterEvent("PLAYER_ENTERING_WORLD")

-- 改进的主定位函数 - 自适应不同数量的窗口
local function SetupDamageMeterPositions()
    -- 动态处理存在的窗口
    local previousWindow = DamageMeterSessionWindow1
    
    -- 按顺序处理Window2和Window3
    for i = 2, 3 do
        local windowName = "DamageMeterSessionWindow"..i
        local currentWindow = _G[windowName]
        
        if currentWindow then
            -- 获取主窗口尺寸
            local mainHeight = DamageMeterSessionWindow1:GetHeight()
            local mainWidth = DamageMeterSessionWindow1:GetWidth()
            
            if mainHeight and mainHeight > 0 and mainWidth and mainWidth > 0 then
                -- 设置尺寸与主窗口一致
                currentWindow:SetHeight(mainHeight)
                currentWindow:SetWidth(mainWidth)
            end
            
            -- 清除现有锚点并设置位置
            currentWindow:ClearAllPoints()
            currentWindow:SetPoint("BOTTOM", previousWindow, "TOP", 0, 0)
            
            -- 设置框架层级
            currentWindow:SetFrameStrata(previousWindow:GetFrameStrata() or "MEDIUM")
            
            previousWindow = currentWindow
        end
    end
end

-- 改进的尺寸同步函数
local function SyncFrameSizes()
    local mainHeight = DamageMeterSessionWindow1:GetHeight()
    local mainWidth = DamageMeterSessionWindow1:GetWidth()
    
    if mainHeight and mainHeight > 0 and mainWidth and mainWidth > 0 then
        -- 同步所有存在的窗口
        for i = 1, 3 do
            local windowName = "DamageMeterSessionWindow"..i
            local window = _G[windowName]
            if window then
                window:SetHeight(mainHeight)
                window:SetWidth(mainWidth)
            end
        end
        
        -- 重新建立位置关系
        local previousWindow = DamageMeterSessionWindow1
        for i = 2, 3 do
            local currentWindow = _G["DamageMeterSessionWindow"..i]
            if currentWindow then
                currentWindow:ClearAllPoints()
                currentWindow:SetPoint("BOTTOM", previousWindow, "TOP", 0, 0)
                previousWindow = currentWindow
            end
        end
    end
end]]


-- 创建智能的鼠标检测区域
local function CreateSmartHoverFrame(parentWindow)
    if not parentWindow then return nil end
    
    local hoverFrame = CreateFrame("Frame", parentWindow:GetName().."SmartHoverController", parentWindow)
    
    -- 获取窗口尺寸
    local windowWidth = parentWindow:GetWidth() or 300
    local windowHeight = parentWindow:GetHeight() or 200
    
    -- 创建三个悬停检测区域：左上、右上、以及除交互元素外的其他区域
    local leftHover = CreateFrame("Frame", nil, hoverFrame)
    leftHover:SetPoint("TOPLEFT", parentWindow, "TOPLEFT", 0, 0)
    leftHover:SetPoint("BOTTOMLEFT", parentWindow, "TOPLEFT", 0, -30)
    leftHover:SetWidth(windowWidth * 0.3)  -- 左侧30%区域
    
    local rightHover = CreateFrame("Frame", nil, hoverFrame)
    rightHover:SetPoint("TOPRIGHT", parentWindow, "TOPRIGHT", 0, 0)
    rightHover:SetPoint("BOTTOMRIGHT", parentWindow, "TOPRIGHT", 0, -30)
    rightHover:SetWidth(windowWidth * 0.3)  -- 右侧30%区域
    
    local centerHover = CreateFrame("Frame", nil, hoverFrame)
    centerHover:SetPoint("TOPLEFT", leftHover, "TOPRIGHT", 0, 0)
    centerHover:SetPoint("TOPRIGHT", rightHover, "TOPLEFT", 0, 0)
    centerHover:SetPoint("BOTTOM", parentWindow, "TOP", 0, -30)
    
    -- 启用鼠标检测但不拦截点击
    leftHover:EnableMouse(true)
    rightHover:EnableMouse(true)
    centerHover:EnableMouse(true)
    
    -- 设置较低的层级
    leftHover:SetFrameStrata("BACKGROUND")
    rightHover:SetFrameStrata("BACKGROUND")
    centerHover:SetFrameStrata("BACKGROUND")
    
    hoverFrame.leftHover = leftHover
    hoverFrame.rightHover = rightHover
    hoverFrame.centerHover = centerHover
    
    return hoverFrame
end

-- 动态构建fadeElements表，只包含实际存在的元素
local function BuildFadeElements()
    local elements = {}
    local windowNames = {"DamageMeterSessionWindow1", "DamageMeterSessionWindow2", "DamageMeterSessionWindow3"}
    
    for _, windowName in ipairs(windowNames) do
        local window = _G[windowName]
        if window then
            -- 确保ScrollBox可以接收鼠标事件
            if window.ScrollBox and window.ScrollBox.ScrollTarget then
                window.ScrollBox:EnableMouse(true)
                window.ScrollBox.ScrollTarget:EnableMouse(true)
            end
            
            -- 添加该窗口的所有界面元素
            local windowElements = {
                window.Header,
                window.SettingsDropdown and window.SettingsDropdown.Icon,
                window.DamageMeterTypeDropdown and window.DamageMeterTypeDropdown.Arrow,
                window.SessionDropdown and window.SessionDropdown.Background,
                window.DamageMeterTypeDropdown and window.DamageMeterTypeDropdown.TypeName,
                window.SessionDropdown and window.SessionDropdown.SessionName,
                window.SessionDropdown and window.SessionDropdown.Text
            }
            
            for _, element in ipairs(windowElements) do
                if element then
                    table.insert(elements, element)
                end
            end
        end
    end
    
    return elements
end

-- 初始隐藏所有元素
local fadeElements = BuildFadeElements()
for _, element in ipairs(fadeElements) do
    element:SetAlpha(0)
    -- 不禁用元素的鼠标检测，让它们可以点击
    -- element:EnableMouse(false)
end

-- 创建智能悬停检测区域
local mainHoverFrame = CreateSmartHoverFrame(DamageMeterSessionWindow1)
local hideTimer = nil
local isMouseOverHoverArea = false
local hideDelay = 3  -- 鼠标离开后3秒隐藏

-- 悬停显示/隐藏函数
local function ShowFadeElements()
    -- 如果已经有隐藏计时器，先取消
    if hideTimer then
        hideTimer:Cancel()
        hideTimer = nil
    end
    
    for _, element in ipairs(fadeElements) do
        if element:GetAlpha() < 1 then
            UIFrameFadeIn(element, 0.3, element:GetAlpha(), 1)
        end
    end
end

local function HideFadeElements()
    for _, element in ipairs(fadeElements) do
        if element:GetAlpha() > 0 then
            UIFrameFadeOut(element, 0.3, element:GetAlpha(), 0)
        end
    end
end

-- 鼠标进入悬停区域时的处理
local function OnEnterHoverArea()
    isMouseOverHoverArea = true
    
    -- 取消任何待定的隐藏计时器
    if hideTimer then
        hideTimer:Cancel()
        hideTimer = nil
    end
    
    ShowFadeElements()
end

-- 鼠标离开悬停区域时的处理
local function OnLeaveHoverArea()
    isMouseOverHoverArea = false
    
    -- 取消任何已有的隐藏计时器
    if hideTimer then
        hideTimer:Cancel()
        hideTimer = nil
    end
    
    -- 设置新的隐藏计时器，延迟3秒
    hideTimer = C_Timer.After(hideDelay, function()
        if not isMouseOverHoverArea then
            HideFadeElements()
        end
    end)
end

-- 为标题栏元素添加鼠标检测
local function SetupHeaderMouseInteraction(window)
    if not window or not window.Header then return end
    
    -- 为Header添加鼠标检测
    if window.Header then
        window.Header:EnableMouse(true)
        window.Header:SetScript("OnEnter", function(self)
            OnEnterHoverArea()
        end)
        window.Header:SetScript("OnLeave", function(self)
            OnLeaveHoverArea()
        end)
    end
    
    -- 为下拉菜单添加鼠标检测
    if window.SettingsDropdown then
        window.SettingsDropdown:EnableMouse(true)
        window.SettingsDropdown:SetScript("OnEnter", function(self)
            OnEnterHoverArea()
        end)
        window.SettingsDropdown:SetScript("OnLeave", function(self)
            OnLeaveHoverArea()
        end)
    end
    
    if window.DamageMeterTypeDropdown then
        window.DamageMeterTypeDropdown:EnableMouse(true)
        window.DamageMeterTypeDropdown:SetScript("OnEnter", function(self)
            OnEnterHoverArea()
        end)
        window.DamageMeterTypeDropdown:SetScript("OnLeave", function(self)
            OnLeaveHoverArea()
        end)
    end
    
    if window.SessionDropdown then
        window.SessionDropdown:EnableMouse(true)
        window.SessionDropdown:SetScript("OnEnter", function(self)
            OnEnterHoverArea()
        end)
        window.SessionDropdown:SetScript("OnLeave", function(self)
            OnLeaveHoverArea()
        end)
    end
end

--[[if mainHoverFrame then
    -- 设置悬停区域的事件
    mainHoverFrame.leftHover:SetScript("OnEnter", OnEnterHoverArea)
    mainHoverFrame.leftHover:SetScript("OnLeave", OnLeaveHoverArea)
    
    mainHoverFrame.rightHover:SetScript("OnEnter", OnEnterHoverArea)
    mainHoverFrame.rightHover:SetScript("OnLeave", OnLeaveHoverArea)
    
    mainHoverFrame.centerHover:SetScript("OnEnter", OnEnterHoverArea)
    mainHoverFrame.centerHover:SetScript("OnLeave", OnLeaveHoverArea)
    
    -- 为标题栏元素设置鼠标交互
    SetupHeaderMouseInteraction(DamageMeterSessionWindow1)
end]]

-- 为存在的窗口创建额外的悬停检测区域
local function CreateAdditionalHoverFrames()
    local windowNames = {"DamageMeterSessionWindow2", "DamageMeterSessionWindow3"}
    
    for _, windowName in ipairs(windowNames) do
        local window = _G[windowName]
        if window then
            local hoverFrame = CreateSmartHoverFrame(window)
            
            if hoverFrame then
                hoverFrame.leftHover:SetScript("OnEnter", OnEnterHoverArea)
                hoverFrame.leftHover:SetScript("OnLeave", OnLeaveHoverArea)
                
                hoverFrame.rightHover:SetScript("OnEnter", OnEnterHoverArea)
                hoverFrame.rightHover:SetScript("OnLeave", OnLeaveHoverArea)
                
                hoverFrame.centerHover:SetScript("OnEnter", OnEnterHoverArea)
                hoverFrame.centerHover:SetScript("OnLeave", OnLeaveHoverArea)
                
                -- 为标题栏元素设置鼠标交互
                SetupHeaderMouseInteraction(window)
            end
        end
    end
end

-- 创建框架定位管理器
local frameManager = CreateFrame("Frame", "DamageMeterFrameManager", UIParent)
frameManager:RegisterEvent("PLAYER_ENTERING_WORLD")
-- 事件处理
frameManager:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_ENTERING_WORLD" then
        C_Timer.After(2, function()
            -- 重新构建fadeElements以确保包含最新元素
            fadeElements = BuildFadeElements()
            
            -- 创建额外的悬停检测框架
            CreateAdditionalHoverFrames()

        end)
        self:UnregisterEvent("PLAYER_ENTERING_WORLD")
    end
end)
