-- selene: allow(shadowing)
local gfx = playdate.graphics

---@alias SpriteCollection { smiling: pd_image, lowHat: pd_image, frowning: pd_image, back: pd_image }

---@param image pd_image
---@param drawInverted boolean
function maybeDrawInverted(image, x, y, drawInverted)
    -- TODO: Bring logo up a pixel on the dark player base?
    local drawMode = gfx.getImageDrawMode()
    if drawInverted then
        gfx.setImageDrawMode(gfx.kDrawModeInverted)
    end
    image:draw(x, y)
    gfx.setImageDrawMode(drawMode)
end

--- TODO: Custom names on jerseys?
---@return SpriteCollection
---@param base pd_image
---@param isDark boolean
function buildCollection(base, back, logo, isDark)
    local smiling = gfx.image.new(base:getSize())
    gfx.lockFocus(smiling)
    base:draw(0, 0)
    Hat:draw(6, 0)
    PlayerSmile:draw(5, 9)
    maybeDrawInverted(logo, 3, 25, isDark)

    local lowHat = gfx.image.new(base:getSize())
    gfx.lockFocus(lowHat)
    base:draw(0, 0)
    Hat:draw(6, 2)
    PlayerSmile:draw(5, 9)
    maybeDrawInverted(logo, 3, 25, isDark)

    local frowning = gfx.image.new(base:getSize())

    gfx.lockFocus(frowning)
    base:draw(0, 0)
    maybeDrawInverted(logo, 3, 25, isDark)
    Hat:draw(6, 0)
    PlayerFrown:draw(5, 9)

    gfx.unlockFocus()

    return {
        smiling = smiling,
        lowHat = lowHat,
        frowning = frowning,
        back = back,
    }
end

--selene: allow(unscoped_variables)
---@type SpriteCollection
AwayTeamSprites = nil

--selene: allow(unscoped_variables)
---@type SpriteCollection
HomeTeamSprites = nil

function replaceAwayLogo(logo)
    AwayTeamSprites = buildCollection(DarkPlayerBase, DarkPlayerBack, logo, true)
end

function replaceHomeLogo(logo)
    HomeTeamSprites = buildCollection(LightPlayerBase, LightPlayerBack, logo, false)
end

replaceAwayLogo(Logos[1].image)
replaceHomeLogo(Logos[2].image)
