package cn.estsh.i3plus.core.apiservice.listener;

import cn.estsh.i3plus.core.api.iservice.busi.ISystemResourceService;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.platform.bean.SysLocaleLanguage;
import cn.estsh.i3plus.pojo.platform.bean.SysLocaleResource;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Description : 初始化资源配置
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-10-12 18:43
 * @Modify:
 **/
@Component
public class SysResourceBindleListener implements ApplicationListener<ContextRefreshedEvent> {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonConstWords.SYSTEM_LOG);

    @Autowired
    public ISystemResourceService systemResourceService;

    @Resource(name="redisRes")
    private ImppRedis redisRes;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        /**
         * 整体数据结构：
         * code <lang : value>
         *    先根据code查询，判断是否存在，然后通过语言获取对应的值
         */
        HashMap<String,HashMap<String,String>> resMap = new HashMap();
        HashMap<String,String> lanMap = null;
        HashMap<String,HashMap<String,String>> webLangMap = new HashMap<>();
        HashMap<String,String> webResMap;

        //语言数量
        List<SysLocaleLanguage> langList = systemResourceService.listSysLocaleLanguage(null);

        //查询所有资源
        List<SysLocaleResource> resourceList = systemResourceService.listSysLocaleResource(null);
        LOGGER.info("【加载平台资源】共有{}个资源，{}种语言。",resourceList.size(),langList.size());
        for(SysLocaleResource res : resourceList) {
            //遍历资源，放入map中
            lanMap = resMap.get(res.getResourceKey());
            //判断是否已存在资源信息
            if(lanMap == null){
                lanMap = new HashMap<String,String>();
                //将key/代码放入
                resMap.put(res.getResourceKey(),lanMap);
            }
            //根据语言放入
            lanMap.put(res.getLanguageCode(),res.getResourceValue());

            // web 资源
            if(!webLangMap.containsKey(res.getLanguageCode())){
                webLangMap.put(res.getLanguageCode(),new HashMap<>());
            }
            // 放入资源信息
            webResMap = webLangMap.get(res.getLanguageCode());
            webResMap.put(res.getResourceKey(),res.getResourceValue());
        }

        for(String key : resMap.keySet()){
            //放入缓存
            redisRes.putHashMap(key,resMap.get(key),0);
        }

        for(String key : webLangMap.keySet()){
            //放入缓存
            redisRes.putHashMap(key,webLangMap.get(key),0);
        }

        LOGGER.info("【资源配置文件已全部加载：{}个】",resMap.size());

        /*
        //资源集合
        HashMap<String,String> resMap;

        //模块集合
        HashMap<String,HashMap<String,String>> langModoleMap = new HashMap();
        //功能集合
        HashMap<String,HashMap<String,String>> langMethodMap = new HashMap();
        //按钮集合
        HashMap<String,HashMap<String,String>> langBtnMap = new HashMap();
        //信息集合
        HashMap<String,HashMap<String,String>> langMsgMap = new HashMap();
        //异常集合
        HashMap<String,HashMap<String,String>> langExMap = new HashMap();

        *//************** 加载国际化 *************//*
        for(SysLocaleLanguage lang : langList) {
            *//************** 加载模块 *************//*
            resource = new SysLocaleResource();
            resource.setResourceType(CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.MODULE.getValue());
            resource.setLanguageCode(lang.getLanguageCode());
            resourceList = systemResourceService.listSysLocaleResource(resource);

            if(resourceList.size() > 0) {
                LOGGER.info("【{}-{},{}】共有{}个资源。", CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.MODULE.getDescription(),
                        lang.getLanguageName(), lang.getLanguageCode(), resourceList.size());
            }
            resMap = new HashMap<String,String>();
            for(SysLocaleResource res : resourceList){
                resMap.put(res.getResourceKey(),res.getResourceValue());
            }
            langModoleMap.put(lang.getLanguageCode(),resMap);

            *//************** 加载功能 *************//*
            resource = new SysLocaleResource();
            resource.setResourceType(CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.METHOD.getValue());
            resource.setLanguageCode(lang.getLanguageCode());
            resourceList = systemResourceService.listSysLocaleResource(resource);

            if(resourceList.size() > 0) {
                LOGGER.info("【{}-{},{}】共有{}个资源。",CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.METHOD.getDescription(),
                                        lang.getLanguageName(),lang.getLanguageCode(),resourceList.size());
            }

            resMap = new HashMap<String,String>();
            for(SysLocaleResource res : resourceList){
                resMap.put(res.getResourceKey(),res.getResourceValue());
            }
            langMethodMap.put(lang.getLanguageCode(),resMap);

            *//************** 加载按钮 *************//*
            resource = new SysLocaleResource();
            resource.setResourceType(CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.BUTTON.getValue());
            resource.setLanguageCode(lang.getLanguageCode());
            resourceList = systemResourceService.listSysLocaleResource(resource);

            if(resourceList.size() > 0) {
                LOGGER.info("【{}-{},{}】共有{}个资源。", CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.BUTTON.getDescription(),
                        lang.getLanguageName(), lang.getLanguageCode(), resourceList.size());
            }

            resMap = new HashMap<String,String>();
            for(SysLocaleResource res : resourceList){
                resMap.put(res.getResourceKey(),res.getResourceValue());
            }
            langBtnMap.put(lang.getLanguageCode(),resMap);

            *//************** 加载资源 *************//*
            resource = new SysLocaleResource();
            resource.setResourceType(CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.COMMON.getValue());
            resource.setLanguageCode(lang.getLanguageCode());
            resourceList = systemResourceService.listSysLocaleResource(resource);

            if(resourceList.size() > 0) {
                LOGGER.info("【{}-{},{}】共有{}个资源。", CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.COMMON.getDescription(),
                        lang.getLanguageName(), lang.getLanguageCode(), resourceList.size());
            }
            resMap = new HashMap<String,String>();
            for(SysLocaleResource res : resourceList){
                resMap.put(res.getResourceKey(),res.getResourceValue());
            }
            langMsgMap.put(lang.getLanguageCode(),resMap);

            *//************** 加载异常 *************//*
            resource = new SysLocaleResource();
            resource.setResourceType(CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.EXCEPTION.getValue());
            resource.setLanguageCode(lang.getLanguageCode());
            resourceList = systemResourceService.listSysLocaleResource(resource);

            if(resourceList.size() > 0) {
                LOGGER.info("【{}-{},{}】共有{}个资源。", CommonEnumUtil.SYS_LOCALE_RESOURCE_TYPE.EXCEPTION.getDescription(),
                        lang.getLanguageName(), lang.getLanguageCode(), resourceList.size());
            }
            resMap = new HashMap<String,String>();
            for(SysLocaleResource res : resourceList){
                resMap.put(res.getResourceKey(),res.getResourceValue());
            }
            langExMap.put(lang.getLanguageCode(),resMap);
        }

        redisCore.putHashMap(CommonConstWords.RES_PLAT_MODULE,langModoleMap);
        redisCore.putHashMap(CommonConstWords.RES_PLAT_MODULE_METHOD,langMethodMap);
        redisCore.putHashMap(CommonConstWords.RES_PLAT_MODULE_BUTTON,langBtnMap);
        redisCore.putHashMap(CommonConstWords.RES_LANGUAGE_CONVERT,langMsgMap);
        redisCore.putHashMap(CommonConstWords.RES_EXCEPTION_CONVERT,langExMap);

        LOGGER.info("【资源配置文件已全部加载】");*/
    }
}
