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

local mod = {}

----------------------------------------------------
-- Lua Locals
----------------------------------------------------
local CreateFrame = CreateFrame

----------------------------------------------------
-- WoW API Locals
----------------------------------------------------
local C_Map = C_Map
local GetZoneText = GetZoneText
local GetSubZoneText = GetSubZoneText
local GetMinimapZoneText = GetMinimapZoneText

----------------------------------------------------
-- File Locals
----------------------------------------------------
local moduleName = "Map Name"

----------------------------------------------------
-- Module Config Settings
----------------------------------------------------
local function SetupModuleConfig()
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "select", L["Display Format"], "displayFormat", 1, {
        [1] = L["Zone Name"],
        [2] = L["Subzone Name"],
        [3] = L["Zone - Subzone"],
        [4] = L["Zone / Subzone (Two Lines)"],
        [5] = L["Minimap Zone"],
    })

    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Show Zone on Tooltip"], "showZoneTooltip", true)
    BDT.ModuleRegistry:AddModuleConfigSetting(moduleName, "checkbox", L["Show Coordinates on Tooltip"], "showCoordinates", 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 currentZone = ""
    local currentSubzone = ""
    local currentMapID = 0

    ----------------------------------------------------
    -- Update logic
    ----------------------------------------------------
    local function UpdateMapName()
        local zone = GetZoneText() or ""
        local subzone = GetSubZoneText() or ""
        local minimapZone = GetMinimapZoneText() or ""
        
        currentZone = zone
        currentSubzone = subzone
        currentMapID = C_Map.GetBestMapForUnit("player") or 0

        local displayFormat = BDT:GetModuleSetting(moduleName, "displayFormat", 1)
        local textString = ""

        if displayFormat == 1 then
            -- Zone Name
            textString = zone
        elseif displayFormat == 2 then
            -- Subzone Name
            textString = subzone ~= "" and subzone or zone
        elseif displayFormat == 3 then
            -- Zone - Subzone
            if subzone ~= "" and subzone ~= zone then
                textString = zone .. " - " .. subzone
            else
                textString = zone
            end
        elseif displayFormat == 4 then
            -- Zone / Subzone (Two Lines)
            if subzone ~= "" and subzone ~= zone then
                textString = zone .. "\n" .. subzone
            else
                textString = zone
            end
        elseif displayFormat == 5 then
            -- Minimap Zone
            textString = minimapZone
        end

        text:SetText(BDT.FormatUtils:ColorModuleText(moduleName, textString))
        BDT.FontManager:ApplyModuleFont(moduleName, text)
    end
    f.Update = UpdateMapName

    ----------------------------------------------------
    -- Tooltip Handler
    ----------------------------------------------------
    slotFrame:EnableMouse(true)
    slotFrame:SetScript("OnEnter", function(self)
        local showZoneTooltip = BDT:GetModuleSetting(moduleName, "showZoneTooltip", true)
        local showCoordinates = BDT:GetModuleSetting(moduleName, "showCoordinates", true)

        if not showZoneTooltip then return end

        local anchor = BDT.FormatUtils:FindBestAnchorPoint(self)
        BDT.Tooltip:SetOwner(self, anchor)
        BDT.Tooltip:ClearLines()

        if not BDT.db.profile.hideModuleTitle then
            BDT.FormatUtils:AddTooltipHeader(BDT.Tooltip, nil, L["Map Name"])
            BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, " ")
        end

        -- Zone info
        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, L["Zone:"], currentZone, 1, 0.82, 0, 1, 1, 1)
        
        if currentSubzone ~= "" and currentSubzone ~= currentZone then
            BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, L["Subzone:"], currentSubzone, 1, 0.82, 0, 1, 1, 1)
        end

        -- Coordinates
        if showCoordinates then
            local mapID = C_Map.GetBestMapForUnit("player")
            if mapID then
                local position = C_Map.GetPlayerMapPosition(mapID, "player")
                if position then
                    local x, y = position:GetXY()
                    if x and y then
                        local coordString = string.format("%.1f, %.1f", x * 100, y * 100)
                        BDT.FormatUtils:AddTooltipLine(BDT.Tooltip, nil, L["Coordinates:"], coordString, 1, 0.82, 0, 1, 1, 1)
                    end
                end
            end
        end

        BDT.Tooltip:Show()
    end)

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

    ----------------------------------------------------
    -- Event Handler
    ----------------------------------------------------
    local function OnEvent(self, event, ...)
        if event == "PLAYER_ENTERING_WORLD"
        or event == "ZONE_CHANGED"
        or event == "ZONE_CHANGED_INDOORS"
        or event == "ZONE_CHANGED_NEW_AREA" then
            UpdateMapName()
        end
    end

    f:SetScript("OnEvent", OnEvent)
    f:RegisterEvent("PLAYER_ENTERING_WORLD")
    f:RegisterEvent("ZONE_CHANGED")
    f:RegisterEvent("ZONE_CHANGED_INDOORS")
    f:RegisterEvent("ZONE_CHANGED_NEW_AREA")

    UpdateMapName()

    return f
end

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

return mod
