#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import sys

from lib.core.data import logger
from lib.core.enums import CUSTOM_LOGGING
from lib.core.settings import PASSWORD_SUPPORTED
from lib.core.settings import VERSION
from lib.request.proxy import checkProxyFormat
from lib.request.proxy import updateProxy

def cmdLineParser():
    """
    Parse the cmdline parameters
    """
    parser = argparse.ArgumentParser(prog = "cracker",
                                        formatter_class = argparse.RawTextHelpFormatter,
                                        description = "A useful tool to crack some passwords",
                                        epilog = "examples:"
                                                    "\n\ncracker 5d41402abc4b2a76b9719d911017c592"
                                                    "\ncracker -t md5 21232f297a57a5a743894a0e4a801fc3"
                                                    "\ncracker -r ~/password.txt")

    parser.add_argument('-l', '--list', help = "list passwords we supported", 
                            action = "store_true")
    parser.add_argument('-f', '--file', help = "read passwords from given file")
    parser.add_argument('-t', '--type', help = "force the password type")
    parser.add_argument('-p', '--proxy', help = "(http|https|socks4|socks5)://address[:port]")
    parser.add_argument('-v', metavar = "VERBOSE", dest = "verbose", 
                            help = "verbosily level: 0-4 (default 1)"
                            "\n0 : Show only critical and error messages"
                            "\n1 : Show also warning and info messages"
                            "\n2 : Show also debug messages"
                            "\n3 : Show also HTTP requests"
                            "\n4 : Show also HTTP responses", 
                            choices = range(5), type = int, default = 1)
    parser.add_argument('--version', help = "print version", action = "version", 
                            version = "%(prog)s {!s}".format(VERSION))
    parser.add_argument('password', help = 'password to crack', type = str, nargs = "*")

    args = parser.parse_args()

    # Set verbosity level
    logger.setLevel([40, 20, 10, CUSTOM_LOGGING.TRAFFIC_OUT, CUSTOM_LOGGING.TRAFFIC_IN][args.verbose])

    # List passwords we support
    if args.list:
        print('we support these passwords now : ' + ', '.join(PASSWORD_SUPPORTED))
        sys.exit(0)

    # Check password type whether we support
    if args.type not in PASSWORD_SUPPORTED + [None]:
        logger.warn('unsupported password type! cracker will determine that by itself')
        args.type = None

    # Check proxy format and then set
    if args.proxy:
        if checkProxyFormat(args.proxy) == False:
            print('error proxy format, use -h for more information')
            sys.exit(1)
        else:
            logger.debug('use proxy {!s}'.format(args.proxy))
            updateProxy(args.proxy)

    # If there is no any password source then exit
    if not any((args.password, args.file)):
        parser.print_usage()
        print('\ncracker: error: please given -f or one password at least')
        sys.exit(0)

    return args
