package com.brilliance.isc.common.util;

import java.util.*;

public class DocUtils {
    public static void putData(Map<String, List<String[]>> dataMap, String tempName, String... tempData) {
        if (dataMap.containsKey(tempName)) {
            List<String[]> list = dataMap.get(tempName);
            if (tempData == null) {
                tempData = new String[0];
            }
            list.add(tempData);
        } else {
            List<String[]> list = new ArrayList<>();
            list.add(tempData);
            dataMap.put(tempName, list);
        }
    }

    @SuppressWarnings("unused")
    public static Map<String, Object> getResult(Map<String, List<String[]>> dataMap) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> objectMap = buildObject(dataMap.keySet());
        Map<String, Object> fieldDiff = new HashMap<>();
        // 遍历 Map
        for (Map.Entry<String, List<String[]>> entry : dataMap.entrySet()) {
            String key = entry.getKey();
            if ("refLine".equals(key)) {
                List<Map<String, String>> mapList = new ArrayList<>();
                for (int i = 0; i < entry.getValue().size(); i++) {
                    Map<String, String> map = new HashMap<>();
                    map.put("argText", entry.getValue().get(i)[0]);
                    map.put("argValue", entry.getValue().get(i)[1]);
                    mapList.add(map);
                }
                result.put(key, mapList);
            } else if ("fieldDiff.intro".equals(key)) {
                String intro = entry.getValue().get(0)[0];
                fieldDiff.put("intro", intro);
            } else if ("fieldDiff.hdr".equals(key)) {
                String hdrDesc = entry.getValue().get(0)[0];
                String hdrTxtOld = entry.getValue().get(0)[1];
                String hdrTxtNew = entry.getValue().get(0)[2];
                fieldDiff.put("hdrDesc", hdrDesc);
                fieldDiff.put("hdrTxtOld", hdrTxtOld);
                fieldDiff.put("hdrTxtNew", hdrTxtNew);
            } else if ("fieldDiff.list".equals(key)) {
                List<Map<String, String>> list = new ArrayList<>();
                for (int i = 0; i < entry.getValue().size(); i++) {
                    Map<String, String> map = new HashMap<>();
                    map.put("desc", entry.getValue().get(i)[0]);
                    map.put("txtOld", entry.getValue().get(i)[1]);
                    map.put("txtNew", entry.getValue().get(i)[2]);
                    list.add(map);
                }
                fieldDiff.put("list", list);
            } else {
                List<String[]> value = entry.getValue();

                if (value.size() > 1) {
                    List<Object> list = new ArrayList<>();
                    for (String[] array : value) {
                        if (array.length > 1) {
                            list.add(convertArrayToMap(array));

                        } else if (array.length == 1) {
                            list.add(array[0]);
                        }
                    }
                    putObject(objectMap, key, list);
                } else if (value.size() == 1) {
                    String[] array = value.get(0);
                    if (array.length > 1) {
                        Map<String, Object> map = convertArrayToMap(array);
                        putObject(objectMap, key, map);
                    } else if (array.length == 1) {
                        putObject(objectMap, key, array[0]);
                    }
                }
            }
        }
        if (!fieldDiff.isEmpty()) {
            result.put("fieldDiff", fieldDiff);
        }
        result.putAll(objectMap);
        return result;
    }

    private static Map<String, Object> buildObject(Set<String> keySet) {
        Map<String, Object> a = new HashMap<>();
        Set<String> excludedSet = new HashSet<>(Arrays.asList("refLine", "fieldDiff.intro", "fieldDiff.hdr", "fieldDiff.list"));
//        keySet.removeAll(excludedSet);

        Set<String> newKeySet = new HashSet<>();
        for (String key : keySet) {
            if (!excludedSet.contains(key)) {
                newKeySet.add(key);
            }
        }

        for (String key : newKeySet) {
            String[] parts = key.split("\\.");
            Map<String, Object> currentObj = a;

            for (int i = 0; i < parts.length; i++) {
                String part = parts[i];

                if (!currentObj.containsKey(part)) {
                    if (i < parts.length - 1) {
                        Map<String, Object> newObj = new HashMap<>();
                        currentObj.put(part, newObj);
                        currentObj = newObj;
                    } else {
                        currentObj.put(part, null);
                    }
                } else {
                    Object existingObj = currentObj.get(part);
                    if (!(existingObj instanceof Map) && i < parts.length - 1) {
                        // 如果已存在的对象不是Map类型，而当前层级还未到达最后一层，则创建一个新的Map对象
                        Map<String, Object> newObj = new HashMap<>();
                        newObj.put(part, existingObj);
                        currentObj.put(part, newObj);
                        currentObj = newObj;
                    } else {
                        currentObj = Utils.convertObjectToMap(existingObj);
                    }
                }
            }
        }

        return a;
    }

    private static void putObject(Map<String, Object> objectMap, String key, Object value) {
        String[] parts = key.split("\\.");

        if (objectMap.get(parts[0]) == null) {
            objectMap.put(parts[0], value);
        } else {
            Map<String, Object> currentObj = Utils.convertObjectToMap(objectMap.get(parts[0]));
            objectMap.put(parts[0], currentObj);
            //遍历取出前一个对象
            for (int i = 1; i < parts.length - 1; i++) {
                String part = parts[i];
                Map<String, Object> temp = Utils.convertObjectToMap(currentObj.get(part));
                currentObj.put(part, temp);
                currentObj = temp;
            }
            currentObj.put(parts[parts.length - 1], value);
        }
    }

    private static Map<String, Object> convertArrayToMap(String[] array) {
        Map<String, Object> map = new HashMap<>();

        if (array.length > 1) {
            for (int i = 0; i < array.length; i++) {
                map.put("val" + i, array[i]);
            }
        }

        return map;
    }
}
