import json
from pathlib import Path

import cv2
import dxcam


root = Path(__file__).resolve().parents[1]
results = {"outputs": dxcam.output_info()}

for index in range(8):
    try:
        camera = dxcam.create(device_idx=0, output_idx=index, output_color="BGRA")
        frame = camera.grab()
        if frame is None:
            results[str(index)] = {"frame": None}
        else:
            filename = f".display-{index}.png"
            cv2.imwrite(str(root / filename), frame)
            results[str(index)] = {
                "file": filename,
                "shape": list(frame.shape),
            }
        del camera
    except Exception as error:
        results[str(index)] = {"error": str(error)}

(root / ".display-diagnostic.json").write_text(
    json.dumps(results, indent=2), encoding="utf-8"
)
