package com.brilliance.isc.swift.objmtconvert.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.util.MessageUtil;
import com.brilliance.swift.util.StringUtil;
import com.prowidesoftware.swift.model.UETRUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MxTransfer {

    public static String transfer(String gsonStr) {
        Map<String, Object> gsonMaps = JSON.parseObject(gsonStr);
        String identifier = (String) gsonMaps.get("identifier");
        identifier = identifier.replace(".", "");
        if (gsonMaps.get("bizMsgIdr") == null) {
            String uetr = UETRUtils.generate();
            gsonMaps.put("uetr", uetr);
            String bizMsgIdr = "MX" + System.nanoTime();
            if (bizMsgIdr.length() > 16) {
                bizMsgIdr = bizMsgIdr.substring(0, 16);
            }
            gsonMaps.put("bizMsgIdr", bizMsgIdr);
        }
        if (gsonMaps.get("uetr") == null) {
            String uetr = UETRUtils.generate();
            gsonMaps.put("uetr", uetr);
        }
        InputStream resourceAsStream = MessageUtil.class.getResourceAsStream("/templates/" + identifier + ".properties");
        if (resourceAsStream == null) {
            throw new SwiftException("没找到对应的配置文件" + identifier);
        }
        List<String> properties = StringUtil.inputStreamToLines(resourceAsStream);
        Map<String, Object> maps = new HashMap<>();
        for (String property : properties) {
            if (property.startsWith("#")) {
                continue;//过滤注释
            }
            String[] strArr = property.split("=");
            if (strArr.length != 2) {
                continue;//过滤掉不符合规则的配置
            }
            String key = strArr[0].trim();
            String path = strArr[1].trim();
            String type = "";
            if (path.indexOf("@") > -1) {
                type = path.substring(path.indexOf("@") + 1);
                path = path.substring(0, path.indexOf("@"));
            }
            Object value = gsonMaps.get(path);
            if (value == null) continue;
            putValue(maps, key, value);
        }
        String swiftGsonStr = new JSONObject(maps).toJSONString();
        return swiftGsonStr;
    }

    //解析key的值，循环遍历，将value放进maps里面
    public static void putValue(Map<String, Object> maps, String key, Object value) {
        String[] keys = key.split("\\.");
        for (int i = 0; i < keys.length; i++) {
            String tmpKey = keys[i];
            if (i == (keys.length - 1)) {
                maps.put(tmpKey, value);
            } else {
                boolean jsonArrayFlag = false;
                if (tmpKey.endsWith("*")) { //表示此节点是JSONARRAY,
                    tmpKey = tmpKey.substring(0, tmpKey.indexOf("*"));
                    jsonArrayFlag = true;
                }
                Object obj = maps.get(tmpKey);
                if (obj == null) { //如果目标maps没有值，则新建一个新的map，再循环遍历
                    if (jsonArrayFlag) {
                        JSONArray jsonArray = new JSONArray();
                        maps.put(tmpKey, jsonArray);
                        maps = new HashMap<>();
                        jsonArray.add(maps);
                    } else {
                        Map<String, Object> tmpMaps = new HashMap<>();
                        maps.put(tmpKey, tmpMaps);
                        maps = tmpMaps;
                    }
                } else {
                    if (jsonArrayFlag) {
                        JSONArray jsonArray = (JSONArray) obj;
                        maps = (Map<String, Object>) jsonArray.get(0);
                    } else {
                        maps = (Map<String, Object>) obj;
                    }
                }
            }
        }
    }


}
