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(10)
        self.set_default_size(400, 200)

        # Define layout paths
        self.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",
            "Krita": "/opt/gimp3-layout-switcher/gimp-3.0-layout/krita/GIMP",
            "MediBang Paint Pro": "/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 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",
        }

        # Current user directory for GIMP configuration
        self.user_config_path = os.path.expanduser("~/.config/GIMP")

        # Create main layout
        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
        self.add(vbox)

        # Menu bar
        menu_bar = self.create_menu_bar()
        vbox.pack_start(menu_bar, False, False, 0)

        # "Choose GIMP3 Layout" label
        label = Gtk.Label(label="Choose GIMP3 Layout:")
        label.set_xalign(0)  # Align label to the left
        vbox.pack_start(label, False, False, 0)

        # Dropdown menu
        self.layout_combo = Gtk.ComboBoxText()
        self.layout_combo.append_text("Please select your GIMP3 layout")  # Placeholder option
        for layout_name in self.layouts.keys():
            self.layout_combo.append_text(layout_name)
        self.layout_combo.set_active(0)  # Default to the placeholder option
        vbox.pack_start(self.layout_combo, False, False, 0)

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

        # Apply button
        self.apply_button = Gtk.Button(label="Apply")
        self.apply_button.connect("clicked", self.on_apply_button_clicked)
        vbox.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):
        # Get selected layout
        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:
            # Delete existing GIMP configuration
            if os.path.exists(self.user_config_path):
                shutil.rmtree(self.user_config_path)

            # Copy selected layout
            shutil.copytree(source_path, self.user_config_path)

            # Update status
            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()
