#!/usr/bin/env python3

import glob
import os
import pathlib
import sys

argv_0 = pathlib.Path(sys.argv[0])

try:
    import pandas
    import numpy
except ImportError:
    print(f"{argv_0.name} requires both Pandas and NumPy.")
    print()
    print("Run this:")
    print(f"  % {sys.executable} -m pip install pandas numpy")
    sys.exit(-1)

def preload_local_starvote():
    """
    Pre-load the local "starvote" module, to preclude finding
    an already-installed one on the path.
    """
    import sys
    import pathlib
    argv_0 = pathlib.Path(sys.argv[0])
    starvote_dir = argv_0.parent.resolve()
    while True:
        starvote_init = starvote_dir / "starvote" / "__init__.py"
        if starvote_init.is_file():
            break
        starvote_dir = starvote_dir.parent
    sys.path.insert(0, str(starvote_dir))
    import starvote
    return starvote_dir

starvote_dir = preload_local_starvote()
import starvote.reference

starvote.reference.monkey_patch()

# load test scaffolding stuff (e.g. custom tiebreakers)
# from unit test suite.
# we don't actually need the symbols from inside,
# they monkey-patch themselves in.
sys.path.insert(0, str(starvote_dir / "tests"))
import harness


os.chdir(starvote_dir)


text = []

def text_clear():
    text.clear()

def text_print(*a, sep=" ", end="\n"):
    text.append(sep.join(str(o) for o in a))
    text.append(end)

def text_getvalue():
    output = "".join(text)
    text.clear()
    return output


files_written = 0

work = []

for extension in (".csv", ".starvote"):
    for election_path in glob.glob(f"test_elections/*{extension}"):
        output_path = election_path.replace(extension, ".txt")
        work.append((election_path, output_path))

work.sort()

for election_path, output_path in work:
    print(election_path)
    text_clear()
    starvote.main_with_usage(["-r", election_path], print=text_print)
    got = text_getvalue().strip() + "\n"

    with open(output_path, "wt") as f:
        f.write(got)
    files_written += 1

print(f"{files_written} output files written.")
