-- modules/Dodge.lua
-- Dodge 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 GetCombatRating = GetCombatRating
local GetCombatRatingBonus = GetCombatRatingBonus
local GetDodgeChance = GetDodgeChance

----------------------------------------------------
-- Constants Locals
----------------------------------------------------
local CR_DODGE = CR_DODGE or 3
local CR_DODGE_TOOLTIP = CR_DODGE_TOOLTIP
local STAT_DODGE = STAT_DODGE

----------------------------------------------------
-- File Locals
----------------------------------------------------
local moduleName = "Dodge"

----------------------------------------------------
-- 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 dodgeChance = 0
    local dodgeRating = 0
    local dodgeBonus = 0

    ----------------------------------------------------
    -- Update logic
    ----------------------------------------------------
    local function UpdateDodge()
        dodgeChance = GetDodgeChance()
        dodgeRating = GetCombatRating(CR_DODGE)
        dodgeBonus = GetCombatRatingBonus(CR_DODGE)
        
        local showLabel = BDT:GetModuleSetting(moduleName, "showLabel", true)
        local hideDecimals = BDT:GetModuleSetting(moduleName, "hideDecimals", false)
        local textString = (showLabel and STAT_DODGE..": " or "") .. BDT.FormatUtils:FormatPercent(dodgeChance, hideDecimals)
        text:SetText(BDT.FormatUtils:ColorModuleText(moduleName, textString))
        BDT.FontManager:ApplyModuleFont(moduleName, text)
    end

    f.Update = UpdateDodge

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

    f:SetScript("OnEvent", OnEvent)
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:RegisterEvent("UNIT_STATS")
    f:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
    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_DODGE, dodgeChance)
        local tooltip = format(CR_DODGE_TOOLTIP, dodgeRating, dodgeBonus)

        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.Tooltip:Show()
    end)

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

    UpdateDodge()
    return f
end

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

return mod
