'''
Example to connect and download all issues on the remote server to a local cache nosql
database using UnQLite
'''

from unqlite import UnQLite
from pprint import pprint

import pythreesixty.core.mobile as mobile

db = UnQLite()  # Creates an in-memory database.
issues = db.collection('issues')
issues.create()  # Create the collection if it does not exist.

projects = mobile.projects()
print("User has access to {count} projects".format(count=len(projects.json())))

for p in projects.json():
    print("\n{id}: {name}".format(
        id=p['project_id'],
        name=p['name'])
    )

    # Get issues
    issues_request = mobile.get_issues(p['project_id'], count_only=True)  # use count_only to just return the number
    if issues_request.status_code == 200:
        print("  - {count} issues found.".format(count=issues_request.json()['count']))

    issues.store(mobile.get_issues(p['project_id']).json())
    print("Local cache contains {0} items.".format(db.__len__()))
companies_request = companies.companies()
print("Discovered {count} companies in masterdata".format(count=len(companies_request.json())))

for c in companies_request.json():
    print("{id}: {name}".format(
        id=c['id'],
        name=c['name'])
    )
    col_companies.insert(c)

print("Companies BSON collection contains {0} projects.".format(col_companies.count()))
#pprint(col_companies.find().sort('_id', DESCENDING).next())  # print the last one inserted

# Download all projects and associated issues
print("\n\n===Downloading projects and issues===")
projects_request = mobile.projects()
print("User has access to {count} projects".format(count=len(projects_request.json())))

for p in projects_request.json():
    print("\n{id}: {name}".format(
        id=p['project_id'],
        name=p['name'])
    )
    col_projects.insert(p)
    print("Projects BSON collection contains {0} projects.".format(col_projects.count()))
    # pprint(projects.find().sort('_id', DESCENDING).next())  # print the last one inserted

    # Get issues
    issues_request = mobile.get_issues(p['project_id'], count_only=True)  # use count_only to just return the number
    if issues_request.status_code == 200:
        print("  - {count} issues found.".format(count=issues_request.json()['count']))
print("\n\n===Downloading linked collections===")
companies_request = companies.companies()
print("Discovered {count} companies in masterdata".format(
    count=len(companies_request.json())))

for c in companies_request.json():
    print("{id}: {name}".format(id=c['id'], name=c['name']))
    col_companies.insert(c)

print("Companies BSON collection contains {0} projects.".format(
    col_companies.count()))
#pprint(col_companies.find().sort('_id', DESCENDING).next())  # print the last one inserted

# Download all projects and associated issues
print("\n\n===Downloading projects and issues===")
projects_request = mobile.projects()
print("User has access to {count} projects".format(
    count=len(projects_request.json())))

for p in projects_request.json():
    print("\n{id}: {name}".format(id=p['project_id'], name=p['name']))
    col_projects.insert(p)
    print("Projects BSON collection contains {0} projects.".format(
        col_projects.count()))
    # pprint(projects.find().sort('_id', DESCENDING).next())  # print the last one inserted

    # Get issues
    issues_request = mobile.get_issues(
        p['project_id'],
        count_only=True)  # use count_only to just return the number
    if issues_request.status_code == 200: