package com.jpsoft.shinestar.modules.common.utils;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.jpsoft.shinestar.config.TemperatureConfig;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 墨鱼_mo
 * @date 2020-3-14 16:43
 */
@Slf4j
@Component
public class LApiUtil {
    private final static int TIMEOUT = 10 * 1000;

    @Autowired
    private PersonInfoService personInfoService;


    public static JSONObject postRequest(String url, HashMap map) {
        String body = HttpRequest.post(url)
                .timeout(TIMEOUT)
                .body(new JSONObject(map).toJSONString()).execute().body();
        JSONObject jsonbody = JSONObject.parseObject(body);
        return jsonbody;
    }

    public static JSONObject getRequest(String url) throws Exception {
        String body = HttpRequest.get(url)
                .timeout(TIMEOUT)
                .execute().body();

        JSONObject jsonbody = JSONObject.parseObject(body);
        return jsonbody;
    }

    public static JSONObject deleteRequest(String url) {
        String body = HttpRequest.delete(url)
                .timeout(TIMEOUT)
                .execute().body();
        JSONObject jsonbody = JSONObject.parseObject(body);
        return jsonbody;
    }

    public static JSONObject putRequest(String url) {
        String body = HttpRequest.put(url)
                .timeout(TIMEOUT)
                .execute().body();

        JSONObject jsonbody = JSONObject.parseObject(body);
        return jsonbody;
    }

    public static JSONObject putRequest(String url, HashMap map) {
        String body = HttpRequest.put(url)
                .timeout(TIMEOUT)
                .body(new JSONObject(map).toJSONString()).execute().body();

        JSONObject jsonbody = JSONObject.parseObject(body);
        return jsonbody;
    }

    public static JSONObject getResponse(JSONObject dataJson) throws Exception {
        if (dataJson.getInteger("ResponseCode") == 1) {
            throw new Exception("通用错误");
        }
        if (dataJson.getInteger("ResponseCode") == 2) {
            throw new Exception("参数非法");
        }
        if (dataJson.getInteger("ResponseCode") == 3) {
            throw new Exception("用户无权限");
        }
        if (dataJson.getInteger("ResponseCode") == 4) {
            throw new Exception("设备不支持");
        }
        if (dataJson.getInteger("ResponseCode") == 5) {
            throw new Exception("用户状态异常");
        }
        if (dataJson.getInteger("StatusCode") == 1) {
            throw new Exception("执行失败");
        }
        if (dataJson.getInteger("StatusCode") == 2) {
            throw new Exception("输入参数非法");
        }
        if (dataJson.getInteger("StatusCode") == 3) {
            throw new Exception("系统内存不足");
        }
        if (dataJson.getInteger("StatusCode") == 8) {
            throw new Exception("功能不支持");
        }
        if (dataJson.getInteger("StatusCode") == 10) {
            throw new Exception("序列化执行失败");
        }
        if (dataJson.getInteger("StatusCode") == 11) {
            throw new Exception("Basic 鉴权失败");
        }
        if (dataJson.getInteger("StatusCode") == 12) {
            throw new Exception("Digest 鉴权失败");
        }
        if (dataJson.getInteger("StatusCode") == 13) {
            throw new Exception("订阅已满");
        }
        if (dataJson.getInteger("StatusCode") == 14) {
            throw new Exception("重复订阅");
        }

        return dataJson.getJSONObject("Data");
    }

    public String getMatchMsg(Integer matchStatus, Integer maskFlag, BigDecimal temperature, TemperatureConfig temperatureConfig) {
        String matchMsg = "";
        if (matchStatus == 1) {
            matchMsg = "核验成功";
        } else if (matchStatus == 2) {
            matchMsg = "对比失败";
        } else if (matchStatus == 3) {
            matchMsg = "对比成功，不在布控时间";
        } else if (matchStatus == 7) {
            matchMsg = "刷脸开门模式下，刷脸成功";
        } else if (matchStatus == 10) {
            if (temperature.compareTo(temperatureConfig.getMax()) == 1) {
                matchMsg = "对比成功，体温异常";
            } else if (maskFlag == 1) {
                matchMsg = "对比成功，未戴口罩";
            } else {
                matchMsg = "对比成功，人脸属性异常";
            }

        } else {
            matchMsg = "未知匹配消息";
        }

        return matchMsg;
    }

    public static Integer imageSize(String imageBase64Str) {

        //1.找到等号，把等号也去掉(=用来填充base64字符串长度用)
        Integer equalIndex = imageBase64Str.indexOf("=");
        if (imageBase64Str.indexOf("=") > 0) {
            imageBase64Str = imageBase64Str.substring(0, equalIndex);
        }
        //2.原来的字符流大小，单位为字节
        Integer strLength = imageBase64Str.length();
        //   System.out.println("imageBase64Str Length = "+strLength);
        //3.计算后得到的文件流大小，单位为字节
        Integer size = strLength - (strLength / 8) * 2;
        return size;
    }

    public static PersonInfo getPersonCode(cn.hutool.json.JSONObject libMatInfoListJson, cn.hutool.json.JSONObject matchPersonInfo) {
        PersonInfo personInfo = new PersonInfo();
        String personName = "";
        Long personId = 0L;
        String identityNo = "";
        if (org.apache.commons.lang.StringUtils.isNotBlank(libMatInfoListJson.getStr("MatchPersonID"))) {
            personName = matchPersonInfo.getStr("PersonName");
            personId = libMatInfoListJson.getLong("MatchPersonID");
            identityNo = matchPersonInfo.getStr("IdentityNo");
            if (personName.contains("_")) {
                personId = Long.valueOf(personName.substring(personName.indexOf("_") + 1));
                personName = personName.substring(0, personName.indexOf("_"));


            }


        }


        personInfo.setId(personId);
        personInfo.setName(personName);
        personInfo.setIdCard(identityNo);
        return personInfo;


    }

    public static boolean compareToInterval(BigDecimal bigDecimal, float min, float max) {
        if (bigDecimal.compareTo(new BigDecimal(min)) == -1 || bigDecimal.compareTo(new BigDecimal(max)) == 1) {
            return false;
        } else {
            return true;
        }

    }

    public static boolean compareToInterval(BigDecimal bigDecimal, BigDecimal min, BigDecimal max) {
        if (bigDecimal.compareTo(min) == -1 || bigDecimal.compareTo(max) == 1) {
            return false;
        } else {
            return true;
        }

    }


    public static void main(String[] args) {
        //   Date date = new Date(1584405803000L);
        //    Date date = new
        //   Date date = new Date();
        // long time = date.getTime();
        //   System.out.println(date);
        //  192.168.11.13:80/V1.0/PeopleLibraries/1584409665/People/1111?LastChange=1584428390872
//        cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject();
//        Integer integer = getPersonCode(jsonObject,jsonObject);
//        System.out.println(integer);
        Map<String, Object> map = new HashMap<>();

//        String requestStr = HttpRequest.post("http://www.baidu.com")
//                .timeout(TIMEOUT)
//                .body(new JSONObject(map).toJSONString());


//        System.out.println(requestStr);
    }
}

