local _, ns = ...

local WoWApi = ns.WoWApi or {}
local getCurrencyInfoByID = WoWApi.GetCurrencyInfoByID

local WeeklyCurrency = ns.WeeklyCurrency or {}
ns.WeeklyCurrency = WeeklyCurrency

local function buildWeeklyCurrencyState(currencyIDs, fallbackName, fallbackMax, tooltipHint, inventoryFormat)
    local bestState

    for _, currencyID in ipairs(currencyIDs or {}) do
        local info = getCurrencyInfoByID(currencyID)
        if info then
            local currentQuantity = math.max(0, tonumber(info.quantity) or 0)
            local earnedQuantity = tonumber(info.quantityEarnedThisWeek)
            local maxQuantity = tonumber(info.maxWeeklyQuantity)

            if info.useTotalEarnedForMaxQty and (tonumber(info.maxQuantity) or 0) > 0 then
                earnedQuantity = tonumber(info.totalEarned) or currentQuantity
                maxQuantity = tonumber(info.maxQuantity) or fallbackMax
            else
                earnedQuantity = earnedQuantity or currentQuantity
                if not maxQuantity or maxQuantity <= 0 then
                    maxQuantity = tonumber(info.maxQuantity) or fallbackMax
                end
            end

            local candidate = {
                currencyID = currencyID,
                name = info.name or fallbackName,
                quantity = math.max(0, tonumber(earnedQuantity) or 0),
                maxQuantity = math.max(0, tonumber(maxQuantity) or tonumber(fallbackMax) or 0),
                currentQuantity = currentQuantity,
            }

            candidate.capped = candidate.maxQuantity > 0 and candidate.quantity >= candidate.maxQuantity
            candidate.text = candidate.maxQuantity > 0 and string.format("%d/%d", candidate.quantity, candidate.maxQuantity)
                or string.format("%d", candidate.quantity)
            candidate.tooltipTitle = candidate.name or fallbackName
            candidate.tooltipLines = { tooltipHint }

            if inventoryFormat and inventoryFormat ~= "" then
                candidate.tooltipLines[#candidate.tooltipLines + 1] = string.format(inventoryFormat, candidate.currentQuantity)
            end

            local chooseCandidate = false
            if not bestState then
                chooseCandidate = true
            elseif (candidate.maxQuantity or 0) > (bestState.maxQuantity or 0) then
                chooseCandidate = true
            elseif (candidate.maxQuantity or 0) == (bestState.maxQuantity or 0)
                and (candidate.quantity or 0) > (bestState.quantity or 0) then
                chooseCandidate = true
            end

            if chooseCandidate then
                bestState = candidate
            end
        end
    end

    if bestState then
        return bestState
    end

    local emptyState = {
        currencyID = currencyIDs and currencyIDs[1] or nil,
        name = fallbackName,
        quantity = 0,
        maxQuantity = math.max(0, tonumber(fallbackMax) or 0),
        currentQuantity = 0,
    }
    emptyState.capped = emptyState.maxQuantity > 0 and emptyState.quantity >= emptyState.maxQuantity
    emptyState.text = emptyState.maxQuantity > 0 and string.format("%d/%d", emptyState.quantity, emptyState.maxQuantity)
        or string.format("%d", emptyState.quantity)
    emptyState.tooltipTitle = emptyState.name or fallbackName
    emptyState.tooltipLines = { tooltipHint }
    if inventoryFormat and inventoryFormat ~= "" then
        emptyState.tooltipLines[#emptyState.tooltipLines + 1] = string.format(inventoryFormat, emptyState.currentQuantity)
    end
    return emptyState
end

WeeklyCurrency.BuildWeeklyCurrencyState = buildWeeklyCurrencyState
