-- modules/ItemLevel.lua
-- ItemLevel datatext for Simple DataTexts (BDT)
local _, ns = ...
local BDT = ns.DataTexts
local L = BDT.L

local mod = {}

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

----------------------------------------------------
-- WoW API Locals
----------------------------------------------------
local GetAverageItemLevel = GetAverageItemLevel

----------------------------------------------------
-- File Locals
----------------------------------------------------
local moduleName = "Item Level"
local equippedIL = 0
local maxIL = 0

----------------------------------------------------
-- Module Config Settings
----------------------------------------------------
local function SetupModuleConfig()
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Show Label"], "showLabel", true)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Show Short Label"], "showShortLabel", false)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Show Max Item Level"], "showMaxItemLevel", 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

    ----------------------------------------------------
    -- Update logic
    ----------------------------------------------------
    local function UpdateItemLevel()
        equippedIL, maxIL = GetAverageItemLevel()
        
        -- Round to whole numbers
        equippedIL = format("%.0f", equippedIL)
        maxIL = format("%.0f", maxIL)
        
        local showLabel = BDT:GetModuleSetting(moduleName, "showLabel", true)
        local showShortLabel = BDT:GetModuleSetting(moduleName, "showShortLabel", false)
        local showMaxItemLevel = BDT:GetModuleSetting(moduleName, "showMaxItemLevel", true)
        
        local label = ""
        if showLabel then
            label = (showShortLabel and L["ilvl"]..": " or L["Item Level"]..": ")
        end
        
        local textString
        if showMaxItemLevel then
            textString = label..format("%s/%s", equippedIL, maxIL)
        else
            textString = label..equippedIL
        end
        
        text:SetText(BDT.FormatUtils:ColorModuleText(moduleName, textString))
        BDT.FontManager:ApplyModuleFont(moduleName, text)
    end
    f.Update = UpdateItemLevel

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

    f:SetScript("OnEvent", OnEvent)
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
    f:RegisterEvent("PLAYER_AVG_ITEM_LEVEL_UPDATE")

    ----------------------------------------------------
    -- Tooltip
    ----------------------------------------------------
    slotFrame:EnableMouse(true)
    slotFrame:SetScript("OnEnter", function(self)
        local anchor = BDT.FormatUtils:FindBestAnchorPoint(self)
        BDT.Tooltip:SetOwner(self, anchor)
        BDT.Tooltip:ClearLines()
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, L["Equipped Item Level"], equippedIL, 1, 0.82, 0, 1, 1, 1)
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, L["Maximum Item Level"], maxIL, 1, 0.82, 0, 1, 1, 1)
        BDT.Tooltip:Show()
    end)

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

    UpdateItemLevel()

    return f
end

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

return mod
