#pragma once

#include <thread>

#include "error_code/error_code.hpp"
#include "ceres_detect/detect_wheel_ceres.h"
#include "proto/enum_type.pb.h"
#include "proto/detect_message.pb.h"
#include "thread/thread_condition.h"
#include "vzense/device_tof3d.h"

#ifdef ENABLE_TENSORRT_DETECT
#include "detect/tensorrt_detect/wheel-detector.h"
#else
#include "detect/onnx_detect/wheel-detector.h"
#endif
#include "detect/ceres_detect/car_pose_detector.h"
#include "detect/ceres_detect/wheel_detector.h"
#include "detect/ceres_detect/detect_wheel_ceres.h"
#include "communication/transitData.h"

class DetectManager {
public:
    struct DetectResult {
        WheelCeresDetector::WheelDetectResult wheel[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
        Car_pose_detector::CarDetectResult car;

        void reset() {
            for (int i = 0; i < DeviceAzimuth::DEVICE_AZIMUTH_MAX; i++) {
                wheel[i].reset();
            }
            car.reset();
        }

        std::string info() {
            std::string str = car.info();
            for (int i = 0; i < DeviceAzimuth::DEVICE_AZIMUTH_MAX; i++) {
                str += wheel[i].info();
            }
            return str;
        }
    };

public:
    static DetectManager *iter() {
        static DetectManager *instance = nullptr;
        if (instance == nullptr) {
            instance = new DetectManager();
        }
        return instance;
    }

    ~DetectManager() = default;

    Error_manager Init();

    MeasureStatu getMeasureResult(DetectResult &result);

    Error_manager carMoveStatus();
protected:
    void run();

    // 车轮点云优化
    Error_manager WheelCloudOptimize(std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> &vct_wheel_cloud, DetectResult &result);

private:
    std::thread *m_thread;
    Thread_condition m_condit;

    TimedLockData<bool> move_statu;
    TimedLockData<MeasureStatu> detect_statu;
    TensorrtWheelDetector *detector;
    TimedLockData<DetectResult> m_detect_result_time_lock_data;
};


