from pynput.keyboard import Listener

from .util import name_to_key


class KeyCombinationListener:
    """
    Simple and bad key combination listener.
    """
    
    def __init__(self):
        self.current_keys = set()
        self.callbacks = {}
        self.listener = Listener(on_press=self.on_key_press, on_release=self.on_key_release)

    def add_comb(self, keys, callback):
        self.callbacks[tuple([name_to_key(key_name) for key_name in sorted(keys)])] = callback

    def on_key_press(self, key):
        self.current_keys.add(key)
        for comb, callback in self.callbacks.items():
            if all(k in self.current_keys for k in comb):
                return callback()

    def on_key_release(self, key):
        if key in self.current_keys:
            self.current_keys.remove(key)

    def start(self):
        self.listener.start()

    def stop(self):
        self.listener.stop()