import functools
from pathlib import Path
import random
import struct
import time
import os

DATA_PATH = Path(__file__).resolve().parent / "data"

def waste_time_rand():
    time.sleep(random.randint(0, 3))

def waste_time(t: int):
    time.sleep(t)

def generate_binary_file(filename: str, N: int):
    with open(DATA_PATH / filename, 'wb') as f:
        for _ in range(N):
            f.write(struct.pack('f', random.uniform(-1000.0, 1000.0)))

def generate_inputs(n_files, nums_per_file):
    print("GENERATING INPUT FILES")
    DATA_PATH.mkdir(exist_ok=True)
    for file in DATA_PATH.iterdir():
        os.remove(file)
    for i in range(n_files):
        generate_binary_file(f"{i}_input.bin", nums_per_file)

def read_binary_file(filename: str) -> list[float]:
    ret = []
    with open(DATA_PATH / filename, 'rb') as f:
        while True:
            aux = f.read(4)
            if not aux:
                break
            ret.append(struct.unpack('f', aux)[0])
    return ret

def measure(fun):
    @functools.wraps(fun)
    def wrapper(*args, **kwargs):
        start = time.time()
        ret = fun(*args, **kwargs)
        end = time.time()
        print(f"{fun.__name__}: {end - start:.4f} s")
        return ret
    return wrapper

