import argparse
import csv
import os
from moss_client.core import submit_and_dl, parse_moss_reports

data_folder = 'data'


def handle_input(user_id, base_folder, parse, only_parse, join_file, batch):
    global data_folder
    abs_path = os.path.abspath(os.path.dirname(__file__))
    root_data_folder = os.path.join(abs_path, data_folder)
    if not os.path.exists(root_data_folder):
        os.makedirs(root_data_folder)
    report_links_file = os.path.join(root_data_folder, 'links_to_moss_reports.html')
    report_csv_file = os.path.join(root_data_folder, 'moss_report.csv')
    if not os.path.isabs(base_folder):
        base_folder = os.path.join(abs_path, base_folder)

    if len(join_file) > 0:
        expected_keys = ["SC_Filepath", "Stackoverflow_Links"]
        with open(join_file, mode='r', encoding='utf-8') as csv_file:
            csv_reader = csv.DictReader(csv_file)
            actual_keys = csv_reader.fieldnames
            if expected_keys[0] != actual_keys[0] or expected_keys[1] != actual_keys[1]:
                print("Error: Unexpected Headers! SC_Filepath and Stackoverflow_Links are required!")
                return -1
    if not only_parse:
        submit_and_dl(user_id, base_folder, report_links_file, batch)
    if parse or only_parse:
        print("Parsing the moss reports...")
        parse_moss_reports(report_links_file, report_csv_file, join_file)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="MOSS CLI client for submitting java files to the service and downloading the report from the "
                    "service locally. Will go through the sub folders of the given folder and submit the java files "
                    "for plagiarism checks and download the reports locally, creating a linking file in the process")
    parser.add_argument('user_id', metavar='U', nargs=1, help="Your user-id for the MOSS service.")
    parser.add_argument('folder', metavar='F', nargs=1, help="The folder whose contents you want to submit.")
    parser.add_argument('-p', '--parse', action='store_true', help="Parses the moss reports into a csv file.")
    parser.add_argument('-o', '--only-parse', action='store_true',
                        help="Only parses the local moss reports and does not submit files and download the reports. "
                             "Requires the reports and the links_to_reports html file created normally by this app.")
    parser.add_argument('-j', '--join-file', nargs=1, default=[""],
                        help="When the parse or only-parse option is given, joins the parsed data with the parsed data.")
    parser.add_argument('-b', '--batch-mode', action='store_true',
                        help="Only submits a 100 folders to the Moss Service, also looks for already processed folders so "
                             "that it does not submit those again.")
    args = parser.parse_args()
    handle_input(args.user_id[0], args.folder[0], args.parse, args.only_parse, args.join_file[0], args.batch_mode)

[((12, 23, 12, 58), 'os.path.join', 'os.path.join', ({(12, 36, 12, 44): 'abs_path', (12, 46, 12, 57): 'data_folder'}, {}), '(abs_path, data_folder)', False, 'import os\n'), ((15, 24, 15, 84), 'os.path.join', 'os.path.join', ({(15, 37, 15, 53): 'root_data_folder', (15, 55, 15, 83): '"""links_to_moss_reports.html"""'}, {}), "(root_data_folder, 'links_to_moss_reports.html')", False, 'import os\n'), ((16, 22, 16, 71), 'os.path.join', 'os.path.join', ({(16, 35, 16, 51): 'root_data_folder', (16, 53, 16, 70): '"""moss_report.csv"""'}, {}), "(root_data_folder, 'moss_report.csv')", False, 'import os\n'), ((36, 13, 39, 117), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((11, 31, 11, 56), 'os.path.dirname', 'os.path.dirname', ({(11, 47, 11, 55): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((13, 11, 13, 43), 'os.path.exists', 'os.path.exists', ({(13, 26, 13, 42): 'root_data_folder'}, {}), '(root_data_folder)', False, 'import os\n'), ((14, 8, 14, 37), 'os.makedirs', 'os.makedirs', ({(14, 20, 14, 36): 'root_data_folder'}, {}), '(root_data_folder)', False, 'import os\n'), ((17, 11, 17, 37), 'os.path.isabs', 'os.path.isabs', ({(17, 25, 17, 36): 'base_folder'}, {}), '(base_folder)', False, 'import os\n'), ((18, 22, 18, 57), 'os.path.join', 'os.path.join', ({(18, 35, 18, 43): 'abs_path', (18, 45, 18, 56): 'base_folder'}, {}), '(abs_path, base_folder)', False, 'import os\n'), ((29, 8, 29, 69), 'moss_client.core.submit_and_dl', 'submit_and_dl', ({(29, 22, 29, 29): 'user_id', (29, 31, 29, 42): 'base_folder', (29, 44, 29, 61): 'report_links_file', (29, 63, 29, 68): 'batch'}, {}), '(user_id, base_folder, report_links_file, batch)', False, 'from moss_client.core import submit_and_dl, parse_moss_reports\n'), ((32, 8, 32, 73), 'moss_client.core.parse_moss_reports', 'parse_moss_reports', ({(32, 27, 32, 44): 'report_links_file', (32, 46, 32, 61): 'report_csv_file', (32, 63, 32, 72): 'join_file'}, {}), '(report_links_file, report_csv_file, join_file)', False, 'from moss_client.core import submit_and_dl, parse_moss_reports\n'), ((23, 25, 23, 49), 'csv.DictReader', 'csv.DictReader', ({(23, 40, 23, 48): 'csv_file'}, {}), '(csv_file)', False, 'import csv\n')]