from devilrycliscriptslib.argparsewrapper import ArgumentParser
from devilrycliscriptslib.login import login_using_args
from devilrycliscriptslib.commonargs import add_common_args
from devilrycliscriptslib.queries import find_groups_in_assignment
from devilrycliscriptslib.queries import find_assignment_id_by_shortnames
from devilrycliscriptslib.utils import split_path

argparser = ArgumentParser(description='List groups on an assignment as json encoded data.')
add_common_args(argparser)
argparser.add_argument('--assignment', required=True,
                       help='Path to assignment. E.g: "duck1010.spring2010.assignment1"')
args = argparser.parse_args()

logincookie = login_using_args(args, getpass())
restful_factory = RestfulFactory(args.url)
AssignmentGroupApi = restful_factory.make('/administrator/restfulsimplifiedassignmentgroup/')
AssignmentApi = restful_factory.make('/administrator/restfulsimplifiedassignment/')
DeadlineApi = restful_factory.make('/administrator/restfulsimplifieddeadline/')


try:
    path = split_path(args.assignment, '--assignment', 3)
    source_assignment_id = find_assignment_id_by_shortnames(AssignmentApi, logincookie,
                                                            *path)
except LookupError:
    raise SystemExit('Assignment {0} not found.'.format(args.assignment))

groups = find_groups_in_assignment(AssignmentGroupApi, logincookie, source_assignment_id,
                                   result_fieldgroups=['feedback', 'users', 'tags'])
print json.dumps(groups, indent=2)
AssignmentApi = restful_factory.make('/{}/restfulsimplifiedassignment/'.format(role))
AssignmentGroupApi = restful_factory.make('/{}/restfulsimplifiedassignmentgroup/'.format(role))
DeadlineApi = restful_factory.make('/{}/restfulsimplifieddeadline/'.format(role))
DeliveryApi = restful_factory.make('/{}/restfulsimplifieddelivery/'.format(role))
FileMetaApi = restful_factory.make('/{}/restfulsimplifiedfilemeta/'.format(role))
FileMetaDownloadApi = restful_factory.make('/student/show-delivery/filedownload/')


try:
    path = split_path(args.assignment, '--assignment', 3)
    source_assignment_id = find_assignment_id_by_shortnames(AssignmentApi, logincookie, *path)
except LookupError:
    raise SystemExit('Assignment {0} not found.'.format(args.assignment))

groups = find_groups_in_assignment(AssignmentGroupApi,
        logincookie, source_assignment_id,
        result_fieldgroups=['assignment', 'period', 'subject', 'users'],
        limit=10000)

if not os.path.exists(outdir):
    raise SystemExit('{} does not exist. Please create it before running this script.'.format(outdir))

for groupnumber, group in enumerate(groups, start=1):
    assignmentpath = '{parentnode__parentnode__parentnode__short_name}.{parentnode__parentnode__short_name}.{parentnode__short_name}'.format(**group)
    if role == 'examiner':
        usernamelist = group['candidates__identifier']
    else:
        usernamelist = group['candidates__student__username']
    usernames = '__'.join(usernamelist)
    assignmentdir = os.path.join(outdir, assignmentpath, usernames)
    print 'Downloading all deliveries for {} (group {}/{})'.format(usernames, groupnumber, len(groups))