#ifndef GRABBAGPRESENTER_H
#define GRABBAGPRESENTER_H

#include "IVrConfig.h"
#include "IVrEyeDevice.h"
#include "IYGrabBagStatus.h"
#include "RobotProtocol.h"
#include "VZNL_Types.h"

class GrabBagPresenter
{
public:
    GrabBagPresenter();
    ~GrabBagPresenter();

    // 初始化
    int Init();

    // 设置状态回调
    void SetStatusCallback(IYGrabBagStatus* status);
    
    // 开始检测
    int StartDetection();

    // 停止检测
    int StopDetection();

    IVrEyeDevice* GetEyeDevice(int index);

private:
    // 机械臂协议相关方法
    int InitRobotProtocol();
    
    // 机械臂协议回调函数
    void OnRobotConnectionChanged(bool connected);
    void OnRobotWorkSignal(bool startWork);
    
    // 连接状态检查和更新
    void CheckAndUpdateWorkStatus();
    
    // 静态回调函数，用于传递给SDK
    static void _StaticCameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);
    
    // 实例方法，处理回调
    void _CameraNotify(EVzDeviceWorkStatus eStatus, void* pExtData, unsigned int nDataLength, void* pInfoParam);

    // 检测数据回调函数
    static void _StaticDetectionCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint, void* pParam);
    void _DetectionCallback(EVzResultDataType eDataType, SVzLaserLineData* pLaserLinePoint);

    // 算法检测线程
    void _AlgoDetectThread();

    int _DetectTask();

private:
    IVrConfig*                      m_vrConfig = nullptr;
    std::vector<IVrEyeDevice*>      m_vrEyeDeviceList;
    IYGrabBagStatus*                m_pStatus = nullptr;

    RobotProtocol*                  m_pRobotProtocol = nullptr;
    
    // 连接状态标志
    bool                            m_bCameraConnected = false;       // 相机连接状态
    bool                            m_bRobotConnected = false;        // 机械臂连接状态
    WorkStatus                      m_currentWorkStatus = WorkStatus::Error;  // 当前工作状态

    std::atomic<bool>               m_bAlgoDetectThreadRunning = false;
    std::mutex                      m_algoDetectMutex;
    std::condition_variable         m_algoDetectCondition;
    
    // 检测数据缓存
    std::vector<SVzLaserLineData*> m_detectionDataCache;
    std::mutex                      m_detectionDataMutex;
};

#endif // GRABBAGPRESENTER_H
