import os
import sys
import importlib
from itertools import accumulate, chain
from pathlib import Path
import ruamel.yaml as yaml

os.environ["GI_TYPELIB_PATH"] = "/usr/local/lib/x86_64-linux-gnu/girepository-1.0"

from ctypes import CDLL
CDLL('libgtk4-layer-shell.so')

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

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

sys.path.insert(0, str((Path(__file__).parent / "shared").resolve()))

import panorama_panel


def get_applet_directories():
    data_home = Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local" / "share"))
    data_dirs = [Path(d) for d in os.getenv("XDG_DATA_DIRS", "/usr/local/share:/usr/share").split(":")]

    all_paths = [data_home / "panorama-panel" / "applets"] + [d / "panorama-panel" / "applets" for d in data_dirs]
    return [d for d in all_paths if d.is_dir()]


def get_config_file():
    config_home = Path(os.getenv("XDG_CONFIG_HOME", Path.home() / ".config"))

    return config_home / "panorama-panel" / "config.yaml"


class ManagerWindow(Gtk.Window):
    def __init__(self):
        super().__init__()
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.set_child(box)
        switch_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
        self.edit_mode_switch = Gtk.Switch()
        switch_box.append(self.edit_mode_switch)
        edit_mode_label = Gtk.Label()
        edit_mode_label.set_text("Panel editing")
        edit_mode_label.set_mnemonic_widget(self.edit_mode_switch)
        switch_box.append(edit_mode_label)
        box.append(switch_box)

        self.edit_mode_switch.connect("state-set", self.set_edit_mode)

        self.set_title("Panel configuration")

        self.connect("close-request", self.on_destroy)

    def on_destroy(self, widget):
        self.destroy()
        if self.get_application():
            self.get_application().manager_window = None

    def set_edit_mode(self, switch, value):
        print(f"Editing is {value}")

        if value:
            for panel in panels:
                panel.set_edit_mode(True)
        else:
            for panel in panels:
                panel.set_edit_mode(False)


class AppletArea(Gtk.Box):
    def __init__(self, orientation=Gtk.Orientation.HORIZONTAL):
        super().__init__()

    def set_edit_mode(self, value):
        child = self.get_first_child()
        while child is not None:
            child.set_sensitive(not value)
            child.set_opacity(0.75 if value else 1)
            child = child.get_next_sibling()


class Panel(Gtk.Window):
    def __init__(self, application: Gtk.Application, monitor: Gdk.Monitor, position: Gtk.PositionType = Gtk.PositionType.TOP, size: int = 40, monitor_index: int = 0):
        super().__init__(application=application)
        self.set_default_size(800, size)
        self.set_decorated(False)
        self.position = position
        self.monitor_index = monitor_index
        self.size = size

        Gtk4LayerShell.init_for_window(self)

        Gtk4LayerShell.set_layer(self, Gtk4LayerShell.Layer.TOP)

        Gtk4LayerShell.auto_exclusive_zone_enable(self)
        Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.TOP, True)
        Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.LEFT, True)
        Gtk4LayerShell.set_anchor(self, Gtk4LayerShell.Edge.RIGHT, True)

        box = Gtk.CenterBox()

        match position:
            case Gtk.PositionType.TOP | Gtk.PositionType.BOTTOM:
                box.set_orientation(Gtk.Orientation.HORIZONTAL)
            case Gtk.PositionType.LEFT | Gtk.PositionType.RIGHT:
                box.set_orientation(Gtk.Orientation.VERTICAL)

        self.set_child(box)

        self.left_area = AppletArea(orientation=box.get_orientation())
        self.centre_area = AppletArea(orientation=box.get_orientation())
        self.right_area = AppletArea(orientation=box.get_orientation())

        box.set_start_widget(self.left_area)
        box.set_center_widget(self.centre_area)
        box.set_end_widget(self.right_area)

        # Add a context menu
        menu = Gio.Menu()

        menu.append("Open _manager", "panel.manager")

        self.context_menu = Gtk.PopoverMenu.new_from_model(menu)
        self.context_menu.set_has_arrow(False)
        self.context_menu.set_parent(self)
        self.context_menu.set_halign(Gtk.Align.START)
        self.context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)

        right_click_controller = Gtk.GestureClick()
        right_click_controller.set_button(3)
        right_click_controller.connect("pressed", self.show_context_menu)

        self.add_controller(right_click_controller)

        action_group = Gio.SimpleActionGroup()
        manager_action = Gio.SimpleAction.new("manager", None)
        manager_action.connect("activate", self.show_manager)
        action_group.add_action(manager_action)
        self.insert_action_group("panel", action_group)

    def set_edit_mode(self, value):
        for area in (self.left_area, self.centre_area, self.right_area):
            area.set_edit_mode(value)

    def show_context_menu(self, gesture, n_presses, x, y):
        rect = Gdk.Rectangle()
        rect.x = int(x)
        rect.y = int(y)
        rect.width = 1
        rect.height = 1

        self.context_menu.set_pointing_to(rect)
        self.context_menu.popup()

    def show_manager(self, _0=None, _1=None):
        print("Showing manager")
        if self.get_application():
            if not self.get_application().manager_window:
                self.get_application().manager_window = ManagerWindow()
                self.get_application().manager_window.set_application(self.get_application())
            self.get_application().manager_window.present()

    def get_orientation(self):
        box = self.get_first_child()
        return box.get_orientation()


