#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from sys import stdout, stderr
from getpass import getpass

from temboardagent.usermgmt import hash_password
from temboardagent.options import CLIOptions
from temboardagent.errors import ConfigurationError, CLIError, HTTPError
from temboardagent.usermgmt import get_user
from temboardagent.logger import get_logger
from temboardagent.configuration import Configuration
from temboardagent.types import T_PASSWORD, T_USERNAME
from temboardagent.tools import validate_parameters

def ask_password():
    raw_pass1 = getpass("Password: ")
    raw_pass2 = getpass("Retype password: ")
    if raw_pass1 != raw_pass2:
        stdout.write("Sorry, passwords do not match.\n")
        return ask_password()
    try:
        password = raw_pass1
        validate_parameters({'password': password},
            [('password', T_PASSWORD, False)])
    except HTTPError as e:
        stdout.write("Invalid password.\n")
        return ask_password()
    return password

def ask_username(config):
    stdout.write("Username: ".encode('utf-8'))
    raw_username = raw_input()
    try:
        get_user(config.temboard['users'], raw_username)
    except HTTPError as e:
        pass
    except ConfigurationError as e:
        pass
    else:
        stdout.write("User already exists.\n")
        return ask_username(config)
    try:
        username = raw_username
        validate_parameters({'username': username},
            [('username', T_USERNAME, False)])
    except HTTPError as e:
        stdout.write("Invalid username.\n")
        return ask_username(config)
    return username

def main():
    """
    Main function.
    """
    # Instanciate a new CLI options parser.
    optparser = CLIOptions(description = "Add a new temboard-agent user.")
    (options, _) = optparser.parse_args()

    # Load configuration from the configuration file.
    try:
        config =  Configuration(options.configfile)
        logger = get_logger(config)
        username = ask_username(config)
        password = ask_password()
        
        with open(config.temboard['users'], 'a') as fd:
            fd.write("%s:%s\n" % (username, hash_password(username, password).decode('utf-8')))
            stdout.write("Done.\n")
    except (ConfigurationError, ImportError, IOError) as e:
        stderr.write("FATAL: %s\n" % str(e))
        exit(1)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt as e:
        stdout.write("\nExit..\n")
