if not DamageMeterModDB then
    DamageMeterModDB = {}
end

-- 全局常量定义
local CONSTANTS = {
    MAX_WINDOWS = 3,  -- 最大伤害统计窗口数量
    WINDOW_PREFIX = "DamageMeterSessionWindow",  -- 窗口名称前缀
    HIDE_DELAY = 3  -- 鼠标离开后自动隐藏的延迟时间（秒）
}

-- 悬停效果模块
-- 此模块负责实现鼠标悬停时显示标题栏元素，离开时自动隐藏的效果
local processedWindows = {}  -- 已处理的窗口表，避免重复处理

local function markWindowAsProcessed(windowName)
    processedWindows[windowName] = true
end

local function isWindowProcessed(windowName)
    return processedWindows[windowName] == true
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

-- 创建智能悬停检测区域
local mainHoverFrame = createSmartHoverFrame(DamageMeterSessionWindow1)
local hideTimer = nil
local isMouseOverHoverArea = false
local fadeElements = {}

-- 悬停显示/隐藏函数
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
    
    hideTimer = C_Timer.After(CONSTANTS.HIDE_DELAY, function()
        if not isMouseOverHoverArea then
            HideFadeElements()
        end
    end)
end

-- 为标题栏元素添加鼠标检测
local function setupHeaderMouseInteraction(window)
    if not window or not window.Header then return end
    
    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

-- 动态构建fadeElements表，只包含实际存在的元素

local function processWindowForHover(window, windowName)
    if not window or isWindowProcessed(windowName) then
        return
    end
    
    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)
        markWindowAsProcessed(windowName)
    end
end

local function processAllWindowsForHover()
    local windowNames = {}
    for i = 1, CONSTANTS.MAX_WINDOWS do
        table.insert(windowNames, CONSTANTS.WINDOW_PREFIX .. i)
    end
    
    for _, windowName in ipairs(windowNames) do
        local window = _G[windowName]
        if window and window:IsVisible() then
            processWindowForHover(window, windowName)
        end
    end
end

local function buildFadeElements()
    local elements = {}
    local windowNames = {}
    for i = 1, CONSTANTS.MAX_WINDOWS do
        table.insert(windowNames, CONSTANTS.WINDOW_PREFIX .. i)
    end
    
    for _, windowName in ipairs(windowNames) do
        local window = _G[windowName]
        if window and window:IsVisible() then
            if window.ScrollBox and window.ScrollBox.ScrollTarget then
                window.ScrollBox:EnableMouse(true)
                window.ScrollBox.ScrollTarget:EnableMouse(true)
            end
            
            local windowElements = {
                window.Header,
                --window.MinimizeButton,
                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,
                --window.MinimizeContainer and window.MinimizeContainer.MinimizeButton
            }
            
            for _, element in ipairs(windowElements) do
                if element then
                    table.insert(elements, element)
                end
            end
        end
    end
    
    return elements
end

local function refreshFadeElements()
    local newElements = buildFadeElements()
    
    for _, element in ipairs(newElements) do
        local found = false
        for _, existingElement in ipairs(fadeElements) do
            if element == existingElement then
                found = true
                break
            end
        end
        
        if not found then
            element:SetAlpha(0)
        end
    end
    
    fadeElements = newElements
end

fadeElements = buildFadeElements()
for _, element in ipairs(fadeElements) do
    element:SetAlpha(0)
end

if mainHoverFrame then
    processWindowForHover(DamageMeterSessionWindow1, "DamageMeterSessionWindow1")
end

local function createAdditionalHoverFrames()
    processAllWindowsForHover()
end

-- 创建框架管理器来处理初始化和事件
local hoverManager = CreateFrame("Frame", "DamageMeterHoverManager", UIParent)
hoverManager:RegisterEvent("PLAYER_ENTERING_WORLD")

hoverManager:SetScript("OnEvent", function(self, event)
    if event == "PLAYER_ENTERING_WORLD" then
        C_Timer.After(2, function()
            refreshFadeElements()
            createAdditionalHoverFrames()
            
            C_Timer.NewTicker(1, function()
                refreshFadeElements()
                createAdditionalHoverFrames()
            end)
        end)
        self:UnregisterEvent("PLAYER_ENTERING_WORLD")
    end
end)
