#!/usr/bin/env python3
from getpass import getpass
import os
import subprocess
import sys

import click
import colorful

from passwords import PasswordRepository


@click.group()
def main():
    pass


@main.command(help='Create a new password repository')
def init():
    try:
        print()
        PasswordRepository.new(os.getcwd())
        msg = 'Created password repository at .passwords'
        print('\n', colorful.bold_green('OK'), msg, '\n')
    except Exception as exc:
        print('\n', colorful.bold_red('ERROR'), exc, '\n', file=sys.stderr)


@main.command(help='Save a password to a path')
@click.argument('path')
@click.option('--debug', default=False, is_flag=True)
def save(path, debug):
    try:
        plaintext = getpass('enter secret: ')
        repo = PasswordRepository.load(os.getcwd())
        password = repo.save(path, plaintext)
        if debug:
            print(file=sys.stderr)
            print(colorful.bold_cyan('ENCRYPTED VALUE'), file=sys.stderr)
            print(password.value, file=sys.stderr)
        msg = 'Saved password to path {}'.format(path)
        print('\n', colorful.bold_green('OK '), msg, '\n')
    except Exception as exc:
        print('\n', colorful.bold_red('ERROR '), exc, '\n', file=sys.stderr)


@main.command(help='Lookup passwords by path')
@click.argument('path', required=False, default='')
def show(path):
    try:
        repo = PasswordRepository.load(os.getcwd())
        output = repo.show(path)
        print(file=sys.stderr)
        print(output)
        print(file=sys.stderr)

    except Exception as exc:
        print('\n', colorful.bold_red('ERROR '), exc, '\n', file=sys.stderr)


@main.command(help='List encrypted passwords by path')
@click.argument('path', required=False, default='')
def list(path):
    try:
        repo = PasswordRepository.load(os.getcwd())
        output = repo.list(path)
        print(file=sys.stderr)
        print(output)
        print(file=sys.stderr)

    except Exception as exc:
        print('\n', colorful.bold_red('ERROR '), exc, '\n', file=sys.stderr)


@main.command(help='Drop password at path')
@click.argument('path')
def drop(path):
    try:
        repo = PasswordRepository.load(os.getcwd())
        repo.drop(path)
        msg = 'Deleted password at {}'.format(path)
        print(file=sys.stderr)
        print('\n', colorful.bold_green('OK '), msg, '\n')
        print(file=sys.stderr)

    except Exception as exc:
        print('\n', colorful.bold_red('ERROR '), exc, '\n', file=sys.stderr)


@main.command(help='Copy value at path to clipboard')
@click.argument('path')
def clipboard(path):
    try:
        repo = PasswordRepository.load(os.getcwd())
        value = repo.get(path)
        p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
        p.communicate(value.encode('utf8'))
        msg = 'Copied {} to clipboard'.format(path)
        print(file=sys.stderr)
        print('\n', colorful.bold_green('OK '), msg, '\n')
        print(file=sys.stderr)

    except Exception as exc:
        print('\n', colorful.bold_red('ERROR '), exc, '\n', file=sys.stderr)



if __name__ == '__main__':
    main()
