import gi
import os
import shutil
import subprocess

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


class Gimp3LayoutSwitcher(Gtk.Window):
    def __init__(self):
        super().__init__(title="GIMP3 Layout Switcher")
        self.set_border_width(0)  # No border to mimic XFWM4 title menu style
        self.set_default_size(400, 200)

        # Define layout paths
        self.layouts = {
            "Default Factory GIMP Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/default-gimp-3.0/GIMP",
            "Improved Default GIMP Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/better-gimp-3.0/GIMP",
            "GIMP Studio Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/gimp-studio-3.0/GIMP",
            "Adobe Photoshop 2024 Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/adobe-photoshop-2024/GIMP",
            "Adobe Photoshop CS6 Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/adobe-photoshop-cs6/GIMP",
            "Affinity Photo Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/affinity-photo/GIMP",
            "Clip Studio Paint Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/clip-studio-paint/GIMP",
            "FireAlpaca Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/firealpaca/GIMP",
            "GIMPShop Reloaded": "/opt/gimp3-layout-switcher/gimp-3.0-layout/gimpshop-reloaded/GIMP",
            "Krita Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/krita/GIMP",
            "MediBang Paint Pro Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/medibang-paint-pro/GIMP",
            "openCanvas 7 Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/opencanvas-7.0/GIMP",
            "Paint.NET Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/paint-dot-net/GIMP",
            "Paint Tool Sai Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/paint-tool-sai/GIMP",
            "PhotoGIMP Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/photogimp/GIMP",
            "Pro Paint Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/pro-paint/GIMP",
            "Sumopaint 2022 Layout": "/opt/gimp3-layout-switcher/gimp-3.0-layout/sumopaint-2022/GIMP",
        }

        # User GIMP config directory
        self.user_config_path = os.path.expanduser("~/.config/GIMP")

        # Create main content box for widgets (excluding menu bar)
        content_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        content_box.set_border_width(10)

        # Create menu bar widget
        menu_bar = self.create_menu_bar()

        # Root vertical box to pack menu bar and content box vertically
        root_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
        self.add(root_vbox)

        # Pack menu bar directly under title bar (no spacing)
        root_vbox.pack_start(menu_bar, False, False, 0)

        # Pack main content below menu bar, expanding to fill
        root_vbox.pack_start(content_box, True, True, 0)

        # Add widgets inside content_box
        label = Gtk.Label(label="Choose GIMP3 Layout:")
        label.set_xalign(0)  # Left align
        content_box.pack_start(label, False, False, 0)

        self.layout_combo = Gtk.ComboBoxText()
        self.layout_combo.append_text("Please select your GIMP3 layout")  # Placeholder
        for layout_name in self.layouts.keys():
            self.layout_combo.append_text(layout_name)
        self.layout_combo.set_active(0)
        content_box.pack_start(self.layout_combo, False, False, 0)

        self.status_label = Gtk.Label(label="Status: None")
        content_box.pack_start(self.status_label, False, False, 0)

        self.apply_button = Gtk.Button(label="Apply")
        self.apply_button.connect("clicked", self.on_apply_button_clicked)
        content_box.pack_start(self.apply_button, False, False, 0)

    def create_menu_bar(self):
        menu_bar = Gtk.MenuBar()

        # File menu
        file_menu = Gtk.Menu()
        file_item = Gtk.MenuItem(label="File")
        file_item.set_submenu(file_menu)

        quit_item = Gtk.MenuItem(label="Quit")
        quit_item.connect("activate", Gtk.main_quit)
        file_menu.append(quit_item)

        menu_bar.append(file_item)

        # Help menu
        help_menu = Gtk.Menu()
        help_item = Gtk.MenuItem(label="Help")
        help_item.set_submenu(help_menu)

        about_item = Gtk.MenuItem(label="About")
        about_item.connect("activate", self.show_about_dialog)
        help_menu.append(about_item)

        menu_bar.append(help_item)

        return menu_bar

    def show_about_dialog(self, widget):
        about_script = "/opt/gimp3-layout-switcher/about.py"
        subprocess.Popen(["python3", about_script])

    def on_apply_button_clicked(self, widget):
        selected_layout = self.layout_combo.get_active_text()
        if not selected_layout or selected_layout == "Please select your GIMP3 layout":
            self.status_label.set_text("Status: Please select a layout!")
            return

        source_path = self.layouts[selected_layout]

        try:
            if os.path.exists(self.user_config_path):
                shutil.rmtree(self.user_config_path)

            shutil.copytree(source_path, self.user_config_path)
            self.status_label.set_text("Status: Done! Please Restart GIMP3!")
        except Exception as e:
            self.status_label.set_text(f"Status: Error - {e}")


def main():
    app = Gimp3LayoutSwitcher()
    app.connect("destroy", Gtk.main_quit)
    app.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()
