package cn.security.license.core.verify;

import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam;
import de.schlichtherle.license.DefaultLicenseParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;
import cn.security.license.core.CustomKeyStoreParam;
import cn.security.license.core.LicenseCustomManager;
import cn.security.license.core.LicenseCheckModel;
import cn.security.license.core.LicenseManagerHolder;
import cn.util.core.LogKit;
import cn.util.time.TimeKit;
import java.io.File;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;

/**
 * License校验类
 */
public class LicenseVerifyManager 
{
    /**
     * 安装License证书
     */
    public synchronized LicenseContent install(LicenseVerifyParam param){
        LicenseContent result = null;
        //1. 安装证书
        try{
            LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
            licenseManager.uninstall();
            result = licenseManager.install(new File(param.getLicensePath()));
            LogKit.info("证书安装成功!");
        }catch (Exception e){
            LogKit.error("证书安装失败！",e);
        }
        return result;
    }

    /**
     * 校验License证书
     */
    public boolean verify(){
        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
        //2. 校验证书
        try {
            LicenseContent licenseContent = licenseManager.verify();
            LogKit.info(MessageFormat.format("证书校验通过，证书有效期：{0} - {1}",
            		TimeKit.dateToStr(licenseContent.getNotBefore()),
            		TimeKit.dateToStr(licenseContent.getNotAfter())));
            return true;
        }catch (Exception e){
            LogKit.error("证书校验失败！",e);
            return false;
        }
    }

  
    /**
     * 初始化证书生成参数
     * @param param License校验类需要的参数
     * @return cn.edu.license.core.LicenseParam
     */
    private LicenseParam initLicenseParam(LicenseVerifyParam param){
        Preferences preferences = Preferences.userNodeForPackage(LicenseVerifyManager.class);

        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());

        KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerifyManager.class
                ,param.getPublicKeysStorePath()
                ,param.getPublicAlias()
                ,param.getStorePass()
                ,null);

        return new DefaultLicenseParam(param.getSubject()
                ,preferences
                ,publicStoreParam
                ,cipherParam);
    }
    
    public static Map<String, Object> info() 
    {
        LicenseCustomManager licenseManager = (LicenseCustomManager) LicenseManagerHolder.getInstance(null);
        Map<String, Object> info = new HashMap<String, Object> ();
        try {
            final LicenseContent content = licenseManager.getContent();
            //主题
            info.put("Subject", content.getSubject());
            //许可时间
            info.put("Issued", content.getIssued());
            //许可开始时间
            info.put("startDate", content.getNotBefore());
            //许可结束时间
            info.put("endDate", content.getNotAfter());
            //许可类型 User
            info.put("ConsumerType", content.getConsumerType());
            //许可数量 1
            info.put("ConsumerAmount", content.getConsumerAmount());
            //扩展属性
            LicenseCheckModel expectedCheckModel = (LicenseCheckModel) content.getExtra();
            info.put("IpAddress", String.join(",", expectedCheckModel.getIpAddress()));
            info.put("MacAddress", String.join(",", expectedCheckModel.getMacAddress()));
            info.put("CpuSerial", String.join(",", expectedCheckModel.getCpuSerial()));
            info.put("MainBoardSerial", String.join(",", expectedCheckModel.getMainBoardSerial()));
            info.put("registerAmount", expectedCheckModel.getRegisterAmount());
        } catch (Exception e) {
            LogKit.error("证书读取失败!");
        }
        return info;
    }
}
