"""
Launcher applets for the Panorama panel.
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public Licence as published by
the Free Software Foundation, either version 3 of the Licence, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public Licence for more details.

You should have received a copy of the GNU General Public Licence
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""

import os
import subprocess
from pathlib import Path
import panorama_panel

import gi
gi.require_version("Gtk", "4.0")

from gi.repository import Gtk, GLib, Gio, Gdk


class CommandButton(panorama_panel.Applet):
    name = "Command button"
    description = "Run a command when clicked"

    def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
        super().__init__(orientation=orientation, config=config)
        if config is None:
            config = {}
        self.button = Gtk.Button()
        self.button.set_has_frame(False)   # flat look
        self.inner_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        self.button.set_child(self.inner_box)
        self.icon_name = config.get("icon_name")
        self.icon_size = config.get("icon_size", 24)
        self.label = config.get("label")
        self.command = config.get("command")
        self.button.connect("clicked", self.execute)
        if config.get("icon_name"):
            self.inner_box.append(Gtk.Image(icon_name=config["icon_name"], pixel_size=config.get("icon_size", 24)))
        if config.get("label"):
            self.inner_box.append(Gtk.Label.new(config["label"]))

        self.append(self.button)

    def execute(self, button, *args):
        subprocess.Popen(self.command, shell=True, start_new_session=True)

    def get_config(self):
        return {"label": self.label, "icon_name": self.icon_name, "icon_size": self.icon_size, "command": self.command}


class DesktopFileButton(panorama_panel.Applet):
    name = "Desktop file button"
    description = "Launch a desktop file when clicked"

    def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
        super().__init__(orientation=orientation, config=config)
        if config is None:
            config = {}
        self.button = Gtk.Button()
        self.button.set_has_frame(False)   # flat look
        self.inner_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        self.button.set_child(self.inner_box)
        self.desktop_file = config.get("desktop_file")
        self.app_id = config.get("app_id")
        if self.desktop_file:
            self.app_info = Gio.DesktopAppInfo.new_from_filename(self.desktop_file)
        elif self.app_id:
            self.app_info = Gio.DesktopAppInfo.new(self.app_id)
        self.button.connect("clicked", self.execute)
        self.icon_size = config.get("icon_size", 24)
        self.inner_box.append(Gtk.Image(gicon=self.app_info.get_icon(), pixel_size=self.icon_size))

        self.append(self.button)

    def execute(self, button, *args):
        self.app_info.launch()

    def get_config(self):
        return {"desktop_file": self.desktop_file, "app_id": self.app_id, "icon_size": self.icon_size}
