package com.brilliance.isc.common.config;


import com.brilliance.isc.constant.ErrorCode;
import com.brilliance.isc.exception.RestBizException;
import com.brilliance.mda.runtime.mda.config.IniConfig;
import com.brilliance.mda.runtime.mda.util.Inis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;

@Component
public class IniLoadConfig {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private String[] iniNames = {"cbsconf.ini", "intace.ini", "swtovr20.ini", "swtsup20.ini",
            "tcd.ini", "tdpara.ini", "tdtrans.ini", "vip.ini", "boltxt.ini", "elcctc.ini", "swttxt.ini"};


    @PostConstruct
    public void loadIni() throws IOException {
        for (int i = 0; i < iniNames.length; i++) {
            String name = iniNames[i].toUpperCase();
            Resource resource = new ClassPathResource("ini/" + iniNames[i]);
            InputStream ins = resource.getInputStream();
//            String iniStr = IOUtils.toString(ins,"UTF-8");
//            logger.info("{} ini UTF-8内容：{}",iniNames[i],iniStr);
//            resource = new ClassPathResource("ini/"+iniNames[i]);
//            ins = resource.getInputStream();
//            iniStr = IOUtils.toString(ins,"GBK");
//            logger.info("{} ini GBK内容：{}",iniNames[i],iniStr);
//            resource = new ClassPathResource("ini/"+iniNames[i]);
//            ins = resource.getInputStream();
            logger.info("加载{}配置文件", "ini/" + iniNames[i]);
            if (ins == null) {
                throw new RestBizException(ErrorCode.CONFIG_READ_ERROR, "INI文件读取失败:" + iniNames[i]);
            }
            IniConfig.loadIni(name, resource.getInputStream());
        }
        String cbcBlk = Inis.getIniBlock("CBSCONF.INI", "PTE-LIAIN", "OPR");
        logger.info("尝试读取CBSCONFIG数据PTE-LIAIN#OPR: {}", cbcBlk);
    }
}
