if __name__ == "__main__":
    from a7p import load
    from a7p.yupy_schema import validate as yupy_validate
    from yupy import ValidationError
    import timeit
    import tqdm
    from pathlib import Path

    # test index switch distance
    with open("tests/data/example2.a7p", "rb") as fp:
        p = load(fp, fail_fast=True, validate_=False)
        try:
            yupy_validate(p, fail_fast=False)
        except ValidationError as err:
            for msg in err.messages:
                print(msg)

    d = Path("a7p-lib/gallery").rglob("*")
    fs = [p for p in d if p.is_file()]
    errs = []
    for f in tqdm.tqdm(fs):
        with open(f, "rb") as fp:
            p = load(fp, fail_fast=True, validate_=False)
            try:
                yupy_validate(p, fail_fast=False)
            except ValidationError as err:
                errs.append(err)

    print("errs:", len(errs))

    if errs:
        for e in errs[0].errors:
            print("Violation:")
            print(f"\tPath\t:\t{e.path}")
            val_repr = repr(e.invalid_value).replace("\n", "")
            truncated = (val_repr[:75] + "...") if len(val_repr) > 75 else val_repr
            print(f"\tValue\t:\t{truncated}")
            print(f"\tReason\t:\t{e.constraint.format_message}")

    def speedtest(validator):
        for f in tqdm.tqdm(fs, desc=validator.__name__):
            # with open("example.a7p", 'rb') as fp:
            with open(f, "rb") as fp:
                p = load(fp, validate_=False, fail_fast=True)
                try:
                    validator(p)
                except ValidationError:
                    pass

    def v_new():
        speedtest(yupy_validate)

    num = 1
    tnew = timeit.timeit(v_new, number=num)
    print("yupy validator", tnew)  # 0.0059s
