import argparse
import asyncio

from clarabot_nano.common.i18n import LOCALIZATION
from clarabot_nano.core.bootstrap import core_bootstrap_login
from clarabot_nano.core.process import CoreProcess

LOCALIZATION.install()


def start_core(parsed_args, *args, cls=CoreProcess):
    if parsed_args.bootstrap_login:
        asyncio.run(
            core_bootstrap_login(
                parsed_args.local_config,
                parsed_args.bootstrap_config_dir,
                parsed_args.bootstrap_data_dir,
            )
        )
    else:
        cls.startup(
            parsed_args.local_config,
            parsed_args.headless,
            parsed_args.bootstrap_config_dir,
            parsed_args.bootstrap_data_dir,
            *args,
        )


def main():
    parser = argparse.ArgumentParser("Clarabot Nano - Core process (the overseer)")
    parser.add_argument(
        "-c",
        "--local_config",
        help="The local configuration file of a Nano instance. "
        "If not provided a default configuration will be used or created under the user's home.",
    )
    parser.add_argument(
        "--headless",
        action="store_true",
        help="Start without graphical interface even if it is available.",
    )
    parser.add_argument(
        "--bootstrap_login",
        action="store_true",
        help="Start a command line utility that handles login or account-change of the instance.",
    )
    parser.add_argument(
        "--bootstrap_config_dir",
        help='Path where configs other than "local.yaml" shall reside for an explicit new instance. '
        'Required if "--local_config" is specified to a new path, ignored otherwise.',
    )
    parser.add_argument(
        "--bootstrap_data_dir",
        help="Path where the data shall be stored for an explicit new instance. "
        'Required if "--local_config" is specified to a new path, ignored otherwise.',
    )
    args = parser.parse_args()
    start_core(args)


if __name__ == "__main__":
    main()
