#!/usr/bin/env python

from argparse import ArgumentParser, RawDescriptionHelpFormatter
from getpass import getuser
from os import path
from textwrap import dedent

from whatidid import commands

class Main(object):
    ''' Main class for whatidid (wid).

    A plain text way of remembering what you did
    '''

    def __init__(self, **kwargs):
        self.command = kwargs.get('command', 'notfound');
        self.message = kwargs.get('message', None)
        self.week = kwargs.get('week', None)
        tags = kwargs.get('tags', '')
        if tags:
            self.tags = tags.split(',')
        else:
            self.tags = []

        if not self.command == 'init':
            if not path.exists('%s/.widrc' % (path.expanduser('~'))):
                print u'wid is not setup properly, you should run: wid init'
                exit(1)
        getattr(self, 'command_' + self.command.replace('-','_'))()

    def command_init(self):
        command = commands.InitCommand()
        command.run()

    def command_update(self):
        command = commands.UpdateCommand(message=self.message, tags=self.tags)
        command.run()

    def command_update_show(self):
        command = commands.UpdateShowCommand(week=self.week)
        command.run()

    def command_todo(self):
        command = commands.TodoCommand(message=self.message, tags=self.tags)
        command.run()

    def command_todo_show(self):
        command = commands.TodoShowCommand(tags=self.tags)
        command.run()

    def command_notfound(self):
        print u'The command was not found'
        exit(1);


if __name__ == '__main__':
    description = dedent('''\
        A minimalist command line app for life logging.

        Commands:

         updates

          wid update\t\tAdd an update to your updates. -m is required, -t is optional
          wid update-show\tLists all of your current updates. -w is optional

         todos

          wid todo\t\tAdd an todo to your todos. -m is required, -t is optional.
          wid todo-show\t\tLists all of your todos.

    ''')
    parser = ArgumentParser(formatter_class=RawDescriptionHelpFormatter, description=description)
    parser.add_argument('command', metavar='COMMAND', type=str,
        help=u'A command, ie: update, update-show, update-mail')
    parser.add_argument('-m', '--message', type=str, metavar='"A message"', help=u'A message')
    parser.add_argument('-t', '--tags', type=str, metavar='tag1[,tag2,tag3]',
        help=u'A tag or comma separated list of tags')
    parser.add_argument('-w', '--week', type=str, metavar='NN', help=u'A week number to display all events.')
    args = parser.parse_args()
    main = Main(command=args.command,
                message=args.message,
                tags=args.tags,
                week=args.week)

