package com.license.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.io.FileUtil;
import com.license.entity.dto.*;
import com.license.service.LicenseService;
import com.zhgd.xmgl.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 邱平毅
 * @ClassName LicenseServiceImpl
 * @date 2022/9/20 9:42
 * @Version 1.0
 */
@Slf4j
@Service
public class LicenseServiceImpl implements LicenseService {
    @Value("${license.publicKeysStorePath}")
    String publicKeysStorePath;

    @Value("${license.licensePath}")
    String licensePath;
    /**
     * 证书subject
     */
    private String subject = "zhgd";

    /**
     * 公钥别称
     */
    private String publicAlias = "publicCert";

    /**
     * 访问公钥库的密码
     */
    private String storePass = "public_password1234";

    @Value("${is-license}")
    private Boolean isLicense;


    @Override
    public void installLicense() throws Exception {
        if (isLicense) {
            if (StringUtils.isNotBlank(licensePath)) {
                log.info("++++++++ 开始安装证书 ++++++++");
                LicenseVerifyParam param = new LicenseVerifyParam();
                param.setSubject(subject);
                param.setPublicAlias(publicAlias);
                param.setStorePass(storePass);
                param.setLicensePath(licensePath);
                param.setPublicKeysStorePath(publicKeysStorePath);
                LicenseVerify licenseVerify = new LicenseVerify();
                //安装证书
                licenseVerify.install(param);
                log.info("++++++++ 证书安装结束 ++++++++");
            }
        }
    }

    /**
     * 获取服务器硬件信息
     */
    @Override
    public LicenseCheckModel getServerInfos() {
        log.info("获取硬件信息中，当前时间：{}", LocalDateTimeUtil.now());
        //操作系统类型
        String osName = System.getProperty("os.name").toLowerCase();

        AbstractServerInfos abstractServerInfos;

        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        } else {//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }

        return abstractServerInfos.getServerInfos();
    }

    @Override
    public void upload(Map fileBaseMap) throws Exception {
        log.info("授权中，接下来打开新的大门");
        String pubDir = publicKeysStorePath.substring(0, publicKeysStorePath.lastIndexOf("/"));
        String pubName = publicKeysStorePath.substring(publicKeysStorePath.lastIndexOf("/") + 1);
        Base64Util.convertBase64ToFile(fileBaseMap.get("pub").toString(), pubDir, pubName);
        String licDir = licensePath.substring(0, licensePath.lastIndexOf("/"));
        String licName = licensePath.substring(licensePath.lastIndexOf("/") + 1);
        Base64Util.convertBase64ToFile(fileBaseMap.get("lic").toString(), licDir, licName);
        installLicense();
    }

    @Override
    public void removeLicense() {
        log.info("清除程序授权中···");
        FileUtil.del(licensePath);
        FileUtil.del(publicKeysStorePath);
        LicenseManagerHolder.setLicenseManager(null);
    }
}
