local MAJOR, MINOR = "LibMatic-MarkerPack-1.0", 1
assert(LibStub, MAJOR .. " requires LibStub")
local lib = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end

local DEFAULT_EMBED = "Libs\\LibMatic\\"

lib.INDICATOR_STYLES = {
    { key = "chevron", name = "Chevron", up = "UpgradeChevron.tga", down = "DowngradeChevron.tga", upAlt = "UpgradeChevronALT.tga" },
    { key = "arrow", name = "Up/Down Arrow", up = "UpgradeArrowIcon.tga", down = "Downgrade-arrow-icon.tga", upAlt = "UpgradeArrowALTIcon.tga" },
}

lib.SKULL_STYLES = {
    { key = "fanged", name = "Fanged", file = "bis-skull-v2.tga" },
    { key = "grim", name = "Grim", file = "bis-skull-v3.tga" },
    { key = "classic", name = "Classic", file = "bis-skull.tga" },
}

lib.DEFAULT_INDICATOR = "chevron"
lib.DEFAULT_SKULL = "fanged"

local function findStyle(list, key)
    for _, entry in ipairs(list) do
        if entry.key == key then return entry end
    end
    return list[1]
end

function lib:GetBasePath(addon, embed)
    embed = embed or DEFAULT_EMBED
    if embed ~= "" and embed:sub(-1) ~= "\\" then embed = embed .. "\\" end
    return "Interface\\AddOns\\" .. addon .. "\\" .. embed .. "LibMatic-MarkerPack-1.0\\Textures\\"
end

function lib:GetPath(addon, file, embed)
    return self:GetBasePath(addon, embed) .. file
end

function lib:GetIndicator(addon, style, direction, embed, alt)
    local entry = findStyle(self.INDICATOR_STYLES, style or self.DEFAULT_INDICATOR)
    local file
    if direction == "down" then
        file = entry.down
    elseif alt and entry.upAlt then
        file = entry.upAlt
    else
        file = entry.up
    end
    return self:GetPath(addon, file, embed)
end

function lib:GetSkull(addon, style, embed)
    local entry = findStyle(self.SKULL_STYLES, style or self.DEFAULT_SKULL)
    return self:GetPath(addon, entry.file, embed)
end

function lib:Markup(path, size)
    size = size or 18
    return ("|T%s:%d:%d|t "):format(path, size, size)
end

function lib:IndicatorOptionValues(addon, embed, names)
    names = names or {}
    local values, sorting = {}, {}
    for _, entry in ipairs(self.INDICATOR_STYLES) do
        values[entry.key] = self:Markup(self:GetPath(addon, entry.up, embed))
            .. self:Markup(self:GetPath(addon, entry.down, embed))
            .. (names[entry.key] or entry.name)
        sorting[#sorting + 1] = entry.key
    end
    return values, sorting
end

function lib:SkullOptionValues(addon, embed, names)
    names = names or {}
    local values, sorting = {}, {}
    for _, entry in ipairs(self.SKULL_STYLES) do
        values[entry.key] = self:Markup(self:GetPath(addon, entry.file, embed)) .. (names[entry.key] or entry.name)
        sorting[#sorting + 1] = entry.key
    end
    return values, sorting
end
