"""Screen capture engine abstraction layer.

Engines with native/platform-specific dependencies are imported inside
try/except blocks so the package loads cleanly on any platform (including
Android via Chaquopy where desktop capture libraries are unavailable).
"""

from ledgrab.core.capture_engines.base import (
    CaptureEngine,
    CaptureStream,
    DisplayInfo,
    ScreenCapture,
)
from ledgrab.core.capture_engines.factory import EngineRegistry

# ── Desktop capture engines (platform-specific native deps) ──────────

try:
    from ledgrab.core.capture_engines.mss_engine import MSSEngine, MSSCaptureStream

    _has_mss = True
except ImportError:
    _has_mss = False

try:
    from ledgrab.core.capture_engines.dxcam_engine import DXcamEngine, DXcamCaptureStream

    _has_dxcam = True
except ImportError:
    _has_dxcam = False

try:
    from ledgrab.core.capture_engines.bettercam_engine import (
        BetterCamEngine,
        BetterCamCaptureStream,
    )

    _has_bettercam = True
except ImportError:
    _has_bettercam = False

try:
    from ledgrab.core.capture_engines.wgc_engine import WGCEngine, WGCCaptureStream

    _has_wgc = True
except ImportError:
    _has_wgc = False

# ── ADB-based Android capture ───────────────────────────────────────

try:
    from ledgrab.core.capture_engines.scrcpy_engine import ScrcpyEngine, ScrcpyCaptureStream

    _has_scrcpy = True
except ImportError:
    _has_scrcpy = False

try:
    from ledgrab.core.capture_engines.scrcpy_client_engine import (
        ScrcpyClientEngine,
        ScrcpyClientCaptureStream,
    )

    _has_scrcpy_client = True
except ImportError:
    _has_scrcpy_client = False

# ── Camera (OpenCV) ─────────────────────────────────────────────────

try:
    from ledgrab.core.capture_engines.camera_engine import CameraEngine, CameraCaptureStream

    _has_camera = True
except ImportError:
    _has_camera = False

# ── Android MediaProjection (Chaquopy bridge) ───────────────────────

try:
    from ledgrab.core.capture_engines.mediaprojection_engine import (
        MediaProjectionEngine,
        MediaProjectionCaptureStream,
    )

    _has_mediaprojection = True
except ImportError:
    _has_mediaprojection = False

# ── Android root screenrecord (rooted Magisk devices) ───────────────

try:
    from ledgrab.core.capture_engines.root_screenrecord_engine import (
        RootScreenrecordEngine,
        RootScreenrecordCaptureStream,
    )

    _has_root_screenrecord = True
except ImportError:
    _has_root_screenrecord = False

# ── Demo / always available ─────────────────────────────────────────

from ledgrab.core.capture_engines.demo_engine import DemoCaptureEngine, DemoCaptureStream

# ── Auto-register available engines ─────────────────────────────────

if _has_mss:
    EngineRegistry.register(MSSEngine)
if _has_dxcam:
    EngineRegistry.register(DXcamEngine)
if _has_bettercam:
    EngineRegistry.register(BetterCamEngine)
if _has_wgc:
    EngineRegistry.register(WGCEngine)
if _has_scrcpy:
    EngineRegistry.register(ScrcpyEngine)
if _has_scrcpy_client:
    EngineRegistry.register(ScrcpyClientEngine)
if _has_camera:
    EngineRegistry.register(CameraEngine)
if _has_mediaprojection:
    EngineRegistry.register(MediaProjectionEngine)
if _has_root_screenrecord:
    EngineRegistry.register(RootScreenrecordEngine)
EngineRegistry.register(DemoCaptureEngine)

# ── Public API ──────────────────────────────────────────────────────

__all__ = [
    "CaptureEngine",
    "CaptureStream",
    "DisplayInfo",
    "ScreenCapture",
    "EngineRegistry",
    "DemoCaptureEngine",
    "DemoCaptureStream",
]

if _has_mss:
    __all__ += ["MSSEngine", "MSSCaptureStream"]
if _has_dxcam:
    __all__ += ["DXcamEngine", "DXcamCaptureStream"]
if _has_bettercam:
    __all__ += ["BetterCamEngine", "BetterCamCaptureStream"]
if _has_wgc:
    __all__ += ["WGCEngine", "WGCCaptureStream"]
if _has_scrcpy:
    __all__ += ["ScrcpyEngine", "ScrcpyCaptureStream"]
if _has_scrcpy_client:
    __all__ += ["ScrcpyClientEngine", "ScrcpyClientCaptureStream"]
if _has_camera:
    __all__ += ["CameraEngine", "CameraCaptureStream"]
if _has_mediaprojection:
    __all__ += ["MediaProjectionEngine", "MediaProjectionCaptureStream"]
if _has_root_screenrecord:
    __all__ += ["RootScreenrecordEngine", "RootScreenrecordCaptureStream"]
