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)
    sys.stdout.write(linesep)

    # Print the documented number for each user on each assignment
    for student, student_assignments in students.iteritems():
        sys.stdout.write(student)

        for assignment_shortname in all_assignments:
            number = 0
            if assignment_shortname in student_assignments:
                group = student_assignments[assignment_shortname]
                deliveries = group['number_of_deliveries']
                if deliveries > 0:
                    if group['feedback__is_passing_grade']:
                        number = 2
                    else:
                        number = 1
            else:
                pass # Student no registered on assignment - keep number at 0
            sys.stdout.write(',{0}'.format(number))
        sys.stdout.write(linesep)


subject, period = split_path(args.period, '--period', 2)
try:
    period_id = find_period_id_by_shortnames(PeriodApi, logincookie, subject, period)
except LookupError, e:
    raise SystemExit('ERROR: Could not find requested period: {0}'.format(args.period))
else:
    students, all_assignments = aggregate_points_for_each_student(AssignmentApi, AssignmentGroupApi, logincookie, period_id)
    csv_approvedstats(students, all_assignments)
                       help='Path to assignment to copy to. E.g: "duck1010.spring2010.assignment2"')
argparser.add_argument('--allow_nonempty', default=False, action='store_true',
                       help=('Allow non-empty target assignment? If you enable '
                             'this, students are copied to the target even if they '
                             'already exist in a group within the target.'))
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.source, '--source', 3)
    source_assignment_id = find_assignment_id_by_shortnames(AssignmentApi, logincookie,
                                                            *path)
except LookupError:
    raise SystemExit('Assignment {0} not found.'.format(args.source))

try:
    path = split_path(args.target, '--target', 3)
    target_assignment_id = find_assignment_id_by_shortnames(AssignmentApi, logincookie,
                                                            *path)
except LookupError:
    raise SystemExit('Assignment {0} not found.'.format(args.target))
sourcegroups = find_groups_in_assignment(AssignmentGroupApi, logincookie, source_assignment_id,
                                         result_fieldgroups=['feedback', 'users', 'tags'])

if not args.allow_nonempty: