#!/usr/bin/env python

import sys
from stamper import Stamper


def usage(name):
    msg = """
    Usage:

    To gather information of recorded times: %(name)s

    Recorded times for a given customer: %(name)s customer

    Detailed times for a given customer: %(name)s customer details
    """ % {'name': name}
    print(msg)


if __name__ == '__main__':

    s = Stamper()
    s.load_stamps()

    if len(sys.argv) == 1:
        totals = s.totals()
        print('Total times for all customers:')
        for t in totals:
            print(' -> %(customer)s: %(total)s' % {'customer': t,
                                                   'total': totals[t]})
    elif len(sys.argv) == 2:
        if sys.argv[1] in s.customers():
            print('Total time for %(customer)s: %(total)s' % {
                'customer': sys.argv[1],
                'total': s.total_by_customer(sys.argv[1])
                })

    elif len(sys.argv) == 3:
        if sys.argv[1] in s.customers():
            print('Detailed time for %(customer)s' % {
                'customer': sys.argv[1],
                })
            details, totals = s.details_by_customer(sys.argv[1])
            for day in details:
                print('------ %(day)s ------' % {'day': day})
                for line in details[day]:
                    print(line)
                print(' Total: %(total)s' % {'total': totals[day]})

    else:
        usage(sys.argv[0])
        sys.exit(1)
