#!/usr/bin/python3
"""
Starts a Toggl task with the given project, tags and name.

Usage:
    toggl_start_task [options] --name <name> [--tag <tag>]...

Options:
    --name, -n <name>       The name of the task.
    --project, -p <project> The name of the associated project.
    --tag, -t <tag> ...     An associated tag.
    --help, -h              Display this help text.

"""
from toggl_boilerplate import sys, requests, auth
from toggl_get_projects import get_workspace_id, get_projects
from docopt import docopt

def main(args):
    wid = get_workspace_id()
    projects = get_projects(wid)

    post_data = {'description': args['--name'], 'created_with': 'T4Py'}

    if args['--tag']:
        post_data['tags'] = args['--tag']

    if args['--project']:
        pid = [p for p in projects if p['name'] == args['--project']][0]['id']
        post_data['pid'] = pid


    r = requests.post("https://api.track.toggl.com/api/v8/time_entries/start",
                      json={'time_entry': post_data}, auth=auth)


    print(r.text)

if __name__ == '__main__':
    main(docopt(__doc__))
