from __future__ import annotations

import logging

from gi.repository import Gdk, GdkX11  # type: ignore[missing-module-attribute] - fails in docker image for some reason

from ulauncher.gi import Gio

logger = logging.getLogger(__name__)


def get_monitor(use_mouse_position: bool = False) -> Gdk.Monitor | None:
    display = Gdk.Display.get_default()
    if not display:
        logger.warning("Could not get default display")
        return None

    if use_mouse_position:
        # GdkX11.X11Display.get_default() resolves to the inherited Gdk.Display.get_default(),
        # so on Wayland it returns the Wayland display, where the pointer position reads (0, 0)
        if (
            isinstance(display, GdkX11.X11Display)
            and (seat := display.get_default_seat())
            and (pointer := seat.get_pointer())
        ):
            (_, x, y) = pointer.get_position()
            return display.get_monitor_at_point(x, y)
        logger.debug("Could not get mouse position (requires X11). Defaulting to primary or first monitor")

    return display.get_primary_monitor() or display.get_monitor(0)


def get_monitor_geometries() -> list[Gdk.Rectangle]:
    display = Gdk.Display.get_default()
    if not display:
        logger.warning("Could not get default display")
        return []
    return [monitor.get_geometry() for i in range(display.get_n_monitors()) if (monitor := display.get_monitor(i))]


def get_text_scaling_factor() -> float:
    # GTK seems to already compensate for monitor scaling, so this just returns font scaling
    # GTK doesn't seem to allow different scaling factors on different displays
    # Text_scaling allow fractional scaling

    return Gio.Settings.new("org.gnome.desktop.interface").get_double("text-scaling-factor")
