package org.jeecg.module.custom.ocr.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.module.custom.ocr.utils.httputil.HttpClient;
import org.jeecg.module.custom.ocr.utils.httputil.HttpHeader;
import org.jeecg.module.custom.ocr.utils.httputil.HttpParamers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageClassUtil {
    private static Logger logger = LoggerFactory.getLogger(ImageClassUtil.class);

    /**
     * 相似度检查
     * 接口文档
     * https://console-docs.apipost.cn/preview/b85e3fe97510f177/96421d73df6c7f7d
     *
     * @param imgUrl
     * @param baseurl
     * @return
     */
    public static String getSimilarity(String imgUrl, String baseurl, String classifyBaseUrl) {
        JSONObject jsonObjectXs = new JSONObject();
        jsonObjectXs.put("imgUrl", imgUrl);
        jsonObjectXs.put("baseUrl", baseurl);

        HttpParamers paramersXs = HttpParamers.httpPostParamers();
        paramersXs.setJsonParamer(jsonObjectXs.toJSONString());
        HttpHeader headerXs = null;
        String responseDataXs = "";
        try {
            responseDataXs = HttpClient.doService(classifyBaseUrl, paramersXs, headerXs, 15000, 30000);
        } catch (Exception e) {
            logger.error("similarity={}", e);
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(responseDataXs);
        return jsonObject.get("similarity").toString();
    }

    /**
     * 类型检查
     * 接口文档
     * https://console-docs.apipost.cn/preview/7add2088a9a191d5/874020cbfba4287b
     *
     * @param imgurl
     * @param taskId
     * @return {
     * "code": 0,
     * "data": [
     * {
     * "imgs": "/server/data/images/1754716707781476352.jpg",
     * "classId": 704,
     * "scores": 0.11721831560134888,
     * "hash": "0000000000000000110001101111111111100110111011101111110011110000"
     * }
     * ],
     * "taskId": "1"
     * }
     */
    public static JSONObject getClassify(String imgurl, String taskId, String serverurl, String classifyBaseUrl) {
        if (StringUtils.isBlank(serverurl)) {
            serverurl = classifyBaseUrl;
        }

        //类型检查
        JSONObject jsonObjectVi = new JSONObject();
        jsonObjectVi.put("taskId", taskId);
        JSONArray jsonArrayVi = new JSONArray();
        // TODO: 测试环境可能需要调整本地图片地址路径
        jsonArrayVi.add(imgurl);
//        jsonArrayVi.add("/server/ocr/data/images/test01.png");
        logger.info("imgurl={}", imgurl);
        jsonObjectVi.put("imgUrls", jsonArrayVi);

        HttpParamers paramersVi = HttpParamers.httpPostParamers();
        paramersVi.setJsonParamer(jsonObjectVi.toJSONString());
        HttpHeader headerVi = null;
        String responseDataVi = "";
        JSONObject jsonObject = null;

        try {
            responseDataVi = HttpClient.doService(serverurl, paramersVi, headerVi, 15000, 30000);
            JSONObject jsonObjectSimi = JSON.parseObject(responseDataVi);
            if (null != jsonObjectSimi && jsonObjectSimi.getString("code").equals("0") && jsonObjectSimi.get("data") != null && jsonObjectSimi.getJSONArray("data").size() > 0) {
            } else {
            }

            jsonObject = JSON.parseObject(responseDataVi);
        } catch (Exception e) {
            logger.error("classify={}", e);
        }

        return jsonObject;
    }

}
