#!/usr/bin/python3

import gettext
import subprocess
from gi.repository import GLib
import notify2
import os

localesApp="software-properties"
gettext.textdomain(localesApp)

_ = gettext.gettext

OVERRIDE_NO_ACTIONS = True

class App():
    def __init__(self):
        if not notify2.init("Default Action Test", mainloop='glib'):
            print("Failed to init notify2")
            exit()
        self.check()

    def check(self):

        number = subprocess.check_output(["sh /usr/bin/get-flatpak-updates-number"], shell=True, universal_newlines=True).strip()

        print("number =", number);

        if (number == "0"):
            print("No updates");
            os._exit(0)
        else:
            print("Creating notification")
            server_capabilities = notify2.get_server_caps()
            n = notify2.Notification(_('Flatpak apps update is available'), _('Click here to open update list'), "/usr/share/icons/Yaru/48x48@2x/apps/software-store.png")
            if ('actions' in server_capabilities) or OVERRIDE_NO_ACTIONS:
                # Default action - click on notification
                n.add_action("default", "Default Action", self.notification_callback)
                # Cancel action - close notification, remind me later
                n.add_action("default2", _('Remind me later'), self.notification_callback_later)
            n.connect('closed', self.on_notification_closed)
            if not n.show():
                print("Failed to send notification")
                exit()

    def on_notification_closed (self, notification):
        print("on_notification_closed: closing notification and exiting update-flatpak")
        os._exit(0)

    def notification_callback(self, notification, action_name):
        print("notification_callback: start")
        subprocess.call(["/usr/bin/inainstall", "update-flatpak"])

    def notification_callback_later(self, notification, action):
        print("notification_callback_later: start")

app = App()
GLib.MainLoop().run()
