// =============================================================================
//  protocols/n2k_bridge.h -- NMEA 2000 <-> NMEA 0183 conversion
// =============================================================================
//
//  Outbound (NMEA 2000 → NMEA 0183 for USB-OUT ports):
//    Receives PGNs from the backbone and formats standard/proprietary sentences.
//
//  Inbound (NMEA 0183 → NMEA 2000 for $PARP commands from USB-IN ports):
//    Converts parsed ParpCommand structs into NMEA 2000 PGN 127237.
// =============================================================================

#pragma once

#include "nmea0183_parser.h"
#include <N2kMessages.h>
#include <Stream.h>

namespace arconcentrador::protocols {

/// Initialise the NMEA 2000 stack and register PGN handlers.
void n2k_bridge_init();

/// Process one incoming NMEA 2000 message.
/// Formats the corresponding NMEA 0183 sentence into out_buf.
/// Returns true if a sentence was written.
bool n2k_to_0183(const tN2kMsg& msg, char* out_buf, size_t out_len);

/// Convert a parsed $PARP command into a NMEA 2000 PGN 127237 and send it.
/// Returns true if a PGN was sent.
bool parp_to_n2k(const ParpCommand& cmd, float current_heading_deg);

/// Latest vessel heading received from backbone (degrees, for status broadcasts).
float n2k_latest_heading_deg();

/// Latest autopilot status received from backbone (PGN 127237 from autopilot).
struct ApStatus {
    float   heading_deg;       ///< actual vessel heading
    float   commanded_deg;     ///< heading setpoint
    float   rudder_deg;        ///< rudder angle
    uint8_t mode;              ///< 0=STANDBY 1=HEADING_HOLD 2=TRACK
    bool    valid;
};
ApStatus n2k_ap_status();

}  // namespace arconcentrador::protocols
