startNavigation method

Future<void> startNavigation()

//////////// ////////////

Implementation

// II.F - Navigation Methods
///////////////
Future<void> startNavigation() async {
  if (_routeDetails == null) return;

  try {
    debugPrint('[DEBUG] Starting navigation... at ${DateTime.now()}');

    // Note: Wakelock functionality removed due to dependency issues
    debugPrint('[DEBUG] Screen wakelock would be enabled here at ${DateTime.now()}');

    // Initialize controller if needed
    _navigationController ??= NavigationController(
      routeService: _routeService,
      locationService: _locationService,
    );

    // Set the route in the controller
    await _navigationController!.planRoute(_routeDetails!);

    // Ensure we have a current location before proceeding
    if (_currentPosition == null) {
      final position = await _locationService.getCurrentLocation();
      if (position != null) {
        _currentPosition = RaliPosition(position.longitude, position.latitude);
      } else {
        // If we still don't have position, show error and return
        _showError('Cannot start navigation without location');
        return;
      }
    }

    // Important: Update UI state for navigation mode before any camera movement
    _routeOverviewMode = false;
    _isOverTheShoulderMode = true; // Default to OTS view when starting navigation

    if (_currentPosition != null && _cameraController != null) {
      // First, do immediate camera positioning to current location
      try {
        await _mapboxMap?.setCamera(
          mapbox.CameraOptions(
            center: mapbox.Point(
              coordinates: mapbox.Position(_currentPosition!.lng, _currentPosition!.lat)
            ),
            zoom: _otsZoom,
            pitch: _otsPitch,
            bearing: _currentBearing,
          )
        );
      } catch (e) {
        print('Initial camera positioning error: $e');
      }

      // Give a small delay for the map to update
      await Future.delayed(const Duration(milliseconds: 100));
    }

    // Start active navigation only after camera is positioned
    await _navigationController!.startNavigation();
    notifyListeners();

    // Finally, smooth transition to OTS mode with proper bearing
    if (_currentPosition != null && _cameraController != null) {
      await _cameraController!.enterOverTheShoulderMode(_currentPosition!, _currentBearing);
    }
  } catch (e) {
    print('Error starting navigation: $e');
    _showError('Failed to start navigation');
  }
}