package com.bonus.core.customer.v4.megvii;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bonus.core.customer.constants.MegviiResponseCodeEnum;
import com.bonus.core.customer.v4.dto.FaceRegisterDTO;
import com.bonus.core.customer.vo.MultiModelFaceVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MegviiUtilV4 {
   private static final Logger log = LoggerFactory.getLogger(MegviiUtilV4.class);
   private static final String FACE_API = "%s/api/getfacefeature?userid=%s&faceVer=%s&type=%s";

   private MegviiUtilV4() {
   }

   public static MultiModelFaceVO generateFaceFeature(FaceRegisterDTO faceRegisterDTO) {
      MultiModelFaceVO modelFaceVO;
      if (faceRegisterDTO.ifRequestToDevice()) {
         modelFaceVO = generateFaceFeatureToDevice(faceRegisterDTO);
      } else {
         modelFaceVO = generateFaceFeatureToSoftware(faceRegisterDTO);
      }

      return modelFaceVO;
   }

   public static MultiModelFaceVO generateFaceFeatureToDevice(FaceRegisterDTO faceRegisterDTO) {
      MultiModelFaceVO modelFaceVO = new MultiModelFaceVO();

      try {
         modelFaceVO.setFaceVer(faceRegisterDTO.getFaceVer());
         String faceDeviceUrl = getFaceUrl(faceRegisterDTO);
         log.info("[设备旷视]请求地址：{}", faceDeviceUrl);
         String response = HttpUtil.createPost(faceDeviceUrl).timeout(10000).body(faceRegisterDTO.getImageBase64(), "text/plain").execute().body();
         log.info("[设备旷视]响应结果：{}", response);
         JSONObject resObj = JSON.parseObject(response);
         Integer code = resObj.getInteger("code");
         if (10000 == code) {
            String feature = resObj.getString("data");
            modelFaceVO.setFeature(feature);
            modelFaceVO.setSuccess(true);
            modelFaceVO.setMsg("[设备]成功");
         } else {
            modelFaceVO.setSuccess(false);
            modelFaceVO.setMsg("[设备失败]" + resObj.getString("msg"));
         }
      } catch (Exception var7) {
         log.error("[设备旷视]异常{}", var7.getMessage(), var7);
         modelFaceVO.setSuccess(false);
         modelFaceVO.setMsg("[设备]特征值抽取异常");
         if (var7 instanceof IORuntimeException) {
            modelFaceVO.setMsg("[设备]接口超时");
         }
      }

      return modelFaceVO;
   }

   public static MultiModelFaceVO generateFaceFeatureToSoftware(FaceRegisterDTO faceRegisterDTO) {
      MultiModelFaceVO modelFaceVO = new MultiModelFaceVO();
      modelFaceVO.setFaceVer(faceRegisterDTO.getFaceVer());

      try {
         String faceUrl = getFaceUrl(faceRegisterDTO);
         log.info("[软件旷视]请求地址：{}", faceUrl);
         String responseStr = HttpUtil.createPost(faceUrl).timeout(10000).body(faceRegisterDTO.getImageBase64(), "text/plain").execute().body();
         log.info("[旷视多模型]响应结果：{}", responseStr);
         JSONObject resObj = JSON.parseObject(responseStr);
         Integer code = resObj.getInteger("code");
         String errorMsg;
         if (10000 == code) {
            errorMsg = resObj.getString("data");
            modelFaceVO.setFeature(errorMsg);
            modelFaceVO.setSuccess(true);
            modelFaceVO.setMsg("[软件]成功");
         } else {
            errorMsg = resObj.getString("msg");
            if (ObjectUtil.isEmpty(errorMsg)) {
               errorMsg = MegviiResponseCodeEnum.getDesc(code);
            }

            modelFaceVO.setSuccess(false);
            modelFaceVO.setMsg("[软件失败:]" + errorMsg);
         }
      } catch (Exception var8) {
         log.error("[旷视多模型]特征值抽取异常:{}", var8.getMessage(), var8);
         modelFaceVO.setSuccess(false);
         modelFaceVO.setMsg("[软件]特征值抽取异常");

         try {
            Thread.sleep(10000L);
         } catch (Exception var7) {
            Thread.currentThread().interrupt();
            log.error("[旷视多模型]休眠异常:{}", var7.getMessage(), var7);
         }

         if (var8 instanceof IORuntimeException) {
            modelFaceVO.setMsg("[软件]接口超时");
         }
      }

      return modelFaceVO;
   }

   public static String getFaceUrl(FaceRegisterDTO faceRegisterDTO) {
      String ipAddr = faceRegisterDTO.getIpFaceAddr();
      if (!ipAddr.startsWith("http://")) {
         ipAddr = "http://" + ipAddr;
      }

      return String.format("%s/api/getfacefeature?userid=%s&faceVer=%s&type=%s", ipAddr, faceRegisterDTO.getCustId(), faceRegisterDTO.getFaceVer(), faceRegisterDTO.getServerType());
   }
}
