#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

import os
import sys
import tooncher.controls


def run(engine_pid,
        toggle_keysym_name=tooncher.controls.EXTENDED_CONTROLS_DEFAULT_TOGGLE_KEYSYM_NAME):
    ec = tooncher.controls.ExtendedControls(
        engine_pid=engine_pid,
        toggle_keysym_name=toggle_keysym_name,
    )
    ec.run()


def _init_argparser():

    import argparse
    argparser = argparse.ArgumentParser(
        description="Attach Extended Controls to an already running Toontown engine.",
    )
    argparser.add_argument(
        'engine_pid',
        type=int,
        help="process id",
    )
    argparser.add_argument(
        '--toggle', '-t',
        metavar='KEYSYM_NAME',
        dest='toggle_keysym_name',
        default=tooncher.controls.EXTENDED_CONTROLS_DEFAULT_TOGGLE_KEYSYM_NAME,
        help='key to turn extended keyboard controls on / off.'
            + ' any keysym name may be used'
            + ' (see XStringToKeysym & X11/keysymdef.h, '
            + ' default: %(default)s)',
    )
    return argparser


def main(argv):

    argparser = _init_argparser()
    try:
        import argcomplete
        argcomplete.autocomplete(argparser)
    except ImportError:
        pass
    args = argparser.parse_args(argv)

    run(**vars(args))

    return 0

if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
