def help_task(chat_id):
    """
    This method will show the help list.
    :param chat_id: specify the current chat.
    """
    send_message("Here is a list of things you can do.", chat_id)
    send_message(__HELP, chat_id)
def duedate_task(msg, chat):
    text = ''
    if msg != '':
        if len(msg.split(' ', 1)) > 1:
            text = msg.split(' ', 1)[1]
            msg = msg.split(' ', 1)[0]

            if not msg.isdigit():
                send_message("You must inform the task id", chat)
            else:
                task_id = int(msg)
                query = db.session.query(Task).filter_by(id=task_id, chat=chat)
                try:
                    task = query.one()
                except sqlalchemy.orm.exc.NoResultFound:
                    send_message("_404_ Task {} not found x.x".format(task_id), chat)
                    return

                if text == '':
                    send_message("You did not provide a date for the task:{} ".format(task_id), chat)
                    return
                else:
                    if not __verify_date_mask(text):
                        send_message("duedate does not meet expected standard: dd/mm/YYYY", chat)
                    else:
                        task.duedate = datetime.strptime(text, '%d/%m/%Y')
                        send_message("Task {} has the duedate: {}".format(task_id, text), chat)
                db.session.commit()
def start_task(chat_id):
    """
    This method will start the bot, show a Welcome Message and a list of possible commands.
    :param chat_id: specify the current chat.
    """
    send_message("Welcome! Here is a list of things you can do.", chat_id)
    send_message(__HELP, chat_id)
def create_issue(chat_id, msg):
    task_id = int(msg)
    task = db.search_Task(task_id, chat_id)
    #body = "Duedate: "+task.duedate
    if make_github_issue(task.name, [task.status, 'create_by_bot']):
        send_message("Issue created.", chat_id)
    else:
        send_message("Bad attempt.", chat_id)
def new_task(chat_id, name):
    """
    This method will create a new task.
    :param chat_id: specify the current chat.
    :param name: the name of the new task.
    """
    task = Task(chat=chat_id, name=name, status='TODO', dependencies='', parents='', priority='', duedate = None)
    if(name != ''):
        db.session.add(task)
        db.session.commit()
        send_message("New task *TODO* [[{}]] {}".format(task.id, task.name), chat_id)
def priority_task(chat_id, msg):
    """
    This method will set the task priority.
    :param chat_id: Specify the current chat.
    :param msg: Contains a task id and their priority.
    """
    text = ''
    if msg != '':
        if len(msg.split(' ', 1)) > 1:
            text = msg.split(' ', 1)[1]
        msg = msg.split(' ', 1)[0]

    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)

        if text == '':
            task.priority = ''
            send_message("_Cleared_ all priorities from task {}".format(task_id), chat_id)
        else:
            if text.lower() not in ['high', 'medium', 'low']:
                send_message("The priority *must be* one of the following: high, medium, low", chat_id)
            else:
                task.priority = text.lower()
                send_message("*Task {}* priority has priority *{}*".format(task_id, text.lower()), chat_id)
        db.session.commit()
def done_task(chat_id, msg):
    """
    This method will show the done task.
    :param chat_id: Specify the current chat.
    :param msg: Contains a task id.
    """
    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)
        task.status = 'DONE'
        db.session.commit()
        send_message("*DONE* task [[{}]] {}".format(task.id, task.name), chat_id)
def todo_task(chat_id, msg):
    """
    This method will show the todo list.
    :param chat_id: Specify the current chat.
    :param msg: Contains a task id.
    """
    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)
        task.status = 'TODO'
        db.session.commit()
        send_message("*TODO* task [[{}]] {} with duedate [[{}]]".format(task.id, task.name), chat_id, task.duedate)
def delete_task(chat_id, msg):
    """
    This method will delete a specific task.
    :param chat_id: specify the current chat.
    :param msg: Contains an id that specify a specific task.
    """
    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)
        for t in task.dependencies.split(',')[:-1]:
            t = db.search_Task(int(t), chat_id)
            t.parents = t.parents.replace('{},'.format(task.id), '')
        db.session.delete(task)
        db.session.commit()
        send_message("Task [[{}]] deleted".format(task_id), chat_id)
