#!/usr/bin/python
#
# TaskPaper Parser
# K. Marchand 2014
#

from datetime import datetime, timedelta
from collections import namedtuple
from dateutil import parser
import sys
import re

tpfile = sys.argv[1]

with open(tpfile, encoding='utf-8') as f:
    tplines = f.readlines()

Flagged = namedtuple('Flagged', ['type', 'taskdate', 'project', 'task'])
flaglist = []
errlist = []
done_count = 0
na_count = 0
total_count = 0

project = ''

for line in tplines:
    try:
        if ':\n' in line:
            project = line.strip()[:-1]
        elif '@done' in line:
            done_count = done_count + 1
        elif '@due' in line:
            na_count = na_count + 1
            duetag = re.search(r'\@due\((.*?)\)', line).group(1)
            taskdate = datetime.date(parser.parse(duetag))
            flaglist.append(
                Flagged('due', taskdate, project, line.strip()[2:]))
        elif '@start' in line:
            na_count = na_count + 1
            starttag = re.search(r'\@start\((.*?)\)', line).group(1)
            taskdate = datetime.date(parser.parse(starttag))
            flaglist.append(
                Flagged('start', taskdate, project, line.strip()[2:]))
        elif '@today' in line:
            na_count = na_count + 1
            flaglist.append(
                Flagged('today', datetime.now(), project, line.strip()[2:]))
        elif '☐' in line:
            na_count = na_count + 1
    except Exception as e:
        errlist.append((line, e))

today = overdue = duethisweek = startthisweek = None
today_date = datetime.date(datetime.now())

print('SUMMARY for {0} [{1}]'.format(tpfile, str(datetime.now())[:16]))

total_count = na_count + done_count

print('\n{0} of {1} tasks done, {2} tasks open'.format(done_count, total_count, na_count))

print('\nTODAY\n')

for task in flaglist:
    if task.type == 'today':
        today = True
        print('\t[%s] %s' % (task.project, task.task))
    elif task.type == 'due' and today_date == task.taskdate:
        today = True
        print('\t[%s] %s' % (task.project, task.task))
if not today:
    print('\t (none)')

print('\nOVERDUE\n')

for task in flaglist:
    if task.type == 'due' and today_date > task.taskdate:
        overdue = True
        print('\t[%s] %s' % (task.project, task.task))
if not overdue:
    print('\t (none)')

print('\nDUE THIS WEEK\n')

for task in flaglist:
    weeklater = today_date + timedelta(days=7)
    if task.type == 'due' and today_date < task.taskdate < weeklater:
        duethisweek = True
        print('\t[%s] %s' % (task.project, task.task))
if not duethisweek:
    print('\t (none)')

print('\nSTARTING THIS WEEK\n')

for task in flaglist:
    weeklater = today_date + timedelta(days=7)
    if task.type == 'start' and today_date < task.taskdate < weeklater:
        startthisweek = True
        print('\t[%s] %s' % (task.project, task.task))
if not startthisweek:
    print('\t (none)')

if len(errlist) != 0:
    print('\nERROR PARSING THESE LINES\n')
    for errtask in errlist:
        print('\t%s' % str(errtask))

print('\n')
