import subprocess, os, random
from polygenerator import (random_polygon)
from shapely import Polygon

def generate(v, n, scale=5000):

    output = f"{v} {n}\n"

    poly = random_polygon(n)
    poly = [(int(v[0]*scale), int(v[1]*scale)) for v in poly]
    while not Polygon(poly).is_valid:
        poly = random_polygon(n)
        poly = [(int(v[0]*scale), int(v[1]*scale)) for v in poly]

    for v in poly:
        output += str(v[0]) + " " + str(v[1]) + "\n"

    return output


if __name__ == '__main__':
    minv, maxv = 2, 5000
    minn, maxn = 3, 50

    def random_case():
        v=random.randint(minv,maxv)
        n=random.randint(minn,maxn)

        return v, n

    random_case_count = 25

    # generate random cases with specific parameters:
    cases = [
        # fully random cases
        *[[*random_case(), 1] for _ in range(random_case_count)]
    ]

    os.makedirs("../data/secret", exist_ok=True) # make data/secret directory if doesn't exist

    test_number = 1
    for i, values in enumerate(cases):
        v, n, count = values
        for j in range(count):
            path = f"../data/secret/r_{test_number:02d}"
            test_number += 1

            test = generate(v, n)
            with open(path + ".in", "w+", newline="\n") as f:
                f.write(test)

            # apply solution to find result
            answer = subprocess.run(
                ["py", "../submissions/accepted/am-polylabel.py"],
                input=test, text=True, capture_output=True)

            with open(path + ".ans", "w+", newline="\n") as f:
                f.write(answer.stdout)
