package com.fdkankan.ucenter.common.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.XML;
import com.fdkankan.common.util.FileUtils;
import com.fdkankan.ucenter.util.DateUserUtil;
import com.fdkankan.ucenter.vo.response.AuthLicenseEntityVo;
import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.validation.constraints.Size;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Slf4j
public class AuthLicenseUtil {

    //    Launcher.bat @inPath";
     static String  cmd ="sudo bash /home/ubuntu/bin/Launcher.sh %s";
     static String licensePath = "/home/backend/tomcat/ucenter/license";

     public static JSONObject readLicense(){
         try {
             if (FileUtil.exist(licensePath + File.separator + "results" + File.separator + "encryption_info.xml")) {
                 String xml = FileUtils.readFile((licensePath + File.separator + "results" + File.separator + "encryption_info.xml"));
                 return XML.toJSONObject(xml);
             }
         }catch (Exception e){
            log.error("readLicense-error:",e);
         }

         return createLicense();
     }

    public static JSONObject createLicense()  {
        File file = new File(licensePath);
        if(!file.exists()){
            file.mkdirs();
        }
        File caches = new File(licensePath + "caches");
        FileUtil.del(caches);
        File results = new File(licensePath + "results");
        FileUtil.del(results);

        JSONObject licenseDataJson = new JSONObject();
        licenseDataJson.put("split_type", "SPLIT_V23");
        licenseDataJson.put("skybox_type", "SKYBOX_V5");
        FileUtils.writeFile(licensePath + File.separator + "data.json", licenseDataJson.toString());
        ShellUtil.execCmd(String.format(cmd,licensePath));
        int count = 10;
        try {
            for (int i = count; i > 0; i--) {
                if (FileUtil.exist(licensePath + File.separator + "results" + File.separator + "encryption_info.xml")) {
                    String xml = FileUtils.readFile((licensePath + File.separator + "results" + File.separator + "encryption_info.xml"));
                    return XML.toJSONObject(xml);
                }
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            log.error("createLicense-error:",e);
            return null;
        }
        return null;

    }

    public static  void checkAuthLicense() {
        try {
            JSONObject jsonObject = AuthLicenseUtil.readLicense();
            JSONObject licenseInfo = jsonObject.getJSONObject("licenseInfo");
            JSONObject snInfo = licenseInfo.getJSONObject("snInfo");
            String endDate = snInfo.getStr("endDate");
            Date date = DateUserUtil.getDate(endDate);
            if(new Date().getTime() > date.getTime()){
                AuthLicenseUtil.createLicense();
            }
        }catch (Exception e){
            log.error("checkAuthLicense-error:",e);
        }

    }

    public static void main(String[] args) {

    }


}
