# Path: tomate/ui/shortcut.py
# class Shortcut(namedtuple("Shortcut", ["name", "value"])):
#     def __str__(self) -> str:
#         return "name={} value={}".format(self.name, self.value)
# 
#     @property
#     def accel_path(self) -> str:
#         return "<tomate>/Global/{}".format(self.name)
# 
# class ShortcutEngine:
#     @inject(config="tomate.config")
#     def __init__(self, config, accel_group=Gtk.AccelGroup()):
#         self._config = config
#         self.accel_group = accel_group
# 
#     def init(self, window: Gtk.Window) -> None:
#         logger.debug("action=init")
#         window.add_accel_group(self.accel_group)
# 
#     def change(self, shortcut: Shortcut) -> None:
#         logger.debug("action=change %s", shortcut)
#         Gtk.AccelMap.change_entry(shortcut.accel_path, *Gtk.accelerator_parse(shortcut.value), True)
# 
#     def connect(self, shortcut: Shortcut, callback: Callable[[], Any]) -> None:
#         logger.debug("action=connect %s", shortcut)
#         Gtk.AccelMap.add_entry(shortcut.accel_path, *self._parse(shortcut))
#         self.accel_group.connect_by_path(shortcut.accel_path, callback)
# 
#     def disconnect(self, shortcut: Shortcut) -> None:
#         logger.debug("action=disconnect %s")
#         self.accel_group.disconnect_key(*self._parse(shortcut))
# 
#     def label(self, shortcut: Shortcut) -> str:
#         return Gtk.accelerator_get_label(*self._parse(shortcut))
# 
#     def _parse(self, shortcut: Shortcut) -> Tuple[int, Gdk.ModifierType]:
#         accelerator = self._config.get(self._config.SHORTCUT_SECTION, shortcut.name, fallback=shortcut.value)
#         return Gtk.accelerator_parse(accelerator)
# 
# Path: tomate/ui/testing.py
# def active_shortcut(shortcut_engine: ShortcutEngine, shortcut: Shortcut, window: Optional[Gtk.Window] = None) -> bool:
#     if window is None:
#         window = Gtk.Window()
# 
#     shortcut_engine.init(window)
#     key, mod = Gtk.accelerator_parse(shortcut.value)
#     return Gtk.accel_groups_activate(window, key, mod)


import pytest
from gi.repository import Gtk
from wiring.scanning import scan_to_graph
from tomate.ui import Shortcut, ShortcutEngine
from tomate.ui.testing import active_shortcut



@pytest.fixture
def shortcut_engine(bus, config, graph) -> ShortcutEngine:
    graph.register_instance("tomate.config", config)
    scan_to_graph(["tomate.ui.shortcut"], graph)
    return graph.get("tomate.ui.shortcut")


def test_module(graph, shortcut_engine):
    instance = graph.get("tomate.ui.shortcut")

    assert isinstance(instance, ShortcutEngine)
    assert instance is shortcut_engine


def test_label(shortcut_engine):
    label = shortcut_engine.label(Shortcut("test", ""))

    assert label == "Ctrl+S"


def test_label_with_fallback(shortcut_engine):

# Path: tomate/ui/shortcut.py
# class Shortcut(namedtuple("Shortcut", ["name", "value"])):
#     def __str__(self) -> str:
#         return "name={} value={}".format(self.name, self.value)
# 
#     @property
#     def accel_path(self) -> str:
#         return "<tomate>/Global/{}".format(self.name)
# 
# class ShortcutEngine:
#     @inject(config="tomate.config")
#     def __init__(self, config, accel_group=Gtk.AccelGroup()):
#         self._config = config
#         self.accel_group = accel_group
# 
#     def init(self, window: Gtk.Window) -> None:
#         logger.debug("action=init")
#         window.add_accel_group(self.accel_group)
# 
#     def change(self, shortcut: Shortcut) -> None:
#         logger.debug("action=change %s", shortcut)
#         Gtk.AccelMap.change_entry(shortcut.accel_path, *Gtk.accelerator_parse(shortcut.value), True)
# 
#     def connect(self, shortcut: Shortcut, callback: Callable[[], Any]) -> None:
#         logger.debug("action=connect %s", shortcut)
#         Gtk.AccelMap.add_entry(shortcut.accel_path, *self._parse(shortcut))
#         self.accel_group.connect_by_path(shortcut.accel_path, callback)
# 
#     def disconnect(self, shortcut: Shortcut) -> None:
#         logger.debug("action=disconnect %s")
#         self.accel_group.disconnect_key(*self._parse(shortcut))
# 
#     def label(self, shortcut: Shortcut) -> str:
#         return Gtk.accelerator_get_label(*self._parse(shortcut))
# 
#     def _parse(self, shortcut: Shortcut) -> Tuple[int, Gdk.ModifierType]:
#         accelerator = self._config.get(self._config.SHORTCUT_SECTION, shortcut.name, fallback=shortcut.value)
#         return Gtk.accelerator_parse(accelerator)
# 
# Path: tomate/ui/testing.py
# def active_shortcut(shortcut_engine: ShortcutEngine, shortcut: Shortcut, window: Optional[Gtk.Window] = None) -> bool:
#     if window is None:
#         window = Gtk.Window()
# 
#     shortcut_engine.init(window)
#     key, mod = Gtk.accelerator_parse(shortcut.value)
#     return Gtk.accel_groups_activate(window, key, mod)


# Path: tests/ui/test_shortcut.py
import pytest
from gi.repository import Gtk
from wiring.scanning import scan_to_graph
from tomate.ui import Shortcut, ShortcutEngine
from tomate.ui.testing import active_shortcut




@pytest.fixture
def shortcut_engine(bus, config, graph) -> ShortcutEngine:
    graph.register_instance("tomate.config", config)
    scan_to_graph(["tomate.ui.shortcut"], graph)
    return graph.get("tomate.ui.shortcut")


def test_module(graph, shortcut_engine):
    instance = graph.get("tomate.ui.shortcut")

    assert isinstance(instance, ShortcutEngine)
    assert instance is shortcut_engine


def test_label(shortcut_engine):
    label = shortcut_engine.label(Shortcut("test", ""))

    assert label == "Ctrl+S"


def test_label_with_fallback(shortcut_engine):

    label = shortcut_engine.label(Shortcut("", "<control>p"))