import argparse
from pythonosc import dispatcher, osc_server
from pythonosc.osc_bundle import OscBundle
from pythonosc.osc_message import OscMessage

def bundle_handler(address, *args):
    for arg in args:
        if isinstance(arg, OscBundle):
            print(f"Received OSC bundle with timestamp: {arg.timestamp}")
            for element in arg.bundle_elements:
                if isinstance(element, OscMessage):
                    print(f"Message: {element.address}, Args: {element.params}")

def main():
    parser = argparse.ArgumentParser(description="OSC Bundle Receiver")
    parser.add_argument("--ip", default="127.0.0.1", help="The IP address to listen on")
    parser.add_argument("--port", type=int, default=5005, help="The port to listen on")
    args = parser.parse_args()

    disp = dispatcher.Dispatcher()
    disp.set_default_handler(bundle_handler)

    server = osc_server.ThreadingOSCUDPServer((args.ip, args.port), disp)
    print(f"Listening for OSC bundles on {args.ip}:{args.port}...")
    server.serve_forever()

if __name__ == "__main__":
    main()