local addonName, ns = ...

-- ========================================================================
-- 本地化
-- ========================================================================
ns.L = setmetatable({}, {
    __index = function(t, key) return key end
})

-- ========================================================================
-- 存档初始化
-- ========================================================================
local function MergeDefaults(target, source)
    for k, v in pairs(source) do
        if type(v) == "table" then
            if target[k] == nil then target[k] = {} end
            MergeDefaults(target[k], v)
        elseif target[k] == nil then
            target[k] = v
        end
    end
end

EventUtil.ContinueOnAddOnLoaded(addonName, function()
    ChatFloaterDB = ChatFloaterDB or {}
    local previousSchema = tonumber(ChatFloaterDB.schemaVersion) or 0

    -- v2.0.2 migration: the old combined Instance/Raid toggle became two independent buttons.
    if ChatFloaterDB.isRaid == nil and ChatFloaterDB.isInstanceRaid ~= nil then
        ChatFloaterDB.isRaid = ChatFloaterDB.isInstanceRaid
    end
    if ChatFloaterDB.isInstance == nil and ChatFloaterDB.isInstanceRaid ~= nil then
        ChatFloaterDB.isInstance = ChatFloaterDB.isInstanceRaid
    end

    if ns.defaults then
        MergeDefaults(ChatFloaterDB, ns.defaults)
    end

    -- Remove obsolete keys from pre-3.1 builds.
    ChatFloaterDB.isTabSwitch = nil
    ChatFloaterDB.isShortChannelName = nil
    ChatFloaterDB.chatVisualStyle = nil

    ChatFloaterDB.iconTheme = nil

    if ChatFloaterDB.bigChatBoxIntegration == nil then
        ChatFloaterDB.bigChatBoxIntegration = true
    end

    -- 1.0.3: Border is configured directly in the main GUI.  Convert the
    -- old Style + SharedMedia pair into the single Border Texture value.
    if previousSchema < 325 then
        local oldStyle = ChatFloaterDB.chatPanelBorderStyle
        local oldMedia = ChatFloaterDB.chatPanelBorderMedia

        if oldStyle == "SHAREDMEDIA" and type(oldMedia) == "string" and oldMedia ~= "" then
            ChatFloaterDB.chatPanelBorderTexture = "LSM:" .. oldMedia
        elseif oldStyle == "SOLID" then
            ChatFloaterDB.chatPanelBorderTexture = "SOLID"
        elseif oldStyle == "NONE" then
            ChatFloaterDB.chatPanelBorderTexture = "NONE"
        elseif type(ChatFloaterDB.chatPanelBorderTexture) ~= "string" then
            ChatFloaterDB.chatPanelBorderTexture = "BLIZZARD_TOOLTIP"
        end

        -- 3.2.3 temporarily used a very thin default. Keep the restored
        -- Blizzard Tooltip look for users who never customized that value.
        if ChatFloaterDB.chatPanelBorderSize == nil or ChatFloaterDB.chatPanelBorderSize <= 5 then
            ChatFloaterDB.chatPanelBorderSize = 10
        end
    end

    if type(ChatFloaterDB.chatPanelBorderTexture) ~= "string" or ChatFloaterDB.chatPanelBorderTexture == "" then
        ChatFloaterDB.chatPanelBorderTexture = "BLIZZARD_TOOLTIP"
    end

    if ChatFloaterDB.chatPanelBorderOffset == nil then
        ChatFloaterDB.chatPanelBorderOffset = 0
    end

    if ChatFloaterDB.chatPanelBorderOpacity == nil then
        ChatFloaterDB.chatPanelBorderOpacity = 0.90
    end

    if type(ChatFloaterDB.chatPanelBorderColor) ~= "table" then
        ChatFloaterDB.chatPanelBorderColor = { r = 0.38, g = 0.44, b = 0.52 }
    end

    -- Old internal keys are no longer used by the UI/rendering path.
    ChatFloaterDB.chatPanelBorderStyle = nil
    ChatFloaterDB.chatPanelBorderMedia = nil
    ChatFloaterDB.schemaVersion = 325

    -- Slash commands
    SLASH_ChatFloater1 = "/chatfloat"
    SLASH_ChatFloater2 = "/cf"
    SLASH_ChatFloater3 = "/chf"
    SLASH_ChatFloater4 = "/chatfloater"
    SlashCmdList["ChatFloater"] = function(msg)
        msg = type(msg) == "string" and msg:lower() or ""
        if msg == "reset" then
            if ns.ResetBarPosition then ns.ResetBarPosition() end
            return
        end
        if ns.ToggleGUI then ns.ToggleGUI() end
    end
end)
