#!/usr/bin/env python

import sys
import requests
from requests.auth import HTTPBasicAuth
import subprocess
import datetime
import decimal
from bs4 import BeautifulSoup
import argparse
from prettytable import PrettyTable 
from prettytable import * 
from basecamp import Basecamp


def main():

  parser = argparse.ArgumentParser()

  parser.add_argument('time', choices=['today', 'week', 'yesterday'], default='today', nargs='?')

  args = parser.parse_args()
  if args.time == 'today':
    listToday()

  elif args.time == 'yesterday':
    listYesterday()

  elif args.time == 'week':
    listWeek()
  

def listYesterday():
  today = datetime.date.today()
  yesterday = today - datetime.timedelta(1)

  breakdown = basecamp.hoursByDay(yesterday)
  print "Hour breakdown for yesterday"
  printBreakdown(breakdown)

def listToday():
  #total = basecamp.hoursToday()
  #print str(total) + " hours logged in basecamp today"
  breakdown = basecamp.hoursToday()
  print "Hour breakdown for today"
  printBreakdown(breakdown)



def listWeek():
  breakdown = basecamp.hoursWeek()
  print "Hour breakdown for the week"
  printBreakdown(breakdown)

def printBreakdown(breakdown):
  total = 0
  table = PrettyTable(["-", "-", "-"],hrules=FRAME)
  table.align = "l"
  table.set_style(PLAIN_COLUMNS)
  table.add_row(["Client", "Project", "Hours"])
  table.add_row(["-","-","-"])
  for key, value in breakdown.iteritems():
    info = basecamp.getProjectInfo(key)
    table.add_row([info['client'], info['name'], value])
    total += value

  table.add_row(["-","-","-"])
  table.add_row(["","Total",total])
  print table



basecamp = Basecamp()



if __name__ == '__main__':
  main()
