# -*- coding: utf-8 -*-
"""
Compatibility shim for QgsWkbTypes across QGIS 3.16 → 4.x.

QGIS 3.30+ deprecated QgsWkbTypes in favor of Qgis.WkbType / Qgis.GeometryType.
QGIS 4.x may REMOVE QgsWkbTypes entirely.

This module provides a unified WkbCompat that works on ALL versions:
    from ..shared.wkb_compat import WkbCompat
    WkbCompat.PointGeometry
    WkbCompat.PolygonGeometry
    WkbCompat.flatType(wkbType)
    WkbCompat.displayString(wkbType)
    WkbCompat.geometryType(wkbType)
"""

try:
    # QGIS 4.x / 3.30+: use Qgis enums
    from qgis.core import Qgis

    # Check if new-style enums exist
    _has_new = hasattr(Qgis, 'GeometryType')

    if _has_new:
        PointGeometry = Qgis.GeometryType.Point
        LineGeometry = Qgis.GeometryType.Line
        PolygonGeometry = Qgis.GeometryType.Polygon

        # WkbType values for feature creation
        try:
            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
        except AttributeError:
            # Fallback to old QgsWkbTypes for wkb values
            from qgis.core import QgsWkbTypes as _Old
            Point = _Old.Point
            MultiPoint = _Old.MultiPoint
            LineString = _Old.LineString
            MultiLineString = _Old.MultiLineString
            Polygon = _Old.Polygon
            MultiPolygon = _Old.MultiPolygon
            GeometryCollection = _Old.GeometryCollection
            Unknown = _Old.Unknown

        def flatType(wkb_type):
            """Get flat (non-Z/M) geometry type."""
            try:
                return Qgis.WkbType(wkb_type & 0xffff)
            except Exception:
                from qgis.core import QgsWkbTypes
                return QgsWkbTypes.flatType(wkb_type)

        def geometryType(wkb_type):
            """Get geometry type (Point/Line/Polygon) from WKB type."""
            try:
                from qgis.core import QgsWkbTypes
                return QgsWkbTypes.geometryType(wkb_type)
            except Exception:
                flat = wkb_type & 0xffff
                if flat in (1, 4, 1001, 1004, 2001, 2004, 3001, 3004):
                    return PointGeometry
                elif flat in (2, 5, 1002, 1005, 2002, 2005, 3002, 3005):
                    return LineGeometry
                elif flat in (3, 6, 1003, 1006, 2003, 2006, 3003, 3006):
                    return PolygonGeometry
                return PointGeometry

        def displayString(wkb_type):
            """Get human-readable geometry type name."""
            try:
                from qgis.core import QgsWkbTypes
                return QgsWkbTypes.displayString(wkb_type)
            except Exception:
                mapping = {
                    1: "Point", 2: "LineString", 3: "Polygon",
                    4: "MultiPoint", 5: "MultiLineString", 6: "MultiPolygon",
                }
                return mapping.get(wkb_type & 0xffff, f"Unknown({wkb_type})")
    else:
        raise ImportError("No Qgis.GeometryType")

except (ImportError, AttributeError):
    # QGIS 3.16 – 3.28: use QgsWkbTypes directly
    from qgis.core import QgsWkbTypes

    PointGeometry = QgsWkbTypes.GeometryType.PointGeometry
    LineGeometry = QgsWkbTypes.GeometryType.LineGeometry
    PolygonGeometry = QgsWkbTypes.GeometryType.PolygonGeometry

    Point = QgsWkbTypes.Type.Point
    MultiPoint = QgsWkbTypes.Type.MultiPoint
    LineString = QgsWkbTypes.Type.LineString
    MultiLineString = QgsWkbTypes.Type.MultiLineString
    Polygon = QgsWkbTypes.Type.Polygon
    MultiPolygon = QgsWkbTypes.Type.MultiPolygon
    GeometryCollection = QgsWkbTypes.Type.GeometryCollection
    Unknown = QgsWkbTypes.Type.Unknown

    flatType = QgsWkbTypes.flatType
    geometryType = QgsWkbTypes.geometryType
    displayString = QgsWkbTypes.displayString
