"""
odometry_api.py — Odometry interface wrapper
"""
import sys
import os
from Core.env_loader import PROJECT_ROOT

ODOM_AVAILABLE = False
odom = None


def init_odometry(zmq_sock=None) -> bool:
    """Start odometry tracking. Returns True if successful."""
    global ODOM_AVAILABLE, odom

    # marcus_odometry.py lives in Navigation/
    nav_dir = os.path.join(PROJECT_ROOT, "Navigation")
    for d in (nav_dir,):
        if d not in sys.path:
            sys.path.insert(0, d)

    try:
        from marcus_odometry import Odometry
        odom = Odometry()
        ODOM_AVAILABLE = odom.start(zmq_sock=zmq_sock)
        print(f"Odometry {'started' if ODOM_AVAILABLE else 'failed'}")
        return ODOM_AVAILABLE
    except ImportError as e:
        print(f"marcus_odometry.py not found ({e}) — precise distance disabled")
        return False
    except Exception as e:
        print(f"Odometry error: {e} — precise distance disabled")
        return False


def get_position():
    """Return current position dict or None."""
    if odom and ODOM_AVAILABLE:
        return odom.get_position()
    return None
