-- TODO? Some other stats
-- * Scroll left and right through games that go into extra innings
-- * Scroll up and down through other stats.
--   + Balls and strikes
--   + Batting average
--   + Farthest hit ball

---@return TeamInningData
local function newTeamInning()
    return {
        score = 0,
        pitching = {
            balls = 0,
            strikes = 0,
        },
        hits = {},
    }
end

---@return table<TeamId, TeamInningData>
local function newInning()
    return {
        home = newTeamInning(),
        away = newTeamInning(),
    }
end

---@alias TeamInningData { score: number, pitching: { balls: number, strikes: number }, hits: XyPair[] }

--- E.g. statistics[1].home.pitching.balls
---@class Statistics
---@field innings (table<TeamId, TeamInningData>)[]
Statistics = {}

---@return Statistics
function Statistics.new()
    return setmetatable({
        innings = { newInning() },
    }, { __index = Statistics })
end

function Statistics:pushInning()
    self.innings[#self.innings + 1] = newInning()
end

---@param inning number
---@param finalInning number
---@param battingTeam TeamId
---@return boolean gameOver
function Statistics:gameIsOver(inning, finalInning, battingTeam)
    local homeScore, awayScore = utils.totalScores(self)
    local isFinalInning = inning >= finalInning
    local gameOver = isFinalInning and battingTeam == "home" and awayScore ~= homeScore
    gameOver = gameOver or battingTeam == "away" and isFinalInning and homeScore > awayScore
    return gameOver
end

-- luacheck: ignore
if not playdate or playdate.TEST_MODE then
    return Statistics
end
