# SPDX-FileCopyrightText: 2025 XLeitstelle Planen und Bauen <xleitstelle@gv.hamburg.de>
# SPDX-FileContributor: Tobias Kraft <tobias.kraft@gv.hamburg.de>
# SPDX-FileContributor: Anton Jacobsson <anton.jacobsson@init.de>
#
# SPDX-License-Identifier: EUPL-1.2

"""XMAS-Plugin — QGIS plugin entrypoint.

This module is the standard QGIS plugin entry point.  QGIS discovers the
plugin by importing this package and calling `classFactory`.

## Architecture overview

The plugin embeds the XMAS-App web application inside a QGIS dock widget
using `QWebEngineView` (Qt WebEngine).  Bidirectional communication between
the JavaScript frontend and the QGIS Python backend runs over `QWebChannel`.

### Key modules

- `xmas_plugin.xmas_plugin` — `XMASPlugin`: plugin lifecycle
  (`initGui` / `unload`), registers the dock widget, processing provider,
  editor widget factory, and layer actions.
- `xmas_plugin.dock` — `XMASPluginDockWidget`: main UI container
  hosting the web view; handles file dialogs, settings, topology checks, and
  the web channel for JS↔Python messaging.
- `xmas_plugin.plan_manager` — `PlanManager`: creates, loads, and
  syncs plan layers; reads layer definitions from
  `resources/config/layers.toml`.
- `xmas_plugin.server_manager` — `ServerManager`: auto-installs pip
  dependencies, starts the XMAS-App server subprocess, and health-checks the
  connection.
- `xmas_plugin.widgets` — Custom QGIS editor widget factory and
  `QWebChannel` bridge (`FeatureInteractionHandler`).
- `xmas_plugin.provider.vector` — Custom `QgsVectorDataProvider` that
  reads from PostgreSQL and routes writes to the webapp backend via HTTP.
- `xmas_plugin.actions` — Map layer context-menu actions.
- `xmas_plugin.topo_check` — Topology validation (bounds, gaps, overlaps).
- `xmas_plugin.processing` — QGIS Processing toolbox algorithms.
"""


def classFactory(iface):  # pylint: disable=invalid-name
    """Initialize plugin class."""

    deps_satisfied = False

    try:
        from PyQt5.QtWebEngineWidgets import QWebEngineView  # noqa: F401

        deps_satisfied = True
    except ImportError:
        from xmas_plugin import server_manager
        from xmas_plugin.util.helpers import get_user_site_subdirs

        # Build requirement string like "PyQtWebEngine~=5.15"
        req = server_manager.pyqtwebengine_requirement()

        user_site_subdirs = get_user_site_subdirs()

        ok = server_manager.verify_dependency_installation(
            [req],
            parent=iface.mainWindow(),
            label="PyQtWebEngine",
        )

        if ok:
            # check if package was installed to user site, which requires a reload of QGIS
            if get_user_site_subdirs() > user_site_subdirs:
                from qgis.PyQt.QtWidgets import QMessageBox

                from xmas_plugin.util.metadata import PLUGIN_NAME

                restart = QMessageBox.question(
                    iface.mainWindow(),
                    PLUGIN_NAME,
                    "PyQtWebEngine steht erst nach einem Neustart von QGIS zur Verfügung.\n\nQGIS jetzt beenden?",
                )
                if restart == QMessageBox.Yes:
                    iface.actionExit().trigger()

        # Auto-install claimed success. Try the import again:
        try:
            from PyQt5.QtWebEngineWidgets import QWebEngineView  # noqa: F401

            deps_satisfied = True
        except ImportError:
            pass

    from xmas_plugin.xmas_plugin import XMASPlugin

    return XMASPlugin(iface, deps_satisfied)
