-- modules/Stagger.lua
-- Stagger datatext adapted from ElvUI for Simple DataTexts (BDT)
local _, ns = ...
local BDT = ns.DataTexts
local L = BDT.L

local mod = {}

----------------------------------------------------
-- Lua Locals
----------------------------------------------------
local format = string.format

----------------------------------------------------
-- WoW API Locals
----------------------------------------------------
local CreateFrame = CreateFrame
local UnitStagger = UnitStagger
local UnitHealthMax = UnitHealthMax

----------------------------------------------------
-- Constants Locals
----------------------------------------------------
local STAT_STAGGER = STAT_STAGGER
local STAT_STAGGER_TARGET_TOOLTIP = STAT_STAGGER_TARGET_TOOLTIP
local STAT_STAGGER_TOOLTIP = STAT_STAGGER_TOOLTIP

----------------------------------------------------
-- File Locals
----------------------------------------------------
local moduleName = "Stagger"

----------------------------------------------------
-- Module Config Settings
----------------------------------------------------
local function SetupModuleConfig()
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Show Label"], "showLabel", true)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Hide Decimals"], "hideDecimals", false)

    BDT.ModuleRegistry:GlobalModuleSettings(moduleName)
end

SetupModuleConfig()

----------------------------------------------------
-- Module Creation
----------------------------------------------------
function mod.Create(slotFrame)
    local f = CreateFrame("Frame", nil, slotFrame)
    f:SetAllPoints(slotFrame)
    f:EnableMouse(false)

    local text = slotFrame.text
    if not text then
        text = slotFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
        text:SetPoint("CENTER")
        slotFrame.text = text
    end

    local staggerPercent, staggerAmount = 0, 0

    ----------------------------------------------------
    -- Update logic
    ----------------------------------------------------
    local function UpdateStagger()
        staggerAmount = UnitStagger("player") or 0
        local maxHealth = UnitHealthMax("player")
        
        if maxHealth and maxHealth > 0 then
            staggerPercent = (staggerAmount / maxHealth) * 100
        else
            staggerPercent = 0
        end
        
        local showLabel = BDT:GetModuleSetting(moduleName, "showLabel", true)
        local hideDecimals = BDT:GetModuleSetting(moduleName, "hideDecimals", false)
        local textString = (showLabel and STAT_STAGGER..": " or "") .. BDT.FormatUtils:FormatPercent(staggerPercent, hideDecimals)
        text:SetText(BDT.FormatUtils:ColorModuleText(moduleName, textString))
        BDT.FontManager:ApplyModuleFont(moduleName, text)
    end

    f.Update = UpdateStagger

    ----------------------------------------------------
    -- Event Handler
    ----------------------------------------------------
    local function OnEvent(self, event, ...)
        if event == "PLAYER_ENTERING_WORLD"
        or event == "UNIT_MAXHEALTH"
        or event == "UNIT_AURA" then
            UpdateStagger()
        end
    end

    f:SetScript("OnEvent", OnEvent)
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:RegisterEvent("UNIT_MAXHEALTH")
    f:RegisterEvent("UNIT_AURA")

    ----------------------------------------------------
    -- Tooltip
    ----------------------------------------------------
    slotFrame:EnableMouse(true)
    slotFrame:SetScript("OnEnter", function(self)
        local anchor = BDT.FormatUtils:FindBestAnchorPoint(self)
        BDT.Tooltip:SetOwner(self, anchor)
        BDT.Tooltip:ClearLines()

        local text = format('%s: |cffFFFFFF%.2f%%|r', STAT_STAGGER, staggerPercent)
        local tooltip = format(STAT_STAGGER_TOOLTIP, staggerPercent)
        local amount = format(L["Stagger Amount:"].." %d (%.2f%%)", staggerAmount, staggerPercent)

        BDT.FormatUtils:AddTooltipHeader(BDT.Tooltip, nil, text)
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, " ")
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, tooltip, nil, nil, nil, nil, nil, nil, nil, true)
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, " ")
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, amount, nil, nil, nil, nil, nil, nil, nil, true)

        BDT.Tooltip:Show()
    end)

    slotFrame:SetScript("OnLeave", function()
        BDT.Tooltip:Hide()
    end)

    UpdateStagger()
    return f
end

----------------------------------------------------
-- Register with BDT
----------------------------------------------------
BDT.ModuleRegistry:RegisterDatatext(moduleName, mod)

return mod
