#ifndef PROTOCOL_H
#define PROTOCOL_H

#include <cJSON.h>
#include <string>
#include <functional>
#include <chrono>

struct BinaryProtocol3 {
    uint8_t type;
    uint8_t reserved;
    uint16_t payload_size;
    uint8_t payload[];
} __attribute__((packed));

enum AbortReason {
    kAbortReasonNone,
    kAbortReasonWakeWordDetected,
    kAbortReasonVoiceInterrupt
    //kAbortReasonNewStory  // websocket推送新故事时中断当前播放
};

enum ListeningMode {
    kListeningModeAutoStop,
    kListeningModeManualStop,
    kListeningModeRealtime // 需要 AEC 支持
};

class Protocol {
public:
    virtual ~Protocol() = default;

    inline int server_sample_rate() const {
        return server_sample_rate_;
    }
    inline int server_frame_duration() const {
        return server_frame_duration_;
    }
    inline bool downlink_is_pcm() const {
        return downlink_is_pcm_;
    }
    inline const std::string& session_id() const {
        return session_id_;
    }
    inline void SetSuppressIncomingMessageLog(bool v) { suppress_incoming_message_log_ = v; }

    void OnIncomingAudio(std::function<void(std::vector<uint8_t>&& data)> callback);
    void OnIncomingJson(std::function<void(const cJSON* root)> callback);
    void OnAudioChannelOpened(std::function<void()> callback);
    void OnAudioChannelClosed(std::function<void()> callback);
    void OnNetworkError(std::function<void(const std::string& message)> callback);
    void OnBotMessage(std::function<void()> callback);

    virtual void Start() = 0;
    virtual bool OpenAudioChannel() = 0;
    virtual void CloseAudioChannel() = 0;
    // RTC 软退出：默认兼容旧协议，RTC 协议可重写为仅退房不销毁 engine。
    virtual void LeaveRoom(bool notify_closed = true) { (void)notify_closed; CloseAudioChannel(); }
    virtual bool IsAudioChannelOpened() const = 0;
    virtual void SendAudio(const std::vector<uint8_t>& data) = 0;
    virtual void SendPcm(const std::vector<uint8_t>& data) {}
    virtual void SendG711A(const std::vector<uint8_t>& data) {}
    virtual void SendWakeWordDetected(const std::string& wake_word);
    virtual void SendStartListening(ListeningMode mode);
    virtual void SendStopListening();
    virtual void SendAbortSpeaking(AbortReason reason);
    virtual void SendTextMessage(const std::string& text);
    virtual void SendStoryRequest();    // 声明 发送讲故事请求 【新增】
    virtual void SendIotDescriptors(const std::string& descriptors);
    virtual void SendIotStates(const std::string& states);
    virtual void SendFunctionResult(const std::string& tool_call_id, const std::string& content) {
        (void)tool_call_id;
        SendTextMessage(content);
    }

protected:
    std::function<void(const cJSON* root)> on_incoming_json_;
    std::function<void(std::vector<uint8_t>&& data)> on_incoming_audio_;
    std::function<void()> on_audio_channel_opened_;
    std::function<void()> on_audio_channel_closed_;
    std::function<void(const std::string& message)> on_network_error_;
    std::function<void()> on_bot_message_;

    int server_sample_rate_ = 24000;
    int server_frame_duration_ = 60;
    bool downlink_is_pcm_ = false;// 是否是PCM格式
    bool error_occurred_ = false;
    std::string session_id_;
    bool start_listening_pending_ = false;// 是否有待处理的监听请求
    ListeningMode pending_listening_mode_ = kListeningModeRealtime;// 待处理的监听模式
    std::chrono::time_point<std::chrono::steady_clock> last_incoming_time_;
    bool suppress_incoming_message_log_ = false;

    virtual void SendText(const std::string& text) = 0;
    virtual void SetError(const std::string& message);
    virtual bool IsTimeout() const;
};

#endif // PROTOCOL_H
