#!/usr/bin/env python
"""
CLI utility for logging the time you spend on things.
    Usage: `logtime --help`
"""

from datetime import date, datetime, timedelta
import os
import re
import click
import logtime_cli.logfile_data as logfile_data
import logtime_cli.logfile_io as logfile_io
import logtime_cli.logtime_config as logtime_config


@click.group(context_settings=dict(help_option_names=['-h', '--help']))
def entry_point():
    """logtime"""
    pass


@entry_point.command()
@click.option('--previous', '-p', type=int, help='Use the logfile X days in the past.')
def calc(previous):
    """Print calculated time for the day.

    Records beginning in `*` are considered "non-work hours".
    """
    if previous:
        logdate = (date.today() - timedelta(days=previous))
    else:
        logdate = date.today()

    if not logfile_io.exists(logdate):
        print "No logfile exists for " + str(logdate) + "."
        return

    log_data = logfile_io.load_logfile(logdate)

    all_hours = sum([x.duration() for x in log_data.entries])
    work_hours = sum([x.duration() for x in log_data.entries if x.task[:1] == '*'])
    non_work_hours = sum([x.duration() for x in log_data.entries if x.task[:1] != '*'])

    print logdate
    print "All Hours: " + str(all_hours)
    print "Non-Work Hours: " + str(work_hours)
    print "Work Hours: " + str(non_work_hours)


@entry_point.command()
@click.option('--previous', '-p', type=int, help='Use the logfile X days in the past.')
def count_tasks(previous):
    """Print number of tasks logged for the given day."""
    if previous:
        logdate = (date.today() - timedelta(days=previous))
    else:
        logdate = date.today()

    if not logfile_io.exists(logdate):
        print "No logfile exists for " + str(logdate) + "."
        return

    log_data = logfile_io.load_logfile(logdate)

    print len(log_data.entries)


@entry_point.command()
def list(): #pylint: disable=redefined-builtin
    """List existing logfiles, along with how many days back it is (for use with `-p`)"""
    log_file_directory = logtime_config.get_option('DEFAULT', 'logfile_directory')
    iso_date_regex = r'[0-9]{4}-[0-9]{2}-[0-9]{2}\.md'

    file_names = [f for f in os.listdir(log_file_directory)
                  if re.match(iso_date_regex, f)
                  and not os.path.isdir(os.path.join(log_file_directory, f))]

    for name in file_names:
        file_date = datetime.strptime(name, '%Y-%m-%d.md')

        delta = datetime.today() - file_date

        print '{date} - {days}'.format(days=delta.days, date=name)

@entry_point.command()
@click.option('--previous', '-p', type=int, help='Use the week X weeks in the past; Monday begins the week.')
def summarize_week(previous):
    """Print Summary section for every day of the chosen week."""
    if previous:
        logdate = (date.today() - timedelta(weeks=previous))
    else:
        logdate = date.today()

    first_day_delta = 0 - logdate.weekday() # 0 is Monday
    last_day_delta = 7 - logdate.weekday() # 7 is the number of weekdays
    week_range = range(first_day_delta, last_day_delta)

    # Center the list on this week; start on Monday
    weekdays = [(logdate + timedelta(days=x)) for x in week_range]

    for day in weekdays:
        if logfile_io.exists(day):
            log_data = logfile_io.load_logfile(day)
            notes = logfile_data.get_notes_data(log_data)
            notes_sections = notes.get_subsections()

            summaries = [x.full_text for x in notes_sections if x.name == 'Summary']
            if len(summaries) > 1:
                raise NotImplementedError('Found more than one Summary for {}. Combine them.'.format(day))
            elif len(summaries) == 1:
                print '## {}'.format(day)
                print summaries[0]
                print ''

if __name__ == '__main__':
    entry_point() #pylint: disable=no-value-for-parameter
