package com.hzya.frame.webapp.web.listener;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.sysnew.application.service.impl.ApplicationCache;
import com.hzya.frame.sysnew.popedomInterface.service.impl.InterfaceCache;
import com.hzya.frame.util.PluginUtils;
import com.hzya.frame.web.action.ApplicationContextUtil;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Content系统初始化参数监听
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-07 10:45
 * @Version 1.0
 */

@Component
public class InitCommandLineRunner implements ApplicationListener<ApplicationReadyEvent> {

    Logger logger = LoggerFactory.getLogger(InitCommandLineRunner.class);
    @Resource
    private ApplicationCache applicationCache;
    @Value("${data.use}")
    private boolean dataUse;
    @Resource
    private InterfaceCache interfaceCache;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        if(dataUse){
            logger.info("初始化ApplicationCache Map start <<<<<<<<<<<<<<<<<<<<<<<<");
            applicationCache.reloadData("1");
            applicationCache.reloadData("2");
            applicationCache.reloadData("3");
            applicationCache.reloadData("4");

            interfaceCache.reloadData("1");
            interfaceCache.reloadData("2");
            interfaceCache.reloadData("3");
            interfaceCache.reloadData("4");
            interfaceCache.reloadData("5");
            interfaceCache.reloadData("6");
            logger.info("初始化ApplicationCache Map END <<<<<<<<<<<<<<<<<<<<<<<<");
        }
        String path = InitCommandLineRunner.class.getClassLoader().getResource("").getPath();
        ;
        logger.info("cfgHomeClassURL:" + path);
//        System.out.println(System.getProperty("java.class.path"));
        //拼接插件路径
        logger.info("初始化插件 START >>>>>>>>>>>>>>>>>>>>>>>>>>");
        String pluginCfgHomePath = path + "cfgHome" + File.separator + "plugin" + File.separator;
        File file = new File(pluginCfgHomePath);
        File[] files = file.listFiles();
        if (null != files && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                File file1 = files[i];
                File[] cFile = file1.listFiles();
                if (cFile.length > 0) {
                    for (int n = 0; n < cFile.length; n++) {
                        File file2 = cFile[n];
                        String fileName = file2.getName();
                        if (fileName.contains("pluginCfg")) {
                            InputStream is = null;
                            try {
                                is = new FileInputStream(file2);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                            String xmlStr = null;
                            try {
                                xmlStr = IOUtils.toString(is, StandardCharsets.UTF_8);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            JSONObject resultJson = JSONUtil.parseFromXml(xmlStr);
                            JSONObject pluginJson = JSONUtil.parseObj(resultJson.get("plugin"));
                            String beanID = pluginJson.get("id") + "_Initializer";
                            try {
//                                A8YundaProjectToEsbManager
                                Object object = ApplicationContextUtil.getBeanByName(beanID);
                                //System.out.println(object.toString());
                                logger.info("初始化插件成功：" + pluginJson.get("name") + " 插件编号：" + pluginJson.get("category"));
                            } catch (Exception e) {
                                logger.info("初始化插件失败：" + pluginJson.get("name") + " 插件编号：" + pluginJson.get("category"));
                                logger.error(e.getMessage());
                            }
                        }
                    }
                }
            }
        }
        logger.info("初始化插件 END <<<<<<<<<<<<<<<<<<<<<<<<");
        logger.info("执行插件init方法 START >>>>>>>>>>>>>>>>>>>>>>>>>>");
        List<PluginBaseEntity> beans = PluginUtils.getPlugins();

        for (PluginBaseEntity pluginBaseEntity : beans) {
            pluginBaseEntity.getPluginId();
            pluginBaseEntity.getPluginId();
            logger.info(pluginBaseEntity.getPluginLabel() + pluginBaseEntity.getPluginId());
        }
        logger.info("pluginBaseEntityList>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

}
