package com.brilliance.mda.runtime.mda.util;

import com.brilliance.mda.runtime.mda.IStream;
import com.brilliance.mda.runtime.mda.config.IniConfig;
import com.brilliance.mda.runtime.mda.impl.StreamImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

import static com.brilliance.mda.runtime.mda.Constants.NULLSTR;

public class Inis {

    //获取INI配置文件中的配置项
    public static String getIniStr(String iniName, String block, String key) {
        if (iniName == null || iniName.length() == 0){
            return NULLSTR;
        }
        return IniConfig.getIniConfig(iniName, block, key);
    }

    public static void setIniStr(String iniName, String block, String key, String value) {
        if (iniName == null || iniName.length() == 0) {
            return;
        }
        IniConfig iniConfig = IniConfig.getIniConfigMap().get(iniName.toUpperCase());
        if (iniConfig == null) {
            return;
        } else {
            iniConfig.set(block, key, value);
        }
    }

    public static String getIniBlock(String iniName, String block, String key) {
        if (iniName == null || iniName.length() == 0) {
            return "";
        }
        IniConfig iniConfig = IniConfig.getIniConfigMap().get(iniName.toUpperCase());
        if(iniConfig == null){
            return "";
        }
        Map<String,String> sectionData = iniConfig.getData().get(block.toUpperCase());
        if(sectionData == null){
            return "";
        }
        List<String> keyList = new ArrayList<>();
        StringJoiner sj = new StringJoiner("\n");
        for(Map.Entry<String,String> entry: sectionData.entrySet()){
            if(!entry.getKey().toLowerCase().startsWith(key.toLowerCase())) {
                continue;
            }
            String idxStr = entry.getKey().substring(key.length());
            if(idxStr.matches("\\d+")){
                int idx = Integer.parseInt(idxStr);
                while(idx >= keyList.size()){
                    keyList.add(null);
                }
                keyList.set(idx,entry.getValue());
            }
        }
        for (String s : keyList) {
            if(s!=null){
                sj.add(s);
            }
        }
        return sj.toString();
    }

    public static int getIniInt(String iniName, String block, String key) {
        if (iniName == null || iniName.length() == 0){
            return -1;
        }
        String val = IniConfig.getIniConfig(iniName, block, key);
        if (val == null || val.length() == 0){
            return -1;
        }
        return Integer.parseInt(val);
    }

    public static void setIniInt(String iniName, String block, String key, int value) {
        if (iniName == null || iniName.length() == 0){
            return;
        }
        IniConfig.getIniConfigMap().get(iniName.toUpperCase()).set(block, key, value+"");
    }

    public static IStream getIniSection(String iniName, String s1) {
        IStream iStream = new StreamImpl();
        if (iniName == null || iniName.length() == 0 || iniName.indexOf(".") < 0) {
            return iStream;
        }
        IniConfig iniConfig = IniConfig.getIniConfigMap().get(iniName.toUpperCase());
        if (iniConfig == null) {
            return iStream;
        } else {
            if (iniConfig.getData().containsKey(s1)) {
                iStream.setRows(new ArrayList<>(iniConfig.getData().keySet()));
            }
            return iStream;
        }
    }

    public static void setIniBlock(String iniName, String s2, String s3, String block) {
        if (iniName == null || iniName.length() == 0) {
            return;
        }
        IniConfig iniConfig = IniConfig.getIniConfigMap().get(iniName.toUpperCase());
        if (iniConfig == null) {
            return;
        } else {
            iniConfig.getData().remove(s2);
            iniConfig.getData().put(s2, iniConfig.getData().get(block));
        }
    }
}
