/*
 * 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/>.
 */

#ifndef VISION_TEXTRECOGNITION_H
#define VISION_TEXTRECOGNITION_H

#include <kylin-ai/aiengine.h>

#include <atomic>
#include <filesystem>
#include <memory>
#include <string>

#include "concurrentqueue/blockingconcurrentqueue.h"
#include "coreaivisionmodelconfig.h"
#include "corevisionserviceglue.h"
#include "taskmanager/taskrunner.h"

struct PixelPoint {
    int x;
    int y;
};

struct TextLine {
    std::string text;
    std::vector<PixelPoint> points;
    float conf;
};

struct TextRecognitionResult {
    std::string allText;
    std::vector<TextLine> lines;
    int errorCode;
    std::string errorMessage;
};

class TextRecognition : public std::enable_shared_from_this<TextRecognition> {
public:
    using RecognitionResultCallback = std::function<void(const TextRecognitionResult&)>;

    explicit TextRecognition(std::unique_ptr<ai_engine::core_ai::vision::AbstractTextRecognitionEngine> visionEngine);

    ~TextRecognition();

    bool init();

    void destroy() const;

    void recognizeTextFromImageFile(const std::string& imageFilePath, RecognitionResultCallback resultCallback);

    void recognizeTextFromImageData(const std::string& imageData, RecognitionResultCallback resultCallback);

private:
    struct TextRecognitionRequest {
        enum Type { Unknown, ImagePath, Base64Data };
        Type type{Unknown};
        std::filesystem::path imagePath{};
        std::string base64Data{};
        RecognitionResultCallback resultCallback{nullptr};
    };

    TextRecognitionRequest generateTextRecognitionRequestWithFilePath(const std::string& imageFilePath,
                                                                      RecognitionResultCallback resultCallback);

    TextRecognitionRequest generateTextRecognitionRequestWithBase64Data(const std::string& base64Data,
                                                                        RecognitionResultCallback resultCallback);

private:
    std::shared_ptr<TextRecognition> getSharedObject() { return shared_from_this(); }

    TextRecognitionResult makeResultFromEngineError(const ai_engine::EngineError& error) const;

    void callTextRecognitionResultCallback(const TextRecognitionResult& result) const;

    void textRecogniseAsync();

    void textRecognizeTask();

private:
    std::unique_ptr<ai_engine::core_ai::vision::AbstractTextRecognitionEngine> visionEngine_{nullptr};
    moodycamel::BlockingConcurrentQueue<TextRecognitionRequest> textRecognitionRequests_{};
    TextRecognitionRequest currentRequest_{};
    std::atomic<task::TaskId> taskId_{task::INVALID_TASK_ID};
};

#endif