#!/usr/bin/env python

import argparse
import sys
import datetime
from enum import Enum
from Models import Task, Work


def count_true(*args):
    count = 0
    for arg in args:
        if arg:
            count += 1
            if count > 1:
                return False
    return True

def raw_string_to_time(input):


class Parser(object):

    def __init__(self, taskManager, workManager):
        parser = argparse.ArgumentParser(
            description='Pretends to be git',
            usage='''git <command> [<args>]

The most commonly used git commands are:
   commit     Record changes to the repository
   fetch      Download objects and refs from another repository
''')
        parser.add_argument('command', help='Subcommand to run')
        # parse_args defaults to [1:] for args, but you need to
        # exclude the rest of the args too, or validation will fail
        args = parser.parse_args(sys.argv[1:2])
        if not hasattr(self, args.command):
            print 'Unrecognized command'
            parser.print_help()
            exit(1)
        # use dispatch pattern to invoke method with same name
        getattr(self, args.command)()

    def work(self):
        print 'parser --> work'
        parser = argparse.ArgumentParser(
            description='add or remove a work!')
        # prefixing the argument with -- means it's optional
        parser.add_argument('-n', '--name')
        parser.add_argument('-d', '--desc')
        parser.add_argument('-a', '--add', action='store_true')
        parser.add_argument('-r', '--remove', action='store_true')
        parser.add_argument('-s', '--show', action='store_true')
        # now that we're inside a subcommand, ignore the first
        # TWO argvs, ie the command (git) and the subcommand (commit)
        args = parser.parse_args(sys.argv[2:])
        name = args.name
        desc = args.desc
        is_add = args.add
        is_remove = args.remove
        is_show = args.show
        print 'isAdd: %s  isRemove:%s isShow:%s name:%s desc:%s' % (is_add, is_remove, is_show, name, desc)
        count = count_true(is_add, is_remove, is_show)
        if count != 1:
            print 'wrong command'
            exit(1)
        if is_add:
            workManager(name=name, desc=desc, workCommand=WorkCommand.add)
        if is_remove:
            workManager(name=name, desc=desc, workCommand=WorkCommand.remove)
        if is_show:
            workManager(name=name, desc=desc, workCommand=WorkCommand.show)

        print 'work command work name: %s isAdd:%s  isRemove:%s' % (args.name, args.add, args.remove)

    def task(self):
        print 'parser --> task'
        parser = argparse.ArgumentParser(
            description='start or end a task')
        # NOT prefixing the argument with -- means it's not optional
        parser.add_argument('command')
        parser.add_argument('-e','--end')
        parser.add_argument('-s','--start')
        parser.add_argument('-w', '--work')
        parser.add_argument('-d', '--desc')
        parser.add_argument('-st', '--state')
        args = parser.parse_args(sys.argv[2:])
        work = args.work
        command = args.command
        start_time = args.start
        end_time = args.end
        desc = args.desc
        state = args.state
        if command == 'start':
            taskManager(work, TaskCommand.start)
        if command == 'stop':
            taskManager(work, TaskCommand.stop)
        if command == 'show':
            taskManager(work, TaskCommand.show)
        if command == 'add':
            taskManager(TaskCommand.add, work, start_time=start_time, end_time=end_time, desc=desc, task_state=state)

        print 'task command : =%s and work : %s ' % (args.command, args.work)




class TaskCommand(Enum):
    start = 0
    stop = 1
    show = 2
    add = 3


def taskManager(taskCommand, work_name, desc, start_time, end_time, task_state):
    work = Work.get_work(name=work_name)
    print 'taskManager()'
    if taskCommand == TaskCommand.start:
         isExist = Task.is_exist(work)
         if isExist:
             print 'a task on this work exist. stop it or choose another work'
         else:
             try:
                Task.add_task(work)
                print 'task start success'
             except Exception as e:
                'a problem in adding task try again'
    if taskCommand == TaskCommand.stop:
        task = Task.get_doing_task(work)
        if task is None:
            print 'not exist any task to stop'
        else:
            task.make_task_done(task)

    if taskCommand == TaskCommand.show:
        print 'show command:D'

    if taskCommand == TaskCommand.add:
        print('add task request : desc:%s  work:%s  startTime:%s  endTime:%s  taskState:%s' %
              (desc, work, start_time, end_time, task_state))
        start_time = raw_string_to_time(start_time)
        end_time = raw_string_to_time(end_time)
        Task.add_task(work, task_state, start_time, end_time)

class WorkCommand(Enum):
    add = 0
    remove = 1
    edit = 2
    show = 3

def workManager(id=0, name=None, desc=None, workCommand=None):
    print 'workManager()'
    if workCommand == WorkCommand.add:
        Work.add_work(name=name, desc=desc)
    if workCommand == WorkCommand.show:
        works = Work.get_works()
        for work in works:
            print str(work) + '\n'






if __name__ == '__main__':
    Parser(taskManager, workManager)