import gi
import os
import shutil
from pathlib import Path

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

class Gimp3LayoutSwitcher(Gtk.ApplicationWindow):
    def __init__(self, app):
        super().__init__(application=app, title="GIMP3 Layout Switcher")
        self.set_default_size(400, 200)

        # Main layout
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        vbox.set_margin_top(10)
        vbox.set_margin_bottom(10)
        vbox.set_margin_start(10)
        vbox.set_margin_end(10)
        self.set_child(vbox)

        # Label
        label = Gtk.Label(label="Select a layout to apply:")
        vbox.append(label)

        # Layout buttons
        layouts = {
            "Default GIMP Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/default-gimp-3.0/GIMP",
            "Affinity Photo Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/affinity-photo/GIMP",
            "Clip Paint Studio Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/clip-paint-studio/GIMP",
            "FireAlpaca Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/firealpaca/GIMP",
            "MediBang Paint Pro": "/opt/gimp3-layout-switcher/gimp-3.0-layout/medibang-paint-pro/GIMP",
            "Paint Tool Sai Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/paint-tool-sai/GIMP",
            "Photoshop Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/photoshop/GIMP",
            "Sumo Paint 2022": "/opt/gimp3-layout-switcher/gimp-3.0-layout/sumo-paint-2022/GIMP"
        }

        for layout_name, layout_path in layouts.items():
            button = Gtk.Button(label=layout_name)
            button.connect("clicked", self.apply_layout, layout_path)
            vbox.append(button)

        # Quit button
        quit_button = Gtk.Button(label="Quit")
        quit_button.connect("clicked", self.on_quit)
        vbox.append(quit_button)

    def apply_layout(self, button, layout_path):
        home_dir = Path.home()
        config_dir = home_dir / ".config/GIMP"

        # Delete existing GIMP config directory
        if config_dir.exists():
            shutil.rmtree(config_dir)
            print(f"Deleted: {config_dir}")

        # Copy selected layout
        try:
            shutil.copytree(layout_path, config_dir)
            print(f"Applied layout from: {layout_path}")
            self.show_message(f"Layout applied successfully:\n{layout_path}")
        except Exception as e:
            self.show_message(f"Error applying layout:\n{e}")

    def show_message(self, message):
        dialog = Gtk.MessageDialog(
            transient_for=self,
            modal=True,
            message_type=Gtk.MessageType.INFO,
            buttons=Gtk.ButtonsType.OK,
            text="GIMP3 Layout Switcher",
        )
        dialog.set_secondary_text(message)
        dialog.show()
        dialog.connect("response", lambda *args: dialog.close())

    def on_quit(self, button):
        self.get_application().quit()


class Gimp3LayoutSwitcherApp(Gtk.Application):
    def __init__(self):
        super().__init__(application_id="org.example.gimp3-layout-switcher")

    def do_activate(self):
        window = Gimp3LayoutSwitcher(self)
        window.present()


if __name__ == "__main__":
    app = Gimp3LayoutSwitcherApp()
    app.run()
