# -*- coding: utf-8 -*-

import sys
from getpass import getpass
import argparse
import pkg_resources
import pyperclip

from .generator import *
from .utils import *
from .constants import *


def show_password_entropy(passwd, word_list):
    """Displays the password entropy calculation results."""
    entropy = calculate_entropy(passwd, dict_set=word_list)
    print("\nPassword length: %d characters" % len(passwd))
    print("\nEntropy")
    print("-------")
    for charset, charset_name in list(PASSWORD_CHARSET_NAMES):
        print(("{:<%d}" % LONGEST_CHARSET_NAME_LEN).format(charset_name) + " : " +
              (("%.6f" % entropy[charset]) if charset in entropy else "not in character set"))
    print("")


def main():
    """Main routine for handling command line functionality for passwdgen."""

    version = pkg_resources.require("passwdgen")[0].version
    parser = argparse.ArgumentParser(description="A password generation utility (v%s)." % version)
    subparsers = parser.add_subparsers(help="The command to execute.", dest="command")

    parser_info = subparsers.add_parser(
        "info",
        help=(
            "Compute information about a password. If passwdgen has input piped into it via stdin, that " +
            "will be interpreted as the password."
        )
    )
    parser_info.add_argument(
        "-d", "--dictionary",
        default=None,
        help="Path to the dictionary file to use. This must be a plain text file with one word per line."
    )
    parser_info.add_argument(
        "-e", "--encoding",
        default=None,
        help=(
            "The encoding to use when read/writing input/output files. " +
            "(See https://docs.python.org/2/library/codecs.html#standard-encodings)"
        )
    )

    parser_generate = subparsers.add_parser(
        "generate",
        help="Generate password(s)."
    )
    parser_generate.add_argument(
        "-c", "--clipboard",