/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include <chrono>
#include <condition_variable>
#include <atomic>
#include <mutex>
#include <thread>
#include <vector>
#include <string>
#include <functional>

#include <kylin-ai/ai-engine/imageprocessengine.h>
#include <kylin-ai/ai-engine/textrecognitionengine.h>

class OcrTritonClient;
class MattingTritonClient;
class DdcpTritonClient;
class BinaryImage;

namespace ai_engine::core_ai::vision {

class OndeviceVisionImageProcessEngine : public AbstractImageProcessEngine {

public:
    OndeviceVisionImageProcessEngine();
    virtual ~OndeviceVisionImageProcessEngine();
    bool isCloud() const override { return false; };
    bool isBuiltInEngine() const override { return true; };

    // 抠图模块
    result::AiEngineBaseResult initMattingsModule() override;
    result::AiEngineBaseResult destroyMattingsModule() override;
    void setMattingResultCallback(ImageResultCallback callback) override;
    result::AiEngineBaseResult mattingFromBase64ImageData(
        const std::string &imageBase64Data) override;
    result::AiEngineBaseResult mattingFromImageFile(
        const std::string &imageFilePath) override;
    void stopMattingProcess() override;
private:
    std::atomic<bool> MattingRunning_;

public:
    // 图像二值化模块
    result::AiEngineBaseResult initImageBinaryModule() override;
    result::AiEngineBaseResult destroyImageBinaryModule() override;
    void setImageBinaryResultCallback(ImageResultCallback callback) override;
    result::AiEngineBaseResult binarizeImageFromBase64ImageData(
        const std::string &imageBase64Data) override;
    result::AiEngineBaseResult binarizeImageFromImageFile(
        const std::string &imageFilePath) override;
    void stopImageBinarization() override;
private:
    std::atomic<bool> binarizeImageRunning_;

public:
    // 文档校准模块
    bool initDocumentImageRectificationModule(EngineError &error);
    bool destroyDocumentImageRectificationModule(EngineError &error);
    void setDocumentImageRectificationCallback(ImageResultCallback callback);
    bool DocumentImageRectificationRequestPath(const std::string &ImagePath, EngineError &error);
    bool DocumentImageRectificationRequestBase64(const char *ImageData, EngineError &error);

    std::string engineName() const override { return "ondevice"; };
    std::string currentModel() const override { return modelName_; };
    std::string currentModelName() const override { return modelName_; };

    std::string modelInfo() const override;
private:
    std::string modelName_ = "端侧视觉模型";
    DdcpTritonClient *ddcpClient_ = nullptr;
    MattingTritonClient *mattingClient_ = nullptr;
    BinaryImage *binImage_ = nullptr;
    std::mutex MattingMutex_;  // 用于保护并发访问的互斥锁
    std::mutex DocimgrecMutex_;  // 用于保护并发访问的互斥锁
    std::mutex binImageMutex_;  // 用于保护并发访问的互斥锁
    ImageResultCallback ddcpResultCallback_ = nullptr;
    ImageResultCallback mattingResultCallback_ = nullptr;
    ImageResultCallback imageBinaryzationResultCallback_ = nullptr;
};

class OndeviceVisionTextRecognitionEngine : public AbstractTextRecognitionEngine {
public:
    OndeviceVisionTextRecognitionEngine();
    virtual ~OndeviceVisionTextRecognitionEngine();
    bool isCloud() const override { return false; };
    bool isBuiltInEngine() const override { return true; };

    void setTextRecognitionResultCallback(
        TextRecognitionResultCallback callback) override;

    result::AiEngineBaseResult init() override;
    result::AiEngineBaseResult destroy() override;

    // 从图片文件中识别文本
    result::AiEngineBaseResult recognizeTextFromFile(
        const std::string &imageFilePath) override;

    // 从图片数据中识别文本
    result::AiEngineBaseResult recognizeTextFromBase64ImageData(
        const std::string &imageBase64Data) override;

    std::string engineName() const override { return "ondevice"; };
    std::string currentModel() const override { return modelName_; };
    std::string currentModelName() const override { return modelName_; };

    std::string modelInfo() const override;
private:
    std::string modelName_ = "端侧视觉模型";
    OcrTritonClient *ocrClient_ = nullptr;
    std::mutex ocrMutex_;  // 用于保护并发访问的互斥锁
    TextRecognitionResultCallback ocrResultCallback_ = nullptr;
};

} // namespace ai_engine::vision

