package com.skr.mdm.api.license;

import cn.hutool.core.lang.Assert;
import com.skr.mdm.license.LicenseCheckModel;
import com.skr.mdm.license.LicenseManagerHolder;
import com.skr.mdm.util.FilePathUtil;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;

/**
 * @author nfq
 * @date 2021/09/09 11:57
 */
@Slf4j
public class MdmLicenseVerify {

    /**
     * 是否超出授权数量
     *
     * @param userCount
     * @return
     */
    public static boolean isOverAllowUseNumber(Long userCount) {
        try {
            Integer allowUseNumber = getAllowUseNumber();
            return userCount > allowUseNumber;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获取授权数量
     *
     * @return
     */
    public static Integer getAllowUseNumber() throws Exception {
        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
        Assert.notNull(licenseManager, "licenseManager must not null");
        LicenseContent licenseContent = licenseManager.verify();
        LicenseCheckModel licenseCheckModel = (LicenseCheckModel) licenseContent.getExtra();
        return licenseCheckModel.getAllowUseNumber();
    }

    /**
     * 获取授权过期时间
     *
     * @return
     */
    public static Date getExpireTime() {
        try {
            LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
            Assert.notNull(licenseManager, "licenseManager must not null");
            LicenseContent licenseContent = licenseManager.verify();
            LicenseCheckModel licenseCheckModel = (LicenseCheckModel) licenseContent.getExtra();
            return licenseContent.getNotAfter();
        } catch (Exception e) {
            log.error("license异常：", e);
        }
        return null;
    }

    /**
     * 获取证书安装路径
     *
     * @return
     */
    public static String getLicensePath() {
        String licensePath;
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.startsWith("windows")) {
            licensePath = FilePathUtil.getProjectAbsolutePath(MdmLicenseVerify.class) + File.separator + "license.lic";
        } else {
            licensePath = FilePathUtil.getFileAbsolutePath(MdmLicenseVerify.class) + File.separator + "license.lic";
        }
        return licensePath;
    }

    /**
     * 获取证书临时安装路径
     *
     * @return
     */
    public static String getTransitionLicensePath() {
        String licensePath;
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.startsWith("windows")) {
            licensePath = FilePathUtil.getProjectAbsolutePath(MdmLicenseVerify.class) + File.separator + "license1.lic";
        } else {
            licensePath = FilePathUtil.getFileAbsolutePath(MdmLicenseVerify.class) + File.separator + "license1.lic";
        }
        return licensePath;
    }

    /**
     * 获取license私钥路径
     *
     * @return
     */
//    public static String getLicensePrivateKeyPath() throws FileNotFoundException {
//        String userDir = System.getProperty("user.dir");
//        String osName = System.getProperty("os.name").toLowerCase();
//        if (osName.startsWith("windows")) {
//            File file = ResourceUtils.getFile("classpath:license/privateKey.keystore");
//            return file.getAbsolutePath();
//        } else {
//            return userDir + File.separator + "privateKey.keystore";
//        }
//    }

    /**
     * 获取license公钥路径
     *
     * @return
     */
    public static String getLicensePublicCertPath() throws FileNotFoundException {
        String userDir = System.getProperty("user.dir");
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.startsWith("windows")) {
            File file = ResourceUtils.getFile("classpath:license/publicCert.keystore");
            return file.getAbsolutePath();
        } else {
            return FilePathUtil.getFileAbsolutePath(MdmLicenseVerify.class) + File.separator + "publicCert.keystore";
        }
    }

}
