#!/usr/bin/python
"""
@author Ryan Summers
@date 2-27-2018

@brief Provides SFTP functionality for managing data labeling activities.
"""

import argparse

from rslabel import collect
from rslabel import get
from rslabel import marker
from rslabel import ret
from rslabel import show
from rslabel import stats
from rslabel import upload


if __name__ == '__main__':
    """ Manages command-line parsing for the rslabel application. """
    parser = argparse.ArgumentParser(description='Utility for robosub labeling tasks.')
    sub_parsers = parser.add_subparsers()

    return_parser = sub_parsers.add_parser('return', description='Return image complete and incomplete image annotations.')
    return_parser.add_argument('annotations', type=str, help='The annotation file to upload.')
    return_parser.add_argument('--auto-delete', action='store_true')
    return_parser.set_defaults(func=ret.app)

    collect_parser = sub_parsers.add_parser('collect', description='Collect all done image sets into a single tarball')
    collect_parser.add_argument('tarball', type=str, help='The name of the final tarball to create.')
    collect_parser.set_defaults(func=collect.app)

    get_parser = sub_parsers.add_parser('get', description='Get a dataset for labeling or validation.')
    get_parser.add_argument('--validation', '-V', action='store_true', help='Denotes that data should be grabbed for validation instead of labeling.')
    get_parser.add_argument('--clarification', '-C', action='store_true', help='Denotes that data should be grabbed for clarification instead of labeling.')
    get_parser.set_defaults(func=get.app)

    show_parser = sub_parsers.add_parser('show', description='Get stats about the image dataset including number of each label type and data sets currently in the processing pipeline.')
    show_parser.set_defaults(func=show.app)

    upload_parser = sub_parsers.add_parser('upload', description='Split images out of a ROS bag and upload them to the server.')
    upload_parser.add_argument('bag_file', type=str, help='The bag file to extract and upload')
    upload_parser.add_argument('--files-per-tar', type=int, default=50, help='The number of images per tar archive')
    upload_parser.set_defaults(func=upload.app)

    mark_parser = sub_parsers.add_parser('mark', description='Validate labels on tagged images.')
    mark_parser.add_argument('annotations', type=str, help='The annotation file to validate.')
    mark_parser.add_argument('--config', type=str, help='YAML configuration file that specifies overlay colors for specific labels.')
    mark_parser.add_argument('--label', type=str, help='Only show images with the specified label.')
    mark_parser.add_argument('--beginning', action='store_true', help='Start at the beginning of the images.')
    mark_parser.add_argument('--scale', type=float, help='Specifies image sizing.', default=1.0)
    mark_parser.set_defaults(func=marker.app)

    stats_parser = sub_parsers.add_parser('stats', description='Get stats on user labeling.')
    stats_parser.set_defaults(func=stats.app)

    args = parser.parse_args()

    # Call the appropriate handler function
    try:
        args.func(args)
    except:
        parser.print_help()
