package net.northking.cctp.element.internal.protocol.socket.cups8583.internal;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * CsvCupsFieldDefReader
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
public class CsvCupsFieldDefReader {

    private static final Logger logger = LoggerFactory.getLogger(CsvCupsFieldDefReader.class);

    public static final TreeMap<String, CupsField> defMap = new TreeMap<>(new CupsFieldKeyComparator());

    static {
        try {
            readConfig();
        } catch (Exception e) {
            logger.error("初始化读取CUPS配置文件失败", e);
        }
    }

    private static void readConfig() throws Exception {
        InputStream in = CsvCupsFieldDefReader.class.getClassLoader().getResourceAsStream("config_cups.csv");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
        int i = 0;
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                i++;
                if (i == 1) {
                    continue;
                }
                line = line.trim();
                if (line.length() > 0) {
                    String[] values = line.split(",");
                    CupsField field = new CupsField();
                    field.key = values[0];
                    field.code = values[0];
                    field.chName = values[1];
                    field.fieldType = CupsFieldType.valueOf(values[2]);
                    field.length = Integer.parseInt(values[3]);
                    field.dataType = CupsDataType.valueOf(values[4]);
                    defMap.put(field.code, field);

                    // 是否子域
                    int p = field.key.indexOf(".");
                    if (p != -1) {
                        field.parentKey = field.key.substring(0, p + 1);
                        defMap.get(field.parentKey).addChildField(field);
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("config_cups配置文件第" + i + "行解析错误", e);
        }
    }

    public static void printDefJson() {
        System.out.println(JSON.toJSONString(defMap.values()));
    }

    public static class CupsFieldKeyComparator implements Comparator<String> {

        @Override
        public int compare(String key1, String key2) {
            if (StringUtils.equals(key1, key2)) {
                return 0;
            }
            String[] v1Parts = StringUtils.split(key1, ".");
            String[] v2Parts = StringUtils.split(key2, ".");
            int min = Math.min(v1Parts.length, v2Parts.length);
            for (int i = 0; i < min; i++) {
                int n = Integer.parseInt(v1Parts[i]) - Integer.parseInt(v2Parts[i]);
                if (n != 0) {
                    return n;
                }
            }
            return v1Parts.length - v2Parts.length;
        }
    }

    public static void main(String[] args) {
        printDefJson();
    }

}
