package com.youchain.modules.license;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
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.RedisUtils;
import com.youchain.utils.RsaUtils;
import com.youchain.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

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



public class LicenseValidate {

    public static void validate()  throws Exception {
        //读取本地文件
        FileProperties properties = SpringContextHolder.getBean(FileProperties.class);

        String result = "";
        try {
            FileReader fileReader = new FileReader(properties.getPath().getPath() + "key/" + "license.key");
            result = fileReader.readString();

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


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

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

        if (key_price.getMainBoardSerial().equals(system_price.getMainBoardSerial())) {

        } else {

            throw new BadRequestException("主板匹配失败");
        }
        if (key_price.getCpuSerial().equals(system_price.getCpuSerial())) {

        } else {

            throw new BadRequestException("CPU匹配失败");
        }


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

        if (end_date.before(now_date)) {
            throw new BadRequestException("过期的License,请重新生成");
        } else {

        }
    }


    public static void saveCode(String license_code)  throws Exception{
        //读取本地文件

        String rsaResult="";
        try{
             rsaResult= RsaUtils.decryptByPublicKey(RsaProperties.licenseKey,license_code);
        }catch (Exception e){
            throw new BadRequestException("License无效，请重新生成");
        }


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


        if(key_price.getMainBoardSerial().equals(system_price.getMainBoardSerial())){

        }else{

            throw new BadRequestException("主板匹配失败");
        }
        if(key_price.getCpuSerial().equals(system_price.getCpuSerial())){

        }else{

            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{

            }
        }catch (Exception e){
            throw new BadRequestException("License有效期错误,请重新生成");
        }

        FileProperties properties= SpringContextHolder.getBean(FileProperties.class);

        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();

    }
}
