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

#include <kylin-ai/ai-engine/aiengine.h>
#include "docimgrec_triton_client/client.h"
#include "ondevicevisioncapabilityengine.h"

namespace ai_engine::core_ai::vision {

void OndeviceVisionImageProcessEngine::setDocumentImageRectificationCallback(
    ImageResultCallback callback) {
    ddcpResultCallback_ = std::move(callback);

    if (ddcpClient_ == nullptr) {
        return;
    }
}

bool OndeviceVisionImageProcessEngine::DocumentImageRectificationRequestPath(
    const std::string &ImagePath, EngineError &error) {
    std::lock_guard<std::mutex> lock(DocimgrecMutex_);
    ImgBase64Result res = ddcpClient_->DdcpRequestByPath(ImagePath);
    if (res.errorCode != -1) {
        error = ai_engine::EngineError(
            (int)ai_engine::VisionEngineErrorCode::ImageFileReadError,
            "[DdcpTriton]: failed to Request Ddcp by img path.");

        return false;
    }
    ImageResult result;
    result.success = true;
    result.imageFormat = res.format;
    result.imageBase64Data = res.base64Result;
    result.modelName = modelName_;
    ddcpResultCallback_(result);

    return true;
}

bool OndeviceVisionImageProcessEngine::DocumentImageRectificationRequestBase64(
    const char *ImageData, EngineError &error) {
    std::lock_guard<std::mutex> lock(DocimgrecMutex_);
    ImgBase64Result res = ddcpClient_->DdcpRequestByBase64(ImageData);
    if (res.errorCode != -1) {
        error = ai_engine::EngineError(
            (int)ai_engine::VisionEngineErrorCode::ImageFileReadError,
            "[DdcpTriton]: failed to Request Ddcp by base64.");

        return false;
    }
    ImageResult result;
    result.success = true;
    result.imageFormat = res.format;
    result.imageBase64Data = res.base64Result;
    result.modelName = modelName_;
    ddcpResultCallback_(result);
    return true;
}

bool OndeviceVisionImageProcessEngine::initDocumentImageRectificationModule(
    EngineError &error) {
#ifdef TRITON_CLIENT
    tc::Error err = ddcpClient_->LoadModel();
    int errorCode = 0;

    if (err.Message() ==
        "explicit model load / unload is not allowed if polling is enabled")
        errorCode = (int)ai_engine::VisionEngineErrorCode::ModelEarlyLoaded;
    else if (err.Message() == "HTTP client failed: Couldn't connect to server")
        errorCode = (int)ai_engine::VisionEngineErrorCode::FileNotFound;
    if (!err.IsOk()) {
        error = ai_engine::EngineError(
            (int)ai_engine::VisionEngineErrorCode::ImageFileReadError,
            err.Message().c_str());

        return false;
    }
#endif
    return true;
}

bool OndeviceVisionImageProcessEngine::destroyDocumentImageRectificationModule(
    EngineError &error) {
#ifdef TRITON_CLIENT
    tc::Error err = ddcpClient_->RemoveModel();

    if (!err.IsOk()) {
        error = ai_engine::EngineError(
            (int)ai_engine::VisionEngineErrorCode::ImageFileReadError,
            err.Message().c_str());

        return false;
    }
#endif
    return true;
}

} // namespace ai_engine::core_ai::vision
