-- RUQuestLog_Core.lua
-- Общие функции для всех модулей RUQuestLog

-- защита от двойной загрузки
if _G.RUQL_CORE_LOADED then return end
_G.RUQL_CORE_LOADED = true

_G.RUQL_ADDON_NAME = 'RUQuestLog'

function _G.RUQL_setRuFontSafe(fs, size, flags)
    if not (fs and fs.SetFont) then return false end

    local path1 = "Interface\\AddOns\\" .. tostring(_G.RUQL_ADDON_NAME or "") .. "\\Fonts\\Morpheus.ttf"
    local path2 = "Fonts\\Morpheus.ttf"

    flags = flags or ""
    size = size or 16

    local ok = false

    local ok1 = pcall(function()
        ok = fs:SetFont(path1, size, flags) and true or false
    end)
    if ok1 and ok then
        return true
    end

    local ok2 = pcall(function()
        ok = fs:SetFont(path2, size, flags) and true or false
    end)
    if ok2 and ok then
        return true
    end

    return false
end

function _G.RUQL_setRuTitleFont(fs)
    local ok = _G.RUQL_setRuFontSafe(fs, 20, "")
    if ok and fs and fs.SetShadowOffset then
        pcall(function() fs:SetShadowOffset(1, -1) end)
    end
    return ok
end

--------------------------------------------------
-- КЛАССЫ
--------------------------------------------------

_G.RUQL_CLASS_RU = {
    WARRIOR = "воин",
    PALADIN = "паладин",
    HUNTER  = "охотник",
    ROGUE   = "разбойник",
    PRIEST  = "жрец",
    SHAMAN  = "шаман",
    MAGE    = "маг",
    WARLOCK = "чернокнижник",
    DRUID   = "друид",
}

_G.RUQL_CLASS_COLOR = {
    WARRIOR = "c79c6e",
    PALADIN = "f58cba",
    HUNTER  = "abd473",
    ROGUE   = "fff569",
    PRIEST  = "ffffff",
    SHAMAN  = "0070de",
    MAGE    = "69ccf0",
    WARLOCK = "9482c9",
    DRUID   = "ff7d0a",
}

_G.RUQL_RACE_RU = {
    HUMAN     = "человек",
    ORC       = "орк",
    DWARF     = "дворф",
    NIGHTELF  = "ночной эльф",
    SCOURGE   = "нежить",
    TAUREN    = "таурен",
    GNOME     = "гном",
    TROLL     = "тролль",
    BLOODELF  = "эльф крови",
    DRAENEI   = "дреней",
}

--------------------------------------------------
-- ИГРОК
--------------------------------------------------

function _G.RUQL_getPlayerName()
    local n = UnitName and UnitName("player")
    return n or ""
end

function _G.RUQL_getPlayerClassRu()
    if not UnitClass then return "" end
    local _, classFile = UnitClass("player")
    return (_G.RUQL_CLASS_RU[classFile]) or ""
end

function _G.RUQL_getPlayerRaceRu()
    if not UnitRace then return "" end
    local _, raceFile = UnitRace("player") -- raceFile: HUMAN, ORC, ...
    return (_G.RUQL_RACE_RU[raceFile]) or ""
end

--------------------------------------------------
-- ТЕКСТ
--------------------------------------------------

function _G.RUQL_bold(text)
    local _, classFile = UnitClass("player")
    local color = _G.RUQL_CLASS_COLOR[classFile] or "ffffff"
    return "|cff" .. color .. tostring(text or "") .. "|r"
end

function _G.RUQL_normalize(text)
    if type(text) ~= "string" then return nil end
    return text:gsub("\r\n", "\n"):gsub("\r", "\n")
end

function _G.RUQL_post(text)
    if type(text) ~= "string" or text == "" then
        return text
    end

    text = _G.RUQL_normalize(text)

    local sex = UnitSex and UnitSex("player") or 2 -- 2 male, 3 female

    -- <вариант1/вариант2>
    text = text:gsub("<([^<>/]+)/([^<>]+)>", function(a, b)
        if sex == 3 then return b end
        return a
    end)

    -- {mog}
    text = text:gsub("{mog}", (sex == 3) and "могла" or "мог")

    local pname  = _G.RUQL_getPlayerName()
    local pclass = _G.RUQL_getPlayerClassRu()
    local prace = _G.RUQL_getPlayerRaceRu()

    -- RU
    text = text:gsub("<имя>",    _G.RUQL_bold(pname))
    text = text:gsub("<Имя>",    _G.RUQL_bold(pname))
    text = text:gsub("<ИМЯ>",    _G.RUQL_bold(pname))

    text = text:gsub("<класс>",  _G.RUQL_bold(pclass))
    text = text:gsub("<Класс>",  _G.RUQL_bold(pclass))
    text = text:gsub("<КЛАСС>",  _G.RUQL_bold(pclass))

    -- EN
    text = text:gsub("<name>",   _G.RUQL_bold(pname))
    text = text:gsub("<Name>",   _G.RUQL_bold(pname))
    text = text:gsub("<NAME>",   _G.RUQL_bold(pname))

    text = text:gsub("<class>",  _G.RUQL_bold(pclass))
    text = text:gsub("<Class>",  _G.RUQL_bold(pclass))
    text = text:gsub("<CLASS>",  _G.RUQL_bold(pclass))

    -- RU
    text = text:gsub("<раса>",  _G.RUQL_bold(prace))
    text = text:gsub("<Раса>",  _G.RUQL_bold(prace))
    text = text:gsub("<РАСА>",  _G.RUQL_bold(prace))

    -- EN
    text = text:gsub("<race>",  _G.RUQL_bold(prace))
    text = text:gsub("<Race>",  _G.RUQL_bold(prace))
    text = text:gsub("<RACE>",  _G.RUQL_bold(prace))

    return text
end