--[[
============================================================================
Enhanced Toy Tracker  -  EToy/Core/ToyAPI.lua
============================================================================
Read access to the toy collection: collected state (via C_ToyBox) and the
human-readable source label for each src code in Data/ToyData.lua.

Toy names are NOT stored — they resolve at runtime via GetItemInfo, exactly
like mounts/pets. The browser handles async name arrival.
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETT = NS.ETT

-- src code -> { label, "rrggbb" colour }
ETT.SOURCE_LABELS = {
    V = { "Vendor",            "ffffd100" },
    D = { "Drop / Object",     "ffff8040" },
    Q = { "Quest",             "ff40ff40" },
    C = { "Crafted",           "ffffaa44" },
    A = { "Achievement",       "ffffcc00" },
    H = { "Holiday",           "ffff60ff" },
    E = { "World Event",       "ff66ccff" },
    P = { "Profession",        "ffd0a060" },
    X = { "PvP",               "ffff4040" },
    B = { "Black Market",      "ff999999" },
    F = { "Expansion Feature", "ff60d0ff" },
    M = { "Promotion",         "ffcc99ff" },
    I = { "Instance",          "ffff8888" },
    U = { "Other",             "ffaaaaaa" },
}

function ETT:SourceLabel(code)
    local e = self.SOURCE_LABELS[code or "U"] or self.SOURCE_LABELS.U
    return e[1], e[2]
end

-- Is a toy collected? C_ToyBox.PlayerHasToy is the canonical API on the
-- retail-engine client. Guarded so a missing API never errors the browser.
function ETT:IsCollected(toyID)
    if C_ToyBox and C_ToyBox.PlayerHasToy then
        local ok, has = pcall(C_ToyBox.PlayerHasToy, toyID)
        if ok then return has and true or false end
    end
    if PlayerHasToy then
        local ok, has = pcall(PlayerHasToy, toyID)
        if ok then return has and true or false end
    end
    return false
end

-- All toys as { {id=, src=}, ... } sorted by source group then id.
function ETT:GetToys()
    local data = self.ToyData
    if type(data) ~= "table" then return {} end
    local list = {}
    for id, info in pairs(data) do
        list[#list + 1] = { id = id, src = (info and info.src) or "U" }
    end
    return list
end

-- Collected / total counts (collected counts only toys present in our data).
function ETT:Counts()
    local total, owned = 0, 0
    local data = self.ToyData
    if type(data) == "table" then
        for id in pairs(data) do
            total = total + 1
            if self:IsCollected(id) then owned = owned + 1 end
        end
    end
    return owned, total
end

-- Alias matching the mount/pet API surface (owned, total).
function ETT:GetProgress()
    return self:Counts()
end

-- Toy display name. Async via GetItemInfo; falls back to a stable placeholder
-- so the row never renders blank.
function ETT:GetName(toyID)
    local name = GetItemInfo(toyID)
    if type(name) == "string" and name ~= "" then return name end
    return "Toy #" .. tostring(toyID)
end

-- Toy icon. GetItemInfoInstant is synchronous on the retail-engine client
-- (5th return = icon fileID), so the icon shows immediately even before the
-- async name has streamed in. Falls back to GetItemInfo, then a default.
function ETT:GetIcon(toyID)
    if GetItemInfoInstant then
        local ok, _, _, _, _, icon = pcall(GetItemInfoInstant, toyID)
        if ok and icon then return icon end
    end
    local tex = select(10, GetItemInfo(toyID))
    if tex then return tex end
    return "Interface\\Icons\\INV_Misc_Toy_10"
end

-- Item-quality colour escape (|cAARRGGBB). Toy quality is async via
-- GetItemInfo; until cached we colour by collected state instead.
local QUALITY_HEX = {
    [0] = "ff9d9d9d", [1] = "ffffffff", [2] = "ff1eff00", [3] = "ff0070dd",
    [4] = "ffa335ee", [5] = "ffff8000", [6] = "ffe6cc80",
}
function ETT:QualityColor(toyID, owned)
    local q = select(3, GetItemInfo(toyID))
    if type(q) == "number" and QUALITY_HEX[q] then
        return "|c" .. QUALITY_HEX[q]
    end
    return owned and "|cffffffff" or "|cff999999"
end
