local _, MerfinPlus = ...
MerfinPlus = LibStub("AceAddon-3.0"):NewAddon("MerfinPlus", "AceEvent-3.0")
local AceDB = LibStub("AceDB-3.0")
local AceConfig = LibStub("AceConfig-3.0")
local AceConfigDialog = LibStub("AceConfigDialog-3.0")
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local LSM = LibStub("LibSharedMedia-3.0")

local defaults = {
    profile = {
        pullStartTime = 0,
        pullExpTime = 0,
        pullTotalTime = 0,
        pullSenderName = 'Unknown',
        breakStartTime = 0,
        breakExpTime = 0,
        breakTotalTime = 0,
        breakSenderName = 'Unknown',
        lfgStartTimer = 0,
        lfgExpTimer = 0,
        font1 = "Expressway",
        font2 = "Expressway",
        font3 = "Expressway",
        font4 = "Expressway",
        font6 = "Expressway",
        font7 = "Expressway",
        font8 = "Expressway",
        font9 = "Expressway",
        font10 = "Expressway",
    }
}

function MerfinPlus:SetupOptions()
    local function GetFontList()
        local fonts = {}
        for name in pairs(LSM:HashTable("font")) do
            fonts[name] = name
        end
        return fonts
    end

    local function ConfirmReload()
        StaticPopupDialogs["MERFINPLUS_RELOAD_UI"] = {
            text = "A reload of the interface is required for this change to take effect.\n\nReload now?",
            button1 = "Yes",
            button2 = "No",
            OnAccept = function()
                ReloadUI()
            end,
            timeout = 0,
            whileDead = true,
            hideOnEscape = true,
            preferredIndex = 3,
        }
        StaticPopup_Show("MERFINPLUS_RELOAD_UI")
    end

    local mainOptions = {
        type = "group",
        name = "MerfinPlus",
        args = {
            description = {
                type = "description",
                name = "Welcome to MerfinPlus settings!\nUse the Fonts section to customize fonts.\n",
                fontSize = "medium",
                order = 1,
            },
            reset = {
                type = "execute",
                name = "Reset to Defaults",
                desc = "Reset all settings to default values.",
                confirm = true,
                confirmText = "Are you sure you want to reset all settings to defaults? This will reload the UI.",
                func = function()
                    self.db:ResetProfile()
                    ReloadUI()
                end,
                order = 2,
            },
        },
    }

    local fontOptions = {
        type = "group",
        name = "Fonts",
        args = {},
    }

    for i = 1, 10 do
        fontOptions.args["font" .. i] = {
            type = "select",
            name = "Font " .. i,
            desc = "Select font for element " .. i,
            values = GetFontList,
            get = function() return self.db.profile["font" .. i] end,
            set = function(_, value)
                self.db.profile["font" .. i] = value
                ConfirmReload()
            end,
            dialogControl = "LSM30_Font",
            order = i,
        }
    end

    AceConfigRegistry:RegisterOptionsTable("MerfinPlus", mainOptions)
    self.optionsFrame = AceConfigDialog:AddToBlizOptions("MerfinPlus", "MerfinPlus")

    AceConfigRegistry:RegisterOptionsTable("MerfinPlus_Fonts", fontOptions)
    AceConfigDialog:AddToBlizOptions("MerfinPlus_Fonts", "Fonts", "MerfinPlus")
end

function MerfinPlus:RegisterCustomFonts()
    local db = self:GetDB()

    local fonts = {
        {key = "font1", name = "Merfin Font 1"},
        {key = "font2", name = "Merfin Font 2"},
        {key = "font3", name = "Merfin Font 3"},
        {key = "font4", name = "Merfin Font 4"},
        {key = "font5", name = "Merfin Font 5"},
        {key = "font6", name = "Merfin Font 6"},
        {key = "font7", name = "Merfin Font 7"},
        {key = "font8", name = "Merfin Font 8"},
        {key = "font9", name = "Merfin Font 9"},
        {key = "font10", name = "Merfin Font 10"},
    }

    for _, font in ipairs(fonts) do
        local path = LSM:Fetch("font", db[font.key])
        if path then
            LSM:Register("font", font.name, path)
        else
            print(string.format("|cffff0000[MerfinPlus]|r Failed to register %s - invalid font path", font.name))
        end
    end
end

function MerfinPlus:OnInitialize()
    self.db = AceDB:New("MerfinPlusSaved", defaults, true)
    self:SetupOptions()
    self:RegisterCustomFonts()
end

function MerfinPlus:OnEnable()
    MerfinPlus:PullTimerEnable()
    MerfinPlus:LFGEnable()
end

function MerfinPlus:GetDB()
    if not self.db then
        error("[MerfinPlus] Database is not initialized yet!")
    end
    return self.db.profile
end

Merfin = Merfin or {}

Merfin.L = GetLocale()
Merfin.ForcedSoundLoc = false

local supportedLoc = {
    ['ruRU'] = true,
    ['enUS'] = true,
    ['zhCN'] = true,
}

Merfin.GetLocale = function()
    if Merfin.ForcedSoundLoc then
        return Merfin.ForcedSoundLoc
    elseif not supportedLoc[Merfin.L] then
        return 'enUS'
    else
        return Merfin.L
    end
end