package com.fdkankan.reg;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fdkankan.reg.dto.CamRegDto;
import com.fdkankan.reg.dto.CamRegSDto;
import com.fdkankan.reg.dto.MachineRegDto;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.util.ObjUtil;

/**
 * @author Xiewj
 * @date 2023/7/6
 */
@Slf4j
@Builder
public class RegCodeUtil {

    private String env;


    /***
     * @description:生成安装码
     * @author: Xiewj
     * @date: 2023/7/6 15:01
     * @param:  machineCode 机器码
     * @return:
     **/
    public  String GenRegeditCode(String machineCode){
        String s = Crypt.encryptAES(machineCode.trim()+"##"+this.env, true);
        log.info("安装注册码:{}",s);
        return s;
    }

    /***
     * @description:生成相机激活码
     * @author: Xiewj
     * @date: 2023/7/6 15:01
     * @param:  plaintext 需要加密的字符串 [{"SN": "SN000001", "TOSN": "", "OPTION": "ADD"}]
     * @return:
     **/
    public  String GenRegeditCamCode(String plaintext){
        String s = Crypt.encryptAES(plaintext.trim(), true);
        log.info("相机注册码:{}",s);
        log.info("相机注册码原始长度:{}",s.length());
        String code = DeflaterUtil.zipString(s);
        log.info("相机注册码压缩:{}",code);
        log.info("相机注册码压缩长度:{}",code.length());

        return code;
    }
    /***
     * @description 获取激活数据
     * @author: Xiewj
     * @date: 2023/7/6 15:01
     * @param:  plaintext 需要转换成对象的字符串
     * @return:
     **/
    public  MachineRegDto ParseMachineCode(String plaintext){
        try {
            String source = Crypt.decryptAES(plaintext.trim(), true);
            log.info("相机注册码解密:{}",source);
            MachineRegDto t = JSON.to( MachineRegDto.class,JSONObject.parseObject(source));

            return t;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("校验失败");
        }
    }
//    /***
//     * @description 获取激活数据
//     * @author: Xiewj
//     * @date: 2023/7/6 15:01
//     * @param:  plaintext 需要转换成对象的字符串
//     * @return:
//     **/
//    public  CamRegDto ParseRegeditCamCode(String machineCode , String plaintext){
//        try {
//            //解压
//            String code = DeflaterUtil.unzipString(plaintext);
//            log.info("相机注册码解压:{}",code);
//            String source = Crypt.decryptAES(code, true);
//            log.info("相机注册码解密:{}",source);
//            CamRegDto t = JSON.toJavaObject(JSONObject.parseObject(source) , CamRegDto.class);
//            if (ObjUtil.isNull(t.getMachineCode()) && !t.getMachineCode().equals(machineCode)){
//                throw new RuntimeException("校验失败");
//            }
//            return t;
//        }catch (Exception e){
//            e.printStackTrace();
//            return null;
//        }
//    }

