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

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

---@alias SpriteCollection PlayerImageBundle[]

---@param image pd_image
---@param drawInverted boolean
function maybeDrawInverted(image, x, y, drawInverted)
    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 PlayerImageBundle
---@param base pd_image
---@param isInverted boolean
function buildPlayerBundle(base, back, smile, frown, logo, isInverted)
    local smiling = gfx.image.new(base:getSize())
    gfx.lockFocus(smiling)
    base:draw(0, 0)
    Hat:draw(6, 0)
    smile:draw(5, 9)
    maybeDrawInverted(logo, 3, 25, isInverted)

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

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

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

    gfx.unlockFocus()

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

---@type SpriteCollection
AwayTeamSpriteGroup = nil

---@type SpriteCollection
HomeTeamSpriteGroup = nil

function replaceAwayLogo(logo)
    AwayTeamSpriteGroup = {
        buildPlayerBundle(DarkPlayerAwayBase, DarkPlayerAwayBack, DarkPlayerSmile, DarkPlayerFrown, logo, true),
        buildPlayerBundle(LightPlayerAwayBase, LightPlayerAwayBack, LightPlayerSmile, LightPlayerFrown, logo, true),
    }
end

function replaceHomeLogo(logo)
    HomeTeamSpriteGroup = {
        buildPlayerBundle(DarkPlayerHomeBase, DarkPlayerHomeBack, DarkPlayerSmile, DarkPlayerFrown, logo, true),
        buildPlayerBundle(LightPlayerHomeBase, LightPlayerHomeBack, LightPlayerSmile, LightPlayerFrown, logo, true),
    }
end

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