#!/usr/bin/python3

from gi.repository import GLib
import threading
import re
import dbus
from dbus.mainloop.glib import DBusGMainLoop

bus_name = 'org.freedesktop.portal.Desktop'
request_interface = 'org.freedesktop.portal.Request'
shortcut_interface = 'org.freedesktop.portal.GlobalShortcuts'
object_path = '/org/freedesktop/portal/desktop'

shortcuts = [
    (
        "binding1",
        {
            "description": "Binding #1",
#            "preferred-trigger": "CTRL+a",
        },
    ),
    (
        "binding2",
        {
            "description": "Binding #2",
#            "preferred-trigger": "CTRL+b",
        },
    ),
]

class PortalGlobalShortcuts:
    def __init__(self):
        DBusGMainLoop(set_as_default=True)

        self.bus = dbus.SessionBus()
        self.portal = self.bus.get_object(bus_name, object_path)
        self.CreateSession()

    def BindShortcuts(self):
        self.portal.BindShortcuts(self.session, shortcuts, '', {}, dbus_interface=shortcut_interface)
        print("Bind Shortcuts")

    def ListShortcuts(self):
        self.portal.ListShortcuts(self.session, {}, dbus_interface=shortcut_interface)
        print("Listing Shortcuts")

    def ShortcutsChanged(self, shortcuts):
        print(shortcuts)

    def Callback(self, response, results):
        print("Callback Event")
        self.ShortcutsChanged(results['shortcuts'])

    def CreateSessionCallback(self, response, results):
        print("CreateSession called back")
        self.session = results['session_handle']
        reply = self.portal.ListShortcuts(self.session, {}, dbus_interface=shortcut_interface)

        self.bus.add_signal_receiver(self.Callback,
                            'Response',
                            request_interface,
                            bus_name,
                            reply)

        self.BindShortcuts()

    def KeyActivatedCallback(self, session_handle, shortcut_id, timestamp, options):
        print(f"{shortcut_id} activated")

    def KeyDeactivatedCallback(self, session_handle, shortcut_id, timestamp, options):
        print(f"{shortcut_id} deactivated")

    def ShortcutsChangedCallback(self, session_handle, shortcuts):
        print("Shortcuts Changed")
        if session_handle == self.session:
            self.ShortcutsChanged(shortcuts)

    def CreateSession(self):
        options = { 'handle_token': 'mytest',
                    'session_handle_token': 'Mytest',
                  }

        reply = self.portal.CreateSession(options, dbus_interface=shortcut_interface)

        self.bus.add_signal_receiver(self.CreateSessionCallback,
                            'Response',
                            request_interface,
                            bus_name,
                            reply)

        self.bus.add_signal_receiver(self.KeyActivatedCallback,
                            'Activated',
                            shortcut_interface,
                            bus_name,
                            object_path)

        self.bus.add_signal_receiver(self.KeyDeactivatedCallback,
                            'Deactivated',
                            shortcut_interface,
                            bus_name,
                            object_path)

        self.bus.add_signal_receiver(self.ShortcutsChangedCallback,
                            'ShortcutsChanged',
                            shortcut_interface,
                            bus_name,
                            object_path)

        print("Created Session")

def loop_th():
    loop.run()

loop = GLib.MainLoop()
portal = PortalGlobalShortcuts()

thread = threading.Thread(target=loop_th)
thread.daemon = True
thread.start()

x = input() #hold off quitting
