#!/usr/bin/env python
'''github-pr-stats

Usage:
   github-pr-stats [options] <user> <repo>
   github-pr-stats --version
   github-pr-stats (-h | --help)

Options:
   -h --help           Show this screen.
      --version        Print the program's installed version.
      --basic          Basic statistics about merged/closed rate.
      --days-open      Analyze number of days opened.
      --comments       Analyze number of comments created.
      --day-created    Analyze day of the week opened.
      --day-closed     Analyze day of the week closed.
      --hour-created   Analyze hour of the day opened.
      --hour-closed    Analyze hour of the day closed.
      --week-created   Analyze week opened.
      --week-closed    Analyze week closed.
      --user-creating  Analyze user who opened.
      --user-closing   Analyze user who closed.
'''

# May you recognize your weaknesses and share your strengths.
# May you share freely, never taking more than you give.
# May you find love and love everyone you find.

import signal
import sys
from collections import defaultdict

from docopt import docopt
from envoy import run
from github3 import authorize

from github_pr_stats import VERSION
from github_pr_stats.github_pr_stats import analyze

# Stack traces are ugly; why would we want to print one on ctrl-c?
def nice_sigint(signal, frame):
   print("")
   sys.exit(0)
signal.signal(signal.SIGINT, nice_sigint)

arguments = docopt(__doc__, version='github-pr-stats %s' % VERSION)

# Use a stored OAuth token so we don't have to ask for the user's password
# every time (or save the password on disk!).
token = run('git config --global github-pr-stats.token').std_out.strip()
if not token:
   from getpass import getpass
   user = password = ''

   while not user:
      user = raw_input('Username: ')
   while not password:
      password = getpass('Password: ')

   auth = authorize(user, password,
                    scopes=['repo'],
                    note='github-pr-stats',
                    note_url='https://github.com/xiongchiamiov/github-pr-stats')
   token = auth.token
   # We need to un-unicode token for now.
   # https://github.com/kennethreitz/envoy/issues/34
   run("git config --global github-pr-stats.token '%s'" % str(token))

config = defaultdict(bool)
if arguments['--basic']:
   config['basicStats'] = True
if arguments['--days-open']:
   config['daysOpen'] = True
if arguments['--comments']:
   config['comments'] = True
if arguments['--day-created']:
   config['dayOfWeekCreated'] = True
if arguments['--day-closed']:
   config['dayOfWeekClosed'] = True
if arguments['--hour-created']:
   config['hourOfDayCreated'] = True
if arguments['--hour-closed']:
   config['hourOfDayClosed'] = True
if arguments['--week-created']:
   config['weekCreated'] = True
if arguments['--week-closed']:
   config['weekClosed'] = True
if arguments['--user-creating']:
   config['userCreating'] = True
if arguments['--user-closing']:
   config['userClosing'] = True
# Did we not get *any* data-specifying options?  Default to everything.
if not config:
   config = defaultdict(lambda: True)

analyze(arguments['<user>'], arguments['<repo>'], token, config)

