#!/usr/bin/env python3
"""LICENSE
Copyright 2017 Hermann Krumrey <hermann@krumreyh.com>

This file is part of bundesliga-tippspiel.

bundesliga-tippspiel 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.

bundesliga-tippspiel 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 bundesliga-tippspiel.  If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

import os
import argparse
from betbot import sentry_dsn
from betbot.prediction import predictors
from betbot.main import multi_main
from puffotter.init import cli_start, argparse_add_verbosity
from puffotter.env import load_env_file


def main(args: argparse.Namespace):
    """
    The main function of the betbot
    :param args: The command line arguments
    :return: None
    """
    if args.env_file is not None:
        load_env_file(args.env_file)

    config = []
    for predictor in predictors:
        base = predictor.name().upper().replace("-", "_")
        user_key = base + "_USER"
        pass_key = base + "_PASS"
        username = os.environ.get(user_key)
        password = os.environ.get(pass_key)
        if username is not None and password is not None:
            config.append((predictor.name(), username, password))

    multi_main(args.url, config, args.loop)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--url", default="https://hk-tippspiel.com")
    parser.add_argument("--loop", action="store_true",
                        help="Place bets once an hour")
    parser.add_argument("--env-file",
                        help="Specifies an environment file in "
                             "which credentials may be stored")
    argparse_add_verbosity(parser)
    cli_start(main, parser, "Thanks for using betbot", "betbot", sentry_dsn)
