import json
import sys


def load_foldernames(filename):
    output = []

    with open(filename, "r") as f:
        data = json.load(f)

    for test_data in data:
        output.append(test_data["foldername"])

    return sorted(set(output))


def load_tests(filename):
    output = []

    with open(filename, "r") as f:
        data = json.load(f)

    for test_data in data:
        if test_data.get("hasunittests", False):
            output.append(test_data["foldername"])

    return sorted(set(output))


def main():
    examples = load_foldernames(
        "wpilibjExamples/src/main/java/org/wpilib/examples/examples.json"
    )
    commands = load_foldernames(
        "wpilibjExamples/src/main/java/org/wpilib/commands/commands.json"
    )
    templates = load_foldernames(
        "wpilibjExamples/src/main/java/org/wpilib/templates/templates.json"
    )
    snippets = load_foldernames(
        "wpilibjExamples/src/main/java/org/wpilib/snippets/snippets.json"
    )
    example_tests = load_tests(
        "wpilibjExamples/src/main/java/org/wpilib/examples/examples.json"
    )
    snippet_tests = load_tests(
        "wpilibjExamples/src/main/java/org/wpilib/snippets/snippets.json"
    )

    output_file = "wpilibjExamples/example_projects.bzl"
    if len(sys.argv) == 2:
        output_file = sys.argv[1]

    with open(output_file, "w") as f:
        f.write('EXAMPLE_FOLDERS = [\n    "' + '",\n    "'.join(examples) + '",\n]\n\n')
        f.write(
            'COMMANDS_V2_FOLDERS = [\n    "' + '",\n    "'.join(commands) + '",\n]\n\n'
        )
        f.write('SNIPPET_FOLDERS = [\n    "' + '",\n    "'.join(snippets) + '",\n]\n\n')
        f.write(
            'TEMPLATE_FOLDERS = [\n    "' + '",\n    "'.join(templates) + '",\n]\n\n'
        )
        f.write(
            'EXAMPLE_TESTS_FOLDERS = [\n    "'
            + '",\n    "'.join(example_tests)
            + '",\n]\n\n'
        )
        f.write(
            'SNIPPET_TESTS_FOLDERS = [\n    "'
            + '",\n    "'.join(snippet_tests)
            + '",\n]\n'
        )


if __name__ == "__main__":
    main()
