#pragma once

#include "RTOCRTypes.h"
#include "RTOCRDetector.h"
#include "RTOCRClassifier.h"
#include "RTOCRRecognizer.h"
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "ANSLicense.h"
namespace ANSCENTER {
namespace rtocr {

class PaddleOCRV5RTEngine {
public:
    PaddleOCRV5RTEngine() = default;
    ~PaddleOCRV5RTEngine() = default;
    PaddleOCRV5RTEngine(const PaddleOCRV5RTEngine&) = delete;
    PaddleOCRV5RTEngine& operator=(const PaddleOCRV5RTEngine&) = delete;

    // Initialize all components
    // clsModelPath can be empty to skip classifier
    bool Initialize(const std::string& detModelPath,
                    const std::string& clsModelPath,
                    const std::string& recModelPath,
                    const std::string& dictPath,
                    int gpuId = 0,
                    const std::string& engineCacheDir = "");

    // Run full OCR pipeline: detect → crop → [classify →] recognize
    std::vector<OCRPredictResult> ocr(const cv::Mat& image);

    // Configuration setters
    void SetDetMaxSideLen(int v) { detMaxSideLen_ = v; }
    void SetDetDbThresh(float v) { detDbThresh_ = v; }
    void SetDetBoxThresh(float v) { detBoxThresh_ = v; }
    void SetDetUnclipRatio(float v) { detUnclipRatio_ = v; }
    void SetClsThresh(float v) { clsThresh_ = v; }
    void SetUseDilation(bool v) { useDilation_ = v; }
    void SetRecImageHeight(int v) { recImgH_ = v; }
    void SetRecImageMaxWidth(int v) { recImgMaxW_ = v; }
    void SetGpuId(int v) { gpuId_ = v; }
    void SetEngineCacheDir(const std::string& v) { engineCacheDir_ = v; }

private:
    std::unique_ptr<RTOCRDetector>    detector_;
    std::unique_ptr<RTOCRClassifier>  classifier_;   // optional
    std::unique_ptr<RTOCRRecognizer>  recognizer_;

    // Configuration
    int   detMaxSideLen_  = kDetMaxSideLen;
    float detDbThresh_    = kDetDbThresh;
    float detBoxThresh_   = kDetBoxThresh;
    float detUnclipRatio_ = kDetUnclipRatio;
    float clsThresh_      = kClsThresh;
    bool  useDilation_    = false;
    int   recImgH_        = kRecImgH;
    int   recImgMaxW_     = kRecImgMaxW;
    int   gpuId_          = 0;
    std::string engineCacheDir_;

    std::recursive_mutex _mutex;
};

} // namespace rtocr
} // namespace ANSCENTER
