local ADDON_NAME, ns = ...

ns = ns or {}
ns.SavedVariablesName = "QuickLoadoutManagerDB"
ns.TemplateTitle = "Quick Loadout"
ns.TemplateSubtitle = "Manager"

local defaults = {
    minimap = {
        hide = false,
        minimapPos = 220,
    },
    window = {
        point = nil,
        relPoint = nil,
        x = 0,
        y = 0,
    },
    sortMode = "name_asc",
}

local function copyDefaults(source, destination)
    for key, value in pairs(source) do
        if type(value) == "table" then
            if type(destination[key]) ~= "table" then
                destination[key] = {}
            end
            copyDefaults(value, destination[key])
        elseif destination[key] == nil then
            destination[key] = value
        end
    end
end

function ns:Print(message)
    DEFAULT_CHAT_FRAME:AddMessage("|cff7C4FE0" .. ns.TemplateTitle .. "|r: " .. tostring(message))
end

function ns.RegisterFonts()
    local lsm = LibStub and LibStub("LibSharedMedia-3.0", true)
    if not lsm then
        return
    end
    local fontRoot = "Interface\\AddOns\\" .. ADDON_NAME .. "\\Fonts\\"
    lsm:Register("font", "Figtree Bold", fontRoot .. "Figtree-Bold.ttf")
    lsm:Register("font", "Figtree Medium", fontRoot .. "Figtree-Medium.ttf")
    lsm:Register("font", "Figtree Regular", fontRoot .. "Figtree-Regular.ttf")
end

local function buildMinimapTooltip(tooltip)
    if not tooltip then
        return
    end
    tooltip:AddLine(ns.TemplateTitle .. " " .. ns.TemplateSubtitle)
    tooltip:AddLine("Left-click to open loadout manager.", 0.85, 0.85, 0.85)
end

function ns.RegisterMinimapButton()
    if not ns.db then
        return
    end
    ns.db.minimap = ns.db.minimap or {}
    if ns.db.minimap.hide == nil then
        ns.db.minimap.hide = false
    end
    if ns.db.minimap.minimapPos == nil then
        ns.db.minimap.minimapPos = 220
    end

    local ldb = LibStub and LibStub("LibDataBroker-1.1", true)
    local libDBIcon = LibStub and LibStub("LibDBIcon-1.0", true)
    if not ldb or not libDBIcon then
        return
    end

    if not ns.minimapLauncher then
        ns.minimapLauncher = ldb:NewDataObject("QuickLoadoutManager", {
            type = "launcher",
            text = ns.TemplateTitle,
            icon = "Interface\\AddOns\\" .. ADDON_NAME .. "\\Assets\\Logo",
            OnClick = function(_, button)
                if button == "LeftButton" and ns.ToggleMainWindow then
                    ns:ToggleMainWindow()
                end
            end,
            OnTooltipShow = function(tooltip)
                buildMinimapTooltip(tooltip)
            end,
        })
    end

    if not libDBIcon:IsRegistered("QuickLoadoutManager") then
        libDBIcon:Register("QuickLoadoutManager", ns.minimapLauncher, ns.db.minimap)
    end

    if ns.db.minimap.hide then
        libDBIcon:Hide("QuickLoadoutManager")
    else
        libDBIcon:Show("QuickLoadoutManager")
    end
end

function ns.ToggleMinimapButton()
    if not ns.db or not ns.db.minimap then
        return
    end
    ns.db.minimap.hide = not ns.db.minimap.hide
    ns.RegisterMinimapButton()
end

function ns.Profile()
    return ns.db or {}
end

function ns.UpdateProfileSetting(key, value)
    if not ns.db then
        return false
    end
    ns.db[key] = value
    return true
end

function ns.CanEditConfig()
    return not InCombatLockdown()
end

local events = CreateFrame("Frame")
events:RegisterEvent("PLAYER_LOGIN")
events:SetScript("OnEvent", function(_, event)
    if event ~= "PLAYER_LOGIN" then
        return
    end

    QuickLoadoutManagerDB = QuickLoadoutManagerDB or {}
    copyDefaults(defaults, QuickLoadoutManagerDB)

    if type(QuickLoadoutManagerDB.global) ~= "table" then
        QuickLoadoutManagerDB.global = {}
    end
    if type(QuickLoadoutManagerDB.global.loadoutMeta) ~= "table" then
        QuickLoadoutManagerDB.global.loadoutMeta = {}
    end

    ns.db = QuickLoadoutManagerDB

    ns.RegisterFonts()
    ns.RegisterMinimapButton()

    if ns.LoadoutData and ns.LoadoutData.Initialize then
        ns.LoadoutData:Initialize()
    end
end)
