require("test/setup")

---@return Fielding, number fielderCount
local function fieldersAtDefaultPositions()
    local fielding = Fielding.new()
    fielding:resetFielderPositions()

    local fielderCount = 0
    for _, fielder in pairs(fielding.fielders) do
        fielder.x = fielder.target.x
        fielder.y = fielder.target.y
        fielderCount = fielderCount + 1
    end

    return fielding, fielderCount
end

---@param x number
---@param y number
---@param z number | nil
function fakeBall(x, y, z)
    return {
        x = x,
        y = y,
        z = z or 0,
        heldBy = nil,
    }
end

function testBallPickedUpByNearbyFielders()
    local fielding, fielderCount = fieldersAtDefaultPositions()
    luaunit.assertIs("table", type(fielding))
    luaunit.assertIs("table", type(fielding.fielders))
    luaunit.assertEquals(9, fielderCount)

    local ball = fakeBall(-100, -100, -100)
    fielding:updateFielderPositions(ball, 0.01)
    luaunit.assertIsNil(ball.heldBy, "Ball should not be held by a fielder yet")

    local secondBaseman = fielding.fielders.second
    ball.x = secondBaseman.x
    ball.y = secondBaseman.y
    fielding:updateFielderPositions(ball, 0.01)
    luaunit.assertIs(secondBaseman, ball.heldBy, "Ball should be held by the nearest fielder")
end

os.exit(luaunit.LuaUnit.run())