    /***
     * @description:生成相机激活码多个
     * @author: Xiewj
     * @date: 2023/7/6 15:01
     * @param:  plaintext 需要加密的字符串 [{"SN": "SN000001", "TOSN": ""},{"SN": "SN000001", "TOSN": ""}]
     * @return:
     **/
    public  String batchGenRegeditCamCode( CamRegSDto dtos){
        dtos.setEnv(env);
        String camCode = GenRegeditCamCode(JSONObject.toJSONString(dtos).toString());
        return camCode;
    }
    /***
     * @description 获取激活数据
     * @author: Xiewj
     * @date: 2023/7/6 15:01
     * @param:  plaintext 需要转换成对象的字符串
     * @return:
     **/
    public  CamRegSDto BatchParseRegeditCamCode(String machineCode,String plaintext){
        try {
            String trim=plaintext.trim();
            if (trim.contains(" ")||trim.length()<=3){
                throw new RuntimeException("校验失败");
            }
            //解压
            String code = DeflaterUtil.unzipString(trim);
            log.info("批量生成相机码解压:{}",code);
            String source = Crypt.decryptAES(code, true);
            CamRegSDto t = JSON.to(CamRegSDto.class,JSONObject.parseObject(source));
            if (ObjUtil.isNull(t.getEnv())||!env.equals(t.getEnv())){
                throw new RuntimeException("环境比对失败");
            }
            if (ObjUtil.isNotNull(t.getMachineCode()) && !t.getMachineCode().equals(machineCode.trim()) ){
                throw new RuntimeException("校验失败");
            }
            return t;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("校验失败");
        }
    }
    public static  void main(String[] args) {
        RegCodeUtil uat = RegCodeUtil.builder().env("PROD").build();

        String machineCode="E17CB0E629A47B26E455D9D8EF35EEAEBF31E9F4B5F40A253550E9A589897927BE11825E5422400D0BE6BF44C0734743";


        //读取机器码
        MachineRegDto machineRegDto = uat.ParseMachineCode(machineCode);
        System.out.println(machineRegDto);


        //生成安装注册码
        String s = uat.GenRegeditCode(machineCode);

//        CamRegDto camRegDto1=new CamRegDto();
//        camRegDto1.setSn("00000001");
//        camRegDto1.setMachineCode(machineCode);
//        //生成相机注册码
//        String camCode = GenRegeditCamCode( JSON.toJSONString(camRegDto1));
//        //校验相机注册码
//        CamRegDto camRegDto3 = ParseRegeditCamCode(machineCode,camCode);


        List<CamRegDto> list=new ArrayList<>();

//        CamRegDto camRegDto=new CamRegDto();
////        camRegDto.setSn("dvt000007");
////        camRegDto.setToSn("90d95cdb5");
////        camRegDto.setToSn("dvt000007");
//        camRegDto.setSn("a0ia7r137");
//        camRegDto.setCameraType(9);

        CamRegDto camRegDto=new CamRegDto();
        camRegDto.setSn("b0147t287");
        camRegDto.setCameraType(11);
        list.add(camRegDto);

//
//        CamRegDto camRegDto1=new CamRegDto();
//        camRegDto1.setSn("bpvt00017");
////        camRegDto1.setToSn("c123456789");
//        camRegDto1.setCameraType(11);
//
//        list.add(camRegDto1);
//
//
        CamRegSDto CamReg=new CamRegSDto();
        CamReg.setMachineCode(machineCode);
        CamReg.setCamRegs(list);
        String camCode1 = uat.batchGenRegeditCamCode( CamReg);
        log.info("生成相机注册码:{}",camCode1);

//        List<CamRegDto> list1 = BatchParseRegeditCamCode(machineCode+"a",camCode1);
        CamRegSDto list1 = uat.BatchParseRegeditCamCode(machineCode,"eNqFk8mVxEAIQ1NiLeCIWfIPaegI5tT12i4j6YtP6YFjQw4Rz4MlJEqr9Rae7icx+GWFThVkdyOqUGXCt6BisgStm26hFcmhaar1HNKCtfArBxdaKLPBiDfOufbVu8uvZsrgk+/7oBj3mdo90/oUhJ/1F7rOqk7hbv61lDwfp3ttdEy/5YYCA/gM2uNpn6dVQ47X+aQzAPunCi1Z2mj922kdkRkzeqsIQWEBO+eMPqlSiSIDKoKarI+5eLfGNsaGTLoHTkO03DPQcR1NKf1uSH2IdAf1l1QhQV+g0lp8S0OueTHeJcJqfPbukKIoYjfFY/TU511Le7TvE0phrJ9OFKtmFYn3BNAZo0y4iX1O6Dk8cPEKtri90y8r5nMUG8EA+iDf5d8Ou7b/8TkYcV8/0wgX6NlZ+CKCmNnJ/+VzF3lq9LHNazcCAQ+0+j7NZ2jn4iYKLOBqTH1hcqSU+hNdyvQ4wntJYF452xpB/fqGIHjUrrM3OT+gXvnofYzdybOQ2VcKnMtsNQHfRA5Q9pX02PnFNwzSlzuGJ24c9v5Mxmtv6Anqa8uc2guS17OyZQ4IH+QSqdl6uAw08YA/YM2cry/351cAwbPBFHK05Vbhvlt6Z17T4LeGRzp5By7bIKHRa+WMwlNPR4EsPQh8evOSrMXYPLtI+FmZh+XtHeqzfPqaAOjZ4dbP6wjk1XPY8vzSLNNmf4OOrq0sBIrtB6vfrd/IafW8Wl3oPmkun9+awLXlLuq2lq5JBqdOzP0E4W0SKfsXp4CeFDVf2XOa4cncWogkyHu3hDfQHxRefYbPq/G71m/JLYn4/V+ulTXyfhgEX+ay+B+/7QuD");




        log.info("结束");
    }

}
