import tkinter as tk

import pytest

from tarot.tarot_api import Tarot
from tarot.ui import CubeDisplay


def test_recursive_binding():
    # Mock Tk root and widgets
    class MockWidget:
        def __init__(self):
            self.children = []
            self.bindings = {}

        def bind(self, key, callback):
            self.bindings[key] = callback

        def winfo_children(self):
            return self.children

        def add_child(self, child):
            self.children.append(child)

    # Monkey patch tk
    original_tk = tk.Tk
    original_frame = tk.ttk.Frame

    try:
        # We don't need to mock everything, just enough to test _bind_recursive

        cube = Tarot.cube
        # We can instantiate CubeDisplay without showing it
        display = CubeDisplay(cube)

        # Create a mock widget tree
        parent = MockWidget()
        child1 = MockWidget()
        child2 = MockWidget()
        grandchild = MockWidget()

        parent.add_child(child1)
        parent.add_child(child2)
        child1.add_child(grandchild)

        # Run recursive binding
        display._bind_recursive(parent)

        # Verify bindings
        assert "<ButtonPress-1>" in parent.bindings
        assert "<B1-Motion>" in parent.bindings

        assert "<ButtonPress-1>" in child1.bindings
        assert "<B1-Motion>" in child1.bindings

        assert "<ButtonPress-1>" in child2.bindings
        assert "<B1-Motion>" in child2.bindings

        assert "<ButtonPress-1>" in grandchild.bindings
        assert "<B1-Motion>" in grandchild.bindings

    finally:
        pass
