-- modules/SpellPower.lua
-- Spell Power 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 GetSpellBonusDamage = GetSpellBonusDamage

----------------------------------------------------
-- Constants Locals
----------------------------------------------------
local ITEM_MOD_SPELL_POWER_SHORT = ITEM_MOD_SPELL_POWER_SHORT
local MAX_SPELL_SCHOOLS = MAX_SPELL_SCHOOLS
local STAT_SPELLPOWER_TOOLTIP = STAT_SPELLPOWER_TOOLTIP
local SPELL_SCHOOL_NAMES = {
    [1] = SPELL_SCHOOL1_CAP, -- Holy
    [2] = SPELL_SCHOOL2_CAP, -- Fire
    [3] = SPELL_SCHOOL3_CAP, -- Nature
    [4] = SPELL_SCHOOL4_CAP, -- Frost
    [5] = SPELL_SCHOOL5_CAP, -- Shadow
    [6] = SPELL_SCHOOL6_CAP, -- Arcane
}

----------------------------------------------------
-- File Locals
----------------------------------------------------
local moduleName = "Spell Power"

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

    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 spellPower = 0
    local maxSpellPower = 0

    ----------------------------------------------------
    -- Update logic
    ----------------------------------------------------
    local function UpdateSpellPower()
        -- Get the highest spell power across all schools
        maxSpellPower = 0
        for i = 2, MAX_SPELL_SCHOOLS do
            local power = GetSpellBonusDamage(i)
            if power > maxSpellPower then
                maxSpellPower = power
            end
        end
        
        spellPower = maxSpellPower
        
        local showLabel = BDT:GetModuleSetting(moduleName, "showLabel", true)
        local textString = (showLabel and ITEM_MOD_SPELL_POWER_SHORT..": " or "") .. spellPower
        text:SetText(BDT.FormatUtils:ColorModuleText(moduleName, textString))
        BDT.FontManager:ApplyModuleFont(moduleName, text)
    end

    f.Update = UpdateSpellPower

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

    f:SetScript("OnEvent", OnEvent)
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:RegisterEvent("UNIT_STATS")
    f:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
    f:RegisterEvent("UNIT_SPELL_HASTE")

    ----------------------------------------------------
    -- 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%d|r', ITEM_MOD_SPELL_POWER_SHORT, maxSpellPower)
        BDT.FormatUtils:AddTooltipHeader(BDT.Tooltip, nil, text)
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, " ")
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, STAT_SPELLPOWER_TOOLTIP, nil, nil, nil, nil, nil, nil, nil, true)
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, " ")
        
        -- Show spell power for each school
        for i = 2, MAX_SPELL_SCHOOLS do
            local power = GetSpellBonusDamage(i)
            local schoolName = SPELL_SCHOOL_NAMES[i-1] or "Unknown"
            BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, schoolName, power, 1, 1, 1)
        end

        BDT.Tooltip:Show()
    end)

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

    UpdateSpellPower()
    return f
end

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

return mod
