/*
 * 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 <iostream>
#include <filesystem>
#include <opencv2/opencv.hpp>
#include <triton/client/http_client.h>
#include <dirent.h>
#include <fstream>
#include <chrono>
#include "base64_transformer.h"
#include <kylin-ai/ai-engine/textrecognitionengine.h>
#include <kylin-ai/ai-engine/aiengine.h>

namespace tc = triton::client;

namespace vision = ai_engine::core_ai::vision;
#define FAIL_IF_ERR(X, MSG)                                        \
  {                                                                \
    tc::Error err = (X);                                           \
    if (!err.IsOk()) {                                             \
      std::cerr << "error: " << (MSG) << ": " << err << std::endl; \
      exit(1);                                                     \
    }                                                              \
  }

class OcrTritonClient {
public:
    OcrTritonClient(const std::string& url);
    vision::TextRecognitionResult OcrRequestByPath(std::string ImagePath);
    vision::TextRecognitionResult OcrRequestByImage(cv::Mat Image);
    vision::TextRecognitionResult OcrRequestByBase64(const char *ImageData);
    tc::Error LoadModel();
    tc::Error RemoveModel();
    std::string GetConfigValue(const char* key, const char* engine);

private:
    std::unique_ptr<tc::InferenceServerHttpClient> client_;  // Triton client as a member variable
    std::string model_name_;
};
