package com.youchain.modules.license;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.youchain.config.FileProperties;
import com.youchain.config.RsaProperties;
import com.youchain.exception.BadRequestException;
import com.youchain.modules.license.domain.LicenseCheck;
import com.youchain.utils.RsaUtils;
import com.youchain.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;
import java.util.Date;


@Slf4j
public class LicenseValidate {

    public static void validate()  throws Exception{
        //读取本地文件
        /*FileProperties properties= SpringContextHolder.getBean(FileProperties.class);
        log.info(properties.getPath().getPath());
        String result="";
        try{
            FileReader fileReader = new FileReader(properties.getPath().getPath()+"key/"+"license.key");
            result = fileReader.readString();

        }catch (Exception e){
//            throw new BadRequestException("License不存在,请上传License");
        }

        log.error(result);
        String rsaResult= RsaUtils.decryptByPublicKey(RsaProperties.licenseKey,result);
        log.error("------------------------解密完成-----------------------");
        LicenseCheck key_price = JSONUtil.toBean(rsaResult, LicenseCheck.class);
        LicenseCheck system_price = getDeviceInfo();




        if(key_price.getMainBoardSerial().equals(system_price.getMainBoardSerial())){
            log.error("主板匹配成功");
        }else{
            log.error("主板匹配失败");
            throw new BadRequestException("主板匹配失败");
        }
        if(key_price.getCpuSerial().equals(system_price.getCpuSerial())){
            log.error("CPU匹配成功");
        }else{
            log.error("CPU匹配失败");
            throw new BadRequestException("CPU匹配失败");
        }

        try{
            Date end_date = DateUtil.parse(key_price.getEndTime());
            Date now_date = DateUtil.date();

            if(end_date.before(now_date)){
                throw new BadRequestException("过期的License,请重新生成");
            }else{
                log.error("有效期验证通过");
            }
        }catch (Exception e){
            throw new BadRequestException("无效的License,请重新生成");
        }*/
    }



    public static String  getLicense(String time)  throws Exception{
        LicenseCheck licenseCheck=getDeviceInfo();
        licenseCheck.setEndTime(time);
        String rest= JSONUtil.toJsonStr(licenseCheck).trim();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lHq99mg5USgusM+fB8FkEmkpRDE6ECgYBTqpB+S9kyTJ5/hU3Zk1htfGJeq41U6e47aeWpH2poLBLttcsw68Qn43I+nGwkxe0iNIxIN2+LBO/+VjflMh3DmRHRoP0q228FMuJde2jwlvq7UP9+qn8NPX/+MuD4R3XKnJAu0FoBykWXeFD+gHQ6RkWwvzt5I8ohAxZYDmjKSQKBgB5gwhM9+5v7U7raCi5enJ0d4buzx33nwSfTdbQGFaaVwtPOU7JgvQhK6GckX5r63WuBpsMvQ488VftLXBBl6FaIK8iy2gWLyXtSgyN4nChHo3/78ogtwt2EMI/8rJooDPvEZfzlHbbD57rmOxJoEWb/rrsxfq40eRAP6oHX547BAoGBAMK7GGdYkzxkD2xuMZSfPro/sO0EbghJdJ/l8nioTEPTPyRx5AZBe/7OhiKYohpUdi5F8+S18ACn88rJkZn7iQGuJvjXCFYnt9bec+w8lqiUcjfw3l4+aAkNTfquIdKz6gJyUBVjbuQYYkzGf052HbMacDwwfbHIlDvpfAS7FUpq";

        /*String result=RsaUtils.encryptByPrivateKey(priKey,rest);
        //读取本地文件

        log.error("------------------------加密完成-----------------------");

        log.error(result);
        String rsaResult= RsaUtils.decryptByPublicKey(RsaProperties.licenseKey,result);

        log.error(rsaResult);
        log.error("------------------------解密完成-----------------------");
        LicenseCheck key_price = JSONUtil.toBean(rsaResult, LicenseCheck.class);
        LicenseCheck system_price = getDeviceInfo();



        if(key_price.getMainBoardSerial().equals(system_price.getMainBoardSerial())){
            log.error("主板匹配成功");
        }else{
            log.error("主板匹配失败");
            throw new BadRequestException("主板匹配失败");
        }
        if(key_price.getCpuSerial().equals(system_price.getCpuSerial())){
            log.error("CPU匹配成功");
        }else{
            log.error("CPU匹配失败");
            throw new BadRequestException("CPU匹配失败");
        }

        try{
            Date end_date = DateUtil.parse(key_price.getEndTime());
            Date now_date = DateUtil.date();

            if(end_date.before(now_date)){
                throw new BadRequestException("过期的License,请重新生成");
            }else{
                log.error("有效期验证通过");
            }
        }catch (Exception e){
            throw new BadRequestException("无效的License,请重新生成");
        }*/
        return null;
    }


    public static void saveCode(String license_code)  throws Exception{
        //读取本地文件
        /*log.error(license_code);
        String rsaResult="";
        try{
             rsaResult= RsaUtils.decryptByPublicKey(RsaProperties.licenseKey,license_code);
        }catch (Exception e){
            throw new BadRequestException("License无效，请重新生成");
        }
        log.error("解密完成");
        log.error(rsaResult);

        LicenseCheck key_price = JSONUtil.toBean(rsaResult, LicenseCheck.class);
        LicenseCheck system_price = getDeviceInfo();




        if(key_price.getMainBoardSerial().equals(system_price.getMainBoardSerial())){
            log.error("主板匹配成功");
        }else{
            log.error("主板匹配失败");
            throw new BadRequestException("主板匹配失败");
        }
        if(key_price.getCpuSerial().equals(system_price.getCpuSerial())){
            log.error("CPU匹配成功");
        }else{
            log.error("CPU匹配失败");
            throw new BadRequestException("CPU匹配失败");
        }

        try{
            Date end_date = DateUtil.parse(key_price.getEndTime());
            Date now_date = DateUtil.date();

            if(end_date.before(now_date)){
                throw new BadRequestException("过期的License,请重新生成");
            }else{
                log.error("有效期验证通过");
            }
        }catch (Exception e){
            throw new BadRequestException("无效的License,请重新生成");
        }

        FileProperties properties= SpringContextHolder.getBean(FileProperties.class);
        log.info(properties.getPath().getPath());

        FileWriter writer = new FileWriter(properties.getPath().getPath()+"key/"+"license.key");
        writer.write(license_code);*/





    }

    public static LicenseCheck getDeviceInfo(){
        /*String   osName = System.getProperty("os.name");
        osName = osName.toLowerCase();
        AGxServerInfos abstractServerInfos;
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        }else{//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }
        return abstractServerInfos.getServerInfos();*/
        return null;
    }
}
