#!/usr/bin/env python3
import glob
import os
from PIL import Image
from strategy import State


def saved_idents():
    return sorted(int(path[13:-4]) for path in glob.glob('screens/board*.png'))


if __name__ == '__main__':
    ok = fail = notfound = 0

    for ident in saved_idents():
        truth_path = 'screens/true%d.txt' % ident
        if os.path.exists(truth_path):
            board = Image.open('screens/board%d.png' % ident).convert('HSV')
            state = State.detect(board, pad=0)
            detected = state.tostring()
            with open(truth_path) as f:
                expected = f.read()
            if detected != expected:
                print('=' * 20)
                print('board', ident, 'FAIL')
                print('expected:')
                print(expected)
                print('detected:')
                print(detected, end='')
                print('=' * 20)
                fail += 1
            else:
                print('board', ident, 'OK')
                ok += 1
        else:
            print(truth_path, 'does not exist')
            notfound += 1

    print('%d boards, %d ok, %d fail' %(ok + fail, ok, fail))
