import gi
import os

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GdkPixbuf


class AboutDialog(Gtk.AboutDialog):
    def __init__(self):
        super().__init__()
        self.set_program_name("GIMP3 Layout Switcher")
        self.set_version("0.0.1+20250310")
        self.set_comments("A tool to switch between GIMP 3.0 layouts.")
        self.set_authors(["linuxmangaka@gmail.com"])
        self.set_license_type(Gtk.License.GPL_3_0)
        self.set_license(
            "This program is free software: you can redistribute it and/or modify "
            "it under the terms of the GNU General Public License as published by "
            "the Free Software Foundation, either version 3 of the License, or "
            "(at your option) any later version.\n\n"
            "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 License for more details."
        )
        self.set_wrap_license(True)
        self.connect("response", self.on_close)

        # Set the GIMP icon as the logo
        icon_path = self.get_gimp_icon_path()
        if icon_path:
            self.set_logo(GdkPixbuf.Pixbuf.new_from_file(icon_path))
        else:
            print("No GIMP icon found, About dialog will display without a logo.")

    def get_gimp_icon_path(self):
        """
        Search for the GIMP icon in common Linux icon paths.
        """
        icon_names = ["gimp", "gimp-icon", "gimp-3.0"]
        search_paths = [
            "/usr/share/icons/hicolor/48x48/apps/",
            "/usr/share/icons/hicolor/64x64/apps/",
            "/usr/share/icons/hicolor/128x128/apps/",
            "/usr/share/pixmaps/",
        ]

        for icon_name in icon_names:
            for path in search_paths:
                icon_file = os.path.join(path, f"{icon_name}.png")
                if os.path.exists(icon_file):
                    return icon_file

        return None

    def on_close(self, dialog, response):
        dialog.destroy()


def main():
    about_dialog = AboutDialog()
    about_dialog.run()


if __name__ == "__main__":
    main()