def rename_task(chat_id, msg):
    """
    This method will rename a specific task.
    :param chat_id: specify the current chat.
    :param msg: get the params passed after /rename.
    """
    # This variable will contain the new name of the task.
    text = ''
    if msg != '':
        if len(msg.split(' ', 1)) > 1:
            text = msg.split(' ', 1)[1]
        # Getting the id.
        msg = msg.split(' ', 1)[0]

    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)
        # If message doesn't have a new name.
        if text == '':
            send_message("You want to modify task {}, but you didn't provide any new text".format(task_id), chat_id)
            return

        old_text = task.name
        task.name = text
        db.session.commit()
        send_message("Task {} redefined from {} to {}".format(task_id, old_text, text), chat_id)
def duplicate_task(chat_id, msg):
    """
    This method will duplicate a specific task.
    :param chat_id: specify the current chat.
    :param msg: contains an id that specifies a specific task.
    """
    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)

        dtask = Task(chat=task.chat, name=task.name, status=task.status, dependencies=task.dependencies,
                     parents=task.parents, priority=task.priority, duedate=task.duedate)
        db.session.add(dtask)

        for t in task.dependencies.split(',')[:-1]:
            t = db.search_Task(int(t), chat_id)
            t.parents += '{},'.format(dtask.id)

        db.session.commit()
        send_message("New task *TODO* [[{}]] {} with duedate [[{}]]".format(dtask.id, dtask.name), chat_id, task.duedate)
def dependson_task(chat_id, msg):
    text = ''
    if msg != '':
        if len(msg.split(' ', 1)) > 1:
            text = msg.split(' ', 1)[1]
        msg = msg.split(' ', 1)[0]

    if not msg.isdigit():
        send_message("You must inform the task id", chat_id)
    else:
        task_id = int(msg)
        task = db.search_Task(task_id, chat_id)

        if text == '':
            for i in task.dependencies.split(',')[:-1]:
                i = int(i)
                t = db.search_Task(i, chat_id)
                t.parents = t.parents.replace('{},'.format(task.id), '')

            task.dependencies = ''
            send_message("Dependencies removed from task {}".format(task_id), chat_id)
        else:
            for depid in text.split(' '):
                if not depid.isdigit():
                    send_message("All dependencies ids must be numeric, and not {}".format(depid), chat_id)
                else:
                    if __verify_circular_dependency_in_list(msg, text, chat_id):
                        send_message('Can not has circular dependency', chat_id)
                    else:
                        depid = int(depid)
                        taskdep = db.search_Task(task_id, chat_id)
                        taskdep.parents += str(task.id) + ','

                        deplist = task.dependencies.split(',')
                        if str(depid) not in deplist:
                            task.dependencies += str(depid) + ','

        db.session.commit()
        send_message("Task {} dependencies up to date".format(task_id), chat_id)
def list_task(chat_id):
    """
    This method will list a list task.
    :param chat_id: Specify the current chat.
    """
    a = ''

    a += '\U0001F4CB Task List\n'
    query = db.session.query(Task).filter_by(parents='', chat=chat_id).order_by(Task.id)
    for task in query.all():
        icon = '\U0001F195'
        if task.status == 'DOING':
            icon = '\U000023FA'
        elif task.status == 'DONE':
            icon = '\U00002611'

        a += __priority_message(task)
        a += __deps_text(task, chat_id)

    send_message(a, chat_id)
    a = ''

    a += '\U0001F4DD _Status_\n'
    query = db.session.query(Task).filter_by(status='TODO', chat=chat_id).order_by(Task.id)
    a += '\n\U0001F195 *TODO*\n'
    for task in query.all():
        a += __priority_message(task)
    query = db.session.query(Task).filter_by(status='DOING', chat=chat_id).order_by(Task.id)
    a += '\n\U000023FA *DOING*\n'
    for task in query.all():
        a += __priority_message(task)
    query = db.session.query(Task).filter_by(status='DONE', chat=chat_id).order_by(Task.id)
    a += '\n\U00002611 *DONE*\n'
    for task in query.all():
        a += __priority_message(task)

    send_message(a, chat_id)