if not DamageMeterModDB then
    DamageMeterModDB = {}
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 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()
            -- 重新构建fadeElements以确保包含最新元素
            fadeElements = BuildFadeElements()
            
            -- 创建额外的悬停检测框架
            CreateAdditionalHoverFrames()
        end)
        self:UnregisterEvent("PLAYER_ENTERING_WORLD")
    end
end)
