
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lutao.cloud.collect.utils.SHAEncrypt;
import com.lutao.entity.PersonCertificate;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
//        System.out.println(getRequestBody(getRequestData()));
//        System.out.println(getRequestBody(null));
//        getDecode();
//        System.out.println(testJson());

        String s = "zki6/UaJcwaARF8JVqyygdmeDexN53+cg6IwrKyaH2X7OMrhRMSgtsoRnRoy2icbtvrIGEECcGQ2l9CXUmhqXSRHtjqySGSpIMXzolAIhkTP0Tih7RGBc01t0tCNket0teOj3zah8j1aAZ/SdBbN4XYj4y4CbkU1T0vykpctVXwlQBGyk0SPNACsyj2Z4wLVrCfbemmIY0U9eG6Qv6AEfNqUnyaUNa+RV8Nh003ABOpAtcpRmQvhNQVOMVJO/UJiWu2AozLvmYeJon+NwlTTqg5h2oDXK33sjbcm1uz6CdH4schJjyz9vDN7QE9vvYtiTcvb70P8ViaUSNPuJfHvxDFEEReBjs7UwPuRhDfwKuJjjhYMoLcx66FPK52ldd9LaeFYGP0u5xZkEoMSj1GXZXx7E5Jf1C9WwCi9xlrE+7rumRkGLpLi7sMjW5VNmnRaOf7C7iwTmg5MZPugfAvetE2jhrOyKz9Z/YeVXVCca3n535c062gv2sM47toThqE=";

        AES aes = new AES(Mode.CFB, Padding.NoPadding,
                new SecretKeySpec("wehyy%kwehdwdese".getBytes(StandardCharsets.UTF_8), "AES"),
                new IvParameterSpec("wehyy%kwehdwdese".getBytes(StandardCharsets.UTF_8)));

        System.out.println(aes.decryptStr(Base64.decode(s)));

        PersonCertificate pc = new PersonCertificate();

        List<PersonCertificate>  list = JSONUtil.toList(aes.decryptStr(Base64.decode(s)),PersonCertificate.class);

        System.out.println("s");
    }

    /**
     * 举例
     * appkey=abc    appSecret=def    AES加密key=wehyy%kwehdwdese
     * 初始化发送Json格式data内容
     * @return
     */
    public static JSONArray getRequestData() {
        String reqData = "data:image/jpeg;base64,[encoded data, 9436 characters]";
        JSONArray jsonArray = JSONUtil.parseArray(reqData);
        return  jsonArray;
    }

    /**
     * 获取请求body内容
     * @param jsonObject
     * @return
     */
    public static JSONObject getRequestBody(JSONArray jsonObject) {
        String appKey = "abc";
        String appSecret = "def";
//        Long ts = System.currentTimeMillis();
        Long ts = 1688552964000L;
        String requestId = "request-123";
//        String command = "add";
        String command = "file_id";
        String aeskey = "wehyy%kwehdwdese";
        String resource = "person_info";
//        String resource = "perception_monitor_info";

//        String reqData = JSONUtil.toJsonStr(jsonObject);
        String reqData = "[{\"id\":\"bc7954df1101055a199fa3671b79c8a1\",\"person_name\":\"测试数据_230714张三\",\"org_name\":\"第四采油厂（滩海开发公司）\",\"gender\":\"1\",\"birthday\":\"1982-03-08\",\"remark\":\"备注123456\",\"phone\":\"13100000000\",\"position\":\"张三\",\"create_date\":\"2023-06-0614:30:04\",\"create_by\":\"张三\",\"update_date\":\"2023-06-0614:30:04\",\"update_by\":\"张三\"}]";
//        String reqData = "1234567890";
        reqData = testJson();

        System.out.println("reqData: " + reqData);
        StringBuffer sb = new StringBuffer();

        sb.append(appKey).append(appSecret).append(ts).append(requestId)
                .append(command).append(resource).append(reqData);
        System.out.println("111: " + sb.toString());
        // 获取sign
        String sign = SHAEncrypt.SHA256(sb.toString());
        System.out.println("sign: " + sign);

//        String sign2 = SHAEncrypt.SHA256("abcdef1688552964000request-123addperception_monitor_info[[\"bc7954df1101055a199fa3671b79c8a1\",\"张三\"]]");
//        System.out.println("sign2:" + sign2);

        AES aes = new AES(Mode.CFB, Padding.NoPadding,
                new SecretKeySpec(aeskey.getBytes(StandardCharsets.UTF_8), "AES"),
                new IvParameterSpec(aeskey.getBytes(StandardCharsets.UTF_8)));

        JSONObject requestBodyJson = new JSONObject();
        // 获取加密后的data
        String resData = aes.encryptBase64(reqData);
        requestBodyJson.putOnce("requestId",requestId);
        requestBodyJson.putOnce("ts",ts);
        requestBodyJson.putOnce("command",command);
        requestBodyJson.putOnce("resource",resource);
        requestBodyJson.putOnce("sign",sign);
        requestBodyJson.putOnce("data",resData);
        requestBodyJson.putOnce("appKey",appKey);
        return requestBodyJson;
    }


    public static void getDecode() {
        String ss = "zmi69kGccBHxFw0OIa+39VaXZPj9xrZmTvumnXSglw8M6zDRDJVhqcw/u/vKdsY=";
        String key = "wehyy%kwehdwdese";
        AES aes = new AES(Mode.CFB, Padding.NoPadding,
                new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"),
                new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8)));
        System.out.println(aes.decryptStr(ss));
        System.out.println(JSONUtil.isJsonArray(aes.decryptStr(ss)));
    }

    private static String  testJson(){
        Map<String,String> map = new HashMap<>();

        map.put("id","bc7954df1101055a199fa3671b79c8a1");
        map.put("person_name","测试  数据_230714张三");
        map.put("org_name","第四采油厂（滩海  开发公司）");
        map.put("gender","1");
        map.put("birthday","1982-03-08");
        map.put("remark","备注123456");
        map.put("phone","13100  000000");
        map.put("position","张三");
        map.put("create_date","2023-06-06 14:30:04");
        map.put("create_by","张三");
        map.put("update_date","2023-06-06 14:30:04");
        map.put("update_by","张三");

        List<Map> list=  new ArrayList<>();
        list.add(map);


        return JSONUtil.toJsonStr(list);

    }

}