def get_all_subclasses(klass: type) -> list[type]:
    subclasses = []
    for subclass in klass.__subclasses__():
        subclasses.append(subclass)
        subclasses += get_all_subclasses(subclass)

    return subclasses

def load_packages_from_dir(dir_path: Path):
    loaded_modules = []

    for path in dir_path.iterdir():
        if path.name.startswith("_"):
            continue

        if path.is_dir() and (path / "__init__.py").exists():
            module_name = path.name
            spec = importlib.util.spec_from_file_location(module_name, path / "__init__.py")
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)
            loaded_modules.append(module)
        else:
            continue

    return loaded_modules


PANEL_POSITIONS = {
    "top": Gtk.PositionType.TOP,
    "bottom": Gtk.PositionType.BOTTOM,
    "left": Gtk.PositionType.LEFT,
    "right": Gtk.PositionType.RIGHT,
}


PANEL_POSITIONS_REVERSE = {
    Gtk.PositionType.TOP: "top",
    Gtk.PositionType.BOTTOM: "bottom",
    Gtk.PositionType.LEFT: "left",
    Gtk.PositionType.RIGHT: "right",
}


class PanoramaPanel(Gtk.Application):
    def __init__(self):
        super().__init__(application_id="com.roundabout_host.panorama.panel")
        self.display = Gdk.Display.get_default()
        self.monitors = self.display.get_monitors()
        self.applets_by_name = {}
        self.panels = []
        self.manager_window = None

    def do_startup(self):
        Gtk.Application.do_startup(self)
        for i, monitor in enumerate(self.monitors):
            geometry = monitor.get_geometry()
            print(f"Monitor {i}: {geometry.width}x{geometry.height} at {geometry.x},{geometry.y}")

        all_applets = list(chain.from_iterable(load_packages_from_dir(d) for d in get_applet_directories()))
        print("Applets:")
        subclasses = get_all_subclasses(panorama_panel.Applet)
        for subclass in subclasses:
            if subclass.__name__ in self.applets_by_name:
                print(f"Name conflict for applet {subclass.__name__}. Only one will be loaded.", file=sys.stderr)
            self.applets_by_name[subclass.__name__] = subclass

        with open(get_config_file(), "r") as config_file:
            yaml_loader = yaml.YAML(typ="rt")
            yaml_file = yaml_loader.load(config_file)
            for panel_data in yaml_file["panels"]:
                position = PANEL_POSITIONS[panel_data["position"]]
                monitor_index = panel_data["monitor"]
                monitor = self.monitors[monitor_index]
                size = panel_data["size"]

                panel = Panel(self, monitor, position, size, monitor_index)
                self.panels.append(panel)
                panel.show()

                print(f"{size}px panel on {position} edge of monitor {monitor_index}")

                for area_name, area in (("left", panel.left_area), ("centre", panel.centre_area), ("right", panel.right_area)):
                    applet_list = panel_data["applets"].get(area_name)
                    if applet_list is None:
                        continue

                    for applet in applet_list:
                        item = list(applet.items())[0]
                        AppletClass = self.applets_by_name[item[0]]
                        options = item[1]
                        applet_widget = AppletClass(orientation=panel.get_orientation(), config=options)

                        area.append(applet_widget)

    def do_activate(self):
        Gio.Application.do_activate(self)

    def save_config(self):
        with open(get_config_file(), "w") as config_file:
            yaml_writer = yaml.YAML(typ="rt")
            data = {"panels": []}
            for panel in self.panels:
                panel_data = {
                    "position": PANEL_POSITIONS_REVERSE[panel.position],
                    "monitor": panel.monitor_index,
                    "size": panel.size,
                    "applets": {}
                }

                for area_name, area in (("left", panel.left_area), ("centre", panel.centre_area), ("right", panel.right_area)):
                    panel_data["applets"][area_name] = []
                    applet = area.get_first_child()
                    while applet is not None:
                        panel_data["applets"][area_name].append({
                            applet.__class__.__name__: applet.get_config(),
                        })

                        applet = applet.get_next_sibling()

                data["panels"].append(panel_data)

            yaml_writer.dump(data, config_file)

    def do_shutdown(self):
        print("Shutting down")
        Gtk.Application.do_shutdown(self)
        self.save_config()


if __name__ == "__main__":
    app = PanoramaPanel()
    app.run(sys.argv)
