/*
 * 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 <dirent.h>
#include <kylin-ai/ai-engine/aiengine.h>
#ifdef TRITON_CLIENT
#include <triton/client/http_client.h>
#endif

#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>

#include "base64_transformer.h"
#include "common.h"

#ifdef TRITON_CLIENT
namespace tc = triton::client;

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

// using namespace std;
// using namespace Ort;
// using namespace cv;

class Document_Dewarp {
public:
    Document_Dewarp() {};
#ifdef TRITON_CLIENT
    ImgBase64Result
    detect(cv::Mat &m_srcImg,
           const std::unique_ptr<triton::client::InferenceServerHttpClient>
               &client,
           const std::string &model_name_, std::string imgFormat);
#endif

private:
    int inputWidth = 992;
    int inputHeight = 992;
    cv::Mat preprocess(cv::Mat &m_srcImg);

    std::vector<float> input_image_;
    std::vector<std::string> input_names;
    std::vector<std::string> output_names;

    int col_gap = 0;
    int row_gap = 0;
    int fiducial_point_gaps[8] = {1, 2, 3, 5, 6, 10, 15, 30};
    int fiducial_point_num[8] = {31, 16, 11, 7, 6, 4, 3, 2};

    cv::Mat flatByRegressWithClassiy_multiProcess(
        std::vector<int> &vec_segment, std::array<int, 2> &segment_shape,
        std::vector<float> &vec_regress, std::array<int, 4> &regress_shape,
        cv::Mat &srcImg);
    cv::Mat linearInterpolation(const cv::Mat &tshape, const cv::Mat &sshape,
                                const cv::Mat &grid_x, const cv::Mat &grid_y);
    void mesgrid(int xstart, int xend, int xpointnum, int ystart, int yend,
                 int ypointnum, cv::Mat &x, cv::Mat &y);
};

class DdcpTritonClient {
public:
    DdcpTritonClient(const std::string &url);
    ImgBase64Result DdcpRequestByPath(std::string ImagePath);
    ImgBase64Result DdcpRequestByImage(cv::Mat Image, std::string imgFormat);
    ImgBase64Result DdcpRequestByBase64(const std::string &imageData);

#ifdef TRITON_CLIENT
    tc::Error LoadModel();
    tc::Error RemoveModel();
#endif
    std::string GetConfigValue(const char *key, const char *engine);
    Document_Dewarp UNet_;

private:
    int inputWidth = 992;
    int inputHeight = 992;
    std::string model_name_;

#ifdef TRITON_CLIENT
    std::unique_ptr<tc::InferenceServerHttpClient>
        client_; // Triton client as a member variable
#endif
};
