# -*- coding: utf-8 -*-
"""
LVT Plugin Suite — QGIS Plugin Package Initializer.

Author: Lộc Vũ Trung (LVT) / Slow Forest
License: GPL-3.0
"""

# ── QGIS 4.x Compatibility Patch ─────────────────────────────────
# QgsWkbTypes was deprecated in QGIS 3.30 and may be removed in 4.x.
# This patch ensures all plugin code using QgsWkbTypes keeps working.
try:
    from qgis.core import QgsWkbTypes  # noqa: F401
except ImportError:
    # QGIS 4.x: recreate QgsWkbTypes from new Qgis enums
    from qgis.core import Qgis
    import qgis.core as _qgis_core

    class _QgsWkbTypesCompat:
        """Compatibility shim for QgsWkbTypes removed in QGIS 4.x."""
        PointGeometry = Qgis.GeometryType.Point
        LineGeometry = Qgis.GeometryType.Line
        PolygonGeometry = Qgis.GeometryType.Polygon
        Point = Qgis.WkbType.Point
        MultiPoint = Qgis.WkbType.MultiPoint
        LineString = Qgis.WkbType.LineString
        MultiLineString = Qgis.WkbType.MultiLineString
        Polygon = Qgis.WkbType.Polygon
        MultiPolygon = Qgis.WkbType.MultiPolygon
        GeometryCollection = Qgis.WkbType.GeometryCollection
        Unknown = Qgis.WkbType.Unknown

        @staticmethod
        def flatType(wkb_type):
            return Qgis.WkbType(wkb_type & 0xffff)

        @staticmethod
        def geometryType(wkb_type):
            flat = wkb_type & 0xffff
            if flat in (1, 4, 1001, 1004, 2001, 2004, 3001, 3004):
                return Qgis.GeometryType.Point
            elif flat in (2, 5, 1002, 1005, 2002, 2005, 3002, 3005):
                return Qgis.GeometryType.Line
            elif flat in (3, 6, 1003, 1006, 2003, 2006, 3003, 3006):
                return Qgis.GeometryType.Polygon
            return Qgis.GeometryType.Point

        @staticmethod
        def displayString(wkb_type):
            mapping = {
                1: "Point", 2: "LineString", 3: "Polygon",
                4: "MultiPoint", 5: "MultiLineString", 6: "MultiPolygon",
            }
            return mapping.get(wkb_type & 0xffff, f"Unknown({wkb_type})")

    # Inject into qgis.core so all 'from qgis.core import QgsWkbTypes' works
    _qgis_core.QgsWkbTypes = _QgsWkbTypesCompat
# ── End Compatibility Patch ───────────────────────────────────────


def classFactory(iface):
    """Entry point called by QGIS to create the plugin instance.

    Args:
        iface: QgisInterface instance providing access to the QGIS environment.

    Returns:
        LvtPlugin: The plugin instance.
    """
    from .lvt_plugin import LvtPlugin
    return LvtPlugin(iface)
