"""
rescriptoon
Copyright (C) 2018-2019 Fabian Peter Hammerle <fabian@hammerle.me>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
"""

import argparse
import logging

from Xlib import X, XK

import rescriptoon

_DEFAULT_TOGGLE_KEYSYM_NAME = "grave"

_VERBOSITY_LEVELS = [logging.WARNING, logging.INFO, logging.DEBUG]


def main() -> None:
    argparser = argparse.ArgumentParser(
        description="Attach rescriptoon to running Toontown Rewritten engines.",
    )
    argparser.add_argument(
        "--toggle",
        "-t",
        metavar="KEYSYM_NAME",
        dest="toggle_keysym_name",
        default=_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)",
    )
    argparser.add_argument(
        "--verbose",
        "-v",
        action="count",
        dest="verbosity",
        default=0,
        help="repeat to further increase verbosity",
    )
    args = argparser.parse_args()
    logging.basicConfig(
        format="%(asctime)s %(levelname)s: %(message)s",
        datefmt="%H:%I:%S",
        level=_VERBOSITY_LEVELS[min(args.verbosity, len(_VERBOSITY_LEVELS) - 1)],
    )
    toggle_keysym = XK.string_to_keysym(args.toggle_keysym_name)
    if toggle_keysym == X.NoSymbol:
        raise ValueError(
            "controls toggle: unknown keysym name '{}'".format(args.toggle_keysym_name)
        )
    rescriptoon.Overlay(toggle_keysym=toggle_keysym).run()
