#!python

import warnings
from argparse import ArgumentParser


with warnings.catch_warnings():
    warnings.simplefilter("ignore")

    print("Loading AFQ libraries...")

    import AFQ
    import AFQ.utils.bin as afb

    import logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('pyAFQ')

usage = \
    f"""pyAFQ /path/to/afq_config.toml

Runs full AFQ processing as specified in the configuration file.

For details about configuration, see instructions in:
https://yeatmanlab.github.io/pyAFQ/usage/config.html

The default configuration file looks like:

"""


def parse_cli(help_msg):
    cli_parser = ArgumentParser(usage=usage + afb.dict_to_toml(arg_dict))

    cli_parser.add_argument(
        dest='config',
        action="store",
        help="Path to config file. For example, /path/to/afq_config.toml")

    cli_parser.add_argument(
        '-g',
        '--generate-config-only',
        dest='generate_toml',
        action="store_true",
        default=False,
        help="Generate a default config file at the path"
        + " specified without running pyAFQ.")

    cli_parser.add_argument(
        '-o',
        '--overwrite-config',
        dest='overwrite',
        action="store_true",
        default=False,
        help="Overwrite config file at the path"
        + " with current arguments and comments,"
        + " preserving previous defaults when applicable.")

    cli_parser.add_argument(
        '-v',
        '--verbose',
        dest='verbose',
        action="store_true",
        default=False,
        help="Verbose when reading TOML file")

    cli_parser.add_argument(
        '-c',
        '--call',
        dest='to_call',
        default="export_all",
        help="AFQ.api method to call using the specified config file."
        + " Defaults to 'export_all', which will perform the entire"
        + " tractometry pipeline.")

    cli_parser.add_argument(
        '-t', '--notrack', action="store_true", default=False,
        help="Disable the use of pyAFQ being recorded by Google Analytics. ")

    opts = cli_parser.parse_args()

    if not opts.notrack:
        logger.info(
            "Your use of pyAFQ is being recorded using Google Analytics. "
            "For more information, please refer to the pyAFQ documentation: "
            "https://yeatmanlab.github.io/pyAFQ/usage.html#usage-tracking-with-google-analytics. "  # noqa
            "To turn this off, use the `--notrack` flag when using the pyAFQ CLI")
        import popylar
        popylar.track_event(AFQ._ga_id, "pyAFQ_cli", "CLI called")

    return opts.config, opts.generate_toml, opts.overwrite, opts.verbose, opts.to_call


if __name__ == '__main__':
    arg_dict = afb.func_dict_to_arg_dict(logger=logger)
    config_file, generate_only, overwrite, verbose, to_call =\
        parse_cli(arg_dict)
    if generate_only:
        afb.generate_config(config_file, arg_dict, overwrite, logger=logger)
    else:
        afb.parse_config_run_afq(
            config_file,
            arg_dict,
            to_call=to_call,
            overwrite=overwrite,
            verbose=verbose,
            logger=logger)
