package com.bonus.core.customer.utils;

import cn.hutool.core.lang.Dict;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bonus.core.customer.face.utils.FacePersonDto;
import com.bonus.core.customer.model.CustInfo;
import com.bonus.core.customer.vo.CustInfoAndCarSeriesNumVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UfaceUtil {
   private static final Logger log = LoggerFactory.getLogger(UfaceUtil.class);

   public static JSONObject createCust(CustInfo custInfo, String pwd, String serverUrl) {
      String createPersonUrl = "http://" + serverUrl + "/person/create";
      FacePersonDto facePersonDto = new FacePersonDto(new CustInfoAndCarSeriesNumVo(custInfo));
      Dict params = Dict.create().set("pass", pwd).set("person", JSON.toJSONString(facePersonDto));
      log.info("闸机创建人员请求参数：{}", params);
      String res = HttpUtil.post(createPersonUrl, params);
      log.info("闸机创建人员响应结果：{}", res);
      return JSONObject.parseObject(res);
   }

   public static JSONObject createFace(String pwd, String serverUrl, Long custId, String imageBase64) {
      Dict createParams = Dict.create().set("pass", pwd).set("personId", custId).set("faceId", custId).set("isEasyWay", false).set("imgBase64", imageBase64);
      String createFaceUrl = "http://" + serverUrl + "/face/create";
      log.info("闸机创建人脸请求参数：{}", createParams);
      String createRes = HttpUtil.post(createFaceUrl, createParams, 12000);
      log.info("闸机创建人脸响应结果：{}", createRes);
      return JSONObject.parseObject(createRes);
   }

   public static JSONObject delFace(String pwd, Long custId, String serverUrl) {
      Dict deleteParams = Dict.create().set("pass", pwd).set("faceId", custId);
      String deleteUrl = "http://" + serverUrl + "/face/delete";
      log.info("闸机删除人脸请求参数：{}", deleteParams);
      String deleteRes = HttpUtil.post(deleteUrl, deleteParams, 12000);
      log.info("闸机删除人脸响应结果：{}", deleteRes);
      return JSONObject.parseObject(deleteRes);
   }
}
