import os
import logging
from argparse import ArgumentParser

from bigcode_fetcher.fetcher import search_projects_command
from bigcode_fetcher.downloader import download_projects_command
from bigcode_fetcher import constants


def create_parser():
    parser = ArgumentParser(prog="bigcode-fetcher",
                            description="Search and download projects from GitHub")
    parser.add_argument("-v", action="count", help="Increase verbosity", default=0)
    subparsers = parser.add_subparsers(dest="command")

    search_projects_parser = subparsers.add_parser("search", help="search projects")
    search_projects_parser.add_argument("-o", "--output", default="repositories.json")
    search_projects_parser.add_argument("--language", help="language to search")
    search_projects_parser.add_argument("--keyword", help="keyword to search for")
    search_projects_parser.add_argument("--keyword-in", help="place to search for keyword",
                                        dest="in")
    search_projects_parser.add_argument("--include-forks", help="includes forks",
                                        default=False, action="store_true", dest="fork")
    search_projects_parser.add_argument("--user", help="limit search to user or org")
    search_projects_parser.add_argument(
        "--sort", help="results sort order, default: stars", default="stars")
    search_projects_parser.add_argument(
        "--size", help="size of the repository, default: {0}".format(constants.DEFAULT_SIZE),
        default=constants.DEFAULT_SIZE)
    search_projects_parser.add_argument(
        "--max-repos", help="total repos to find, default: 100", type=int, default=100)
    search_projects_parser.add_argument(
        "--stars", help="number of stars, default: {0}".format(constants.DEFAULT_STARS),
        default=constants.DEFAULT_STARS)
    search_projects_parser.add_argument(
        "--licenses", help="acceptable license, default: {0}".format(constants.DEFAULT_LICENSES),
        default=constants.DEFAULT_LICENSES)
    search_projects_parser.add_argument(
        "--token", default=os.environ.get("GITHUB_TOKEN"),
        help="GitHub authentication token. Can be passed with GITHUB_TOKEN env variable")

    download_projects_parser = subparsers.add_parser("download", help="download projects in list")
    download_projects_parser.add_argument(
        "-i", "--input-file", required=True, help="JSON file generated by search command")
    download_projects_parser.add_argument(
        "-o", "--output-dir", required=True, help="directory where to download projects")
    download_projects_parser.add_argument(
        "--full", default=False, action="store_true",
        help="unless passed, only the latest revision will be fetched (i.e. git clone --depth 1)")

    return parser


def run_command(args):
    if args.command == "search":
        search_projects_command(args)
    elif args.command == "download":
        download_projects_command(args)


def run(args=None):
    parser = create_parser()
    namespace = parser.parse_args(args)
    log_level = logging.INFO - (namespace.v * 10)
    logging.basicConfig(level=log_level)
    if not namespace.command:
        parser.error("no command provided")
    run_command(namespace)
