#pragma once

#include <QString>
#include <QMap>

namespace AetherSDR {

class RadioModel;
class SliceModel;

// Pure protocol handler for Hamlib rigctld emulation.
// No I/O — receives a text line, returns the response string.
// Shared by both the TCP server and the PTY virtual serial port.
class RigctlProtocol {
public:
    explicit RigctlProtocol(RadioModel* model);

    // Process one command line (may contain ';' or '|'-separated batch commands).
    // '|' separator enables extended responses joined by '|' (rigctld pipe mode).
    // Returns the complete response string to send back to the client.
    QString handleLine(const QString& line);

    // Which slice this protocol instance controls.
    void setSliceIndex(int idx) { m_sliceIndex = idx; }
    int  sliceIndex() const     { return m_sliceIndex; }

    // Extended response mode (prepend '+' to enable).
    void setExtendedMode(bool on) { m_extended = on; }
    bool extendedMode() const     { return m_extended; }

private:
    // Process a single command (short or long form).
    // Returns the response string.
    QString processCommand(const QString& cmd);

    // Individual command handlers
    QString cmdGetFreq();
    QString cmdSetFreq(const QString& arg);
    QString cmdGetMode();
    QString cmdSetMode(const QString& args);
    QString cmdGetVfo();
    QString cmdSetVfo(const QString& arg);
    QString cmdGetPtt();
    QString cmdSetPtt(const QString& arg);
    QString cmdGetInfo();
    QString cmdGetSplitVfo();
    QString cmdSetSplitVfo(const QString& args);
    QString cmdGetSplitFreq();
    QString cmdSetSplitFreq(const QString& args);
    QString cmdGetSplitMode();
    QString cmdSetSplitMode(const QString& args);
    QString cmdGetLevel(const QString& arg);
    QString cmdSetLevel(const QString& args);
    QString cmdDumpState();
    QString cmdSendMorse(const QString& text);  // b <text> / \send_morse
    QString cmdStopMorse();                     // \stop_morse
    QString cmdSetKeySpeed(const QString& arg); // \set_level KEYSPD <wpm>

    // Helpers
    SliceModel* currentSlice() const;
    SliceModel* findTxSlice() const;
    QString rprt(int code) const;

    // Mode conversion tables
    static QString smartsdrToHamlib(const QString& mode);
    static QString hamlibToSmartSDR(const QString& mode);
    static int     hamlibModeFlag(const QString& mode);

    RadioModel* m_model;
    int  m_sliceIndex{0};
    bool m_extended{false};
    // Set when a bare `b` / `\send_morse` arrives without inline text.
    // The next line is consumed verbatim as the morse text. Hamlib spec
    // allows this two-line form and Not1MM contest CW relies on it.
    bool m_pendingMorseLine{false};
};

} // namespace AetherSDR
