#pragma once
#include "AiDemoTypes.h"
#include "AiHttpClient.h"
#include "AiWebSocketClient.h"
#include <condition_variable>
#include <deque>
#include <functional>
#include <mutex>
#include <thread>
#include <atomic>

// Demo AI input port: SDK video callback -> frame copy -> JPEG -> local AI service.
// This class is independent from the SDK DLL, so traditional SDK users are not affected.
class AiInputPort
{
public:
    typedef std::function<void(const AiDemoResult&)> ResultCallback;

    AiInputPort();
    ~AiInputPort();

    void SetResultCallback(ResultCallback cb);
    void Start(const AiInputConfig& cfg);
    void Stop();
    bool IsRunning() const;

    // Called from SDK video callback. This function must stay non-blocking.
    void PushFrame(const HWS_VIDEO_FRAME* frame);

private:
    void WorkerThread();
    bool ShouldAcceptFrame(unsigned long long nowMs);

private:
    mutable std::mutex lock_;
    std::condition_variable cv_;
    std::deque<AiCapturedFrame> queue_;
    std::thread worker_;
    std::atomic<bool> running_;
    std::atomic<bool> stopping_;
    AiInputConfig config_;
    ResultCallback resultCb_;
    unsigned long long lastAcceptTickMs_;
};
