package cn.edu.whu.boot.common.utils.xml;

import cn.edu.whu.boot.common.domain.CommonConstant;
import cn.edu.whu.boot.xml.dto.*;
import cn.edu.whu.boot.xml.xmlreader.XmlTemplateReader;
import cn.edu.whu.boot.xml.xmlreader.bean.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

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

/**
 * 通过导入 XML 文件来生成 Task 的时候，需要将 XML 解析成前端需要的字符串!!!
 * 该工具类就是提供该方法!!!
 */
public class XmlFileToJsonUtil{

    /**
     * 读取 XML 文件生成前端需要的字符串
     */
    public static String xmlReader(String xmlFilePath){
        Config config = XmlTemplateReader.read(xmlFilePath);
        FrontEndConfig frontEndConfig = new FrontEndConfig();
        // 构建 FrontEndMeta 类作为 frontEndConfig的 meta 属性
        Meta meta = config.getMeta();
        FrontEndMeta frontEndMeta = new FrontEndMeta();
//        List<String> drawTool = meta.getDrawTool();
        Map<String, List<String>> drawTool = meta.getDrawTool();
//        List<Map<String, String>> frontDrawTool = new ArrayList<>();
        Map<String, List<Map<String, String>>> frontDrawTool = new HashMap<>();
//        for(String tool : drawTool){
//            Map<String, String> toolMap = new HashMap<>();
//            toolMap.put(CommonConstant.NAME, tool);
//            frontDrawTool.add(toolMap);
//        }
        for(String group:drawTool.keySet()){
            List<Map<String, String>> groupMap = new ArrayList<>();
            for(String tool : drawTool.get(group)){
                Map<String, String> toolMap = new HashMap<>();
                toolMap.put(CommonConstant.NAME, tool);
                groupMap.add(toolMap);
            }
            frontDrawTool.put(group, groupMap);
        }
        frontEndMeta.setDrawtool(frontDrawTool);
        List<String> position = meta.getPosition();
        List<Map<String, String>> frontPosition = new ArrayList<>();
        for(String pos : position){
            Map<String, String> posMap = new HashMap<>();
            posMap.put(CommonConstant.NAME, pos);
            frontPosition.add(posMap);
        }
        frontEndMeta.setPosition(frontPosition);
        frontEndMeta.setBasemap(meta.getBaseMap());
        frontEndMeta.setOperationLayers(meta.getOperationLayers());
        frontEndConfig.setMeta(frontEndMeta);

        // 构建List<FrontEndTable>作为frontEndConfig的tables属性
        List<FrontEndTable> frontEndTables = new ArrayList<>();
        Map<String, Table> tables = config.getTables();
        for(Map.Entry<String, Table> tableEntry : tables.entrySet()){
            FrontEndTable frontEndTable = new FrontEndTable();
            frontEndTable.setKey(tableEntry.getValue().getKey());
            frontEndTable.setName(tableEntry.getValue().getName());
            frontEndTable.setRoot(tableEntry.getValue().isRoot());
            frontEndTable.setDrawTool(tableEntry.getValue().getDrawTool());
            List<FrontEndField> frontEndFieldList = new ArrayList<>();
            for(Map.Entry<String, Field> fieldEntry : tableEntry.getValue().getFields().entrySet()){
                Field field = fieldEntry.getValue();
                FrontEndField frontEndField = JSON.toJavaObject(
                        JSON.parseObject(JSON.toJSONString(field, SerializerFeature.DisableCircularReferenceDetect)),
                        FrontEndField.class);

                List<FrontEndSelect> frontEndSelects = new ArrayList<>();
                List<Map<String, String>> frontEndGroupList = new ArrayList<>();
                if(field.getGroups() != null && field.getGroups().size() != 0){
                    for(Map.Entry<String, Group> groupEntry : field.getGroups().entrySet()){
                        Group group = groupEntry.getValue();
                        Map<String, String> tempFrontEndGroupMap = new HashMap<>();
                        tempFrontEndGroupMap.put("group", group.getKey());
                        group.getSelects().forEach(select -> {
                            FrontEndSelect frontEndSelect = new FrontEndSelect();
                            frontEndSelect.setGroup(group.getKey());
                            frontEndSelect.setKey(select.getKey());
                            frontEndSelect.setValue(select.getValue());
                            frontEndSelects.add(frontEndSelect);
                        });
                        frontEndGroupList.add(tempFrontEndGroupMap);
                    }
                }
                if(field.getSelects() != null && field.getSelects().size() != 0){
                    for(Map.Entry<String, Select> selectEntry : field.getSelects().entrySet()){
                        FrontEndSelect frontEndSelect = new FrontEndSelect();
                        frontEndSelect.setGroup("");
                        frontEndSelect.setKey(selectEntry.getValue().getKey());
                        frontEndSelect.setValue(selectEntry.getValue().getValue());
                        frontEndSelects.add(frontEndSelect);
                    }
                }
                frontEndField.setGroup(frontEndGroupList);
                frontEndField.setSelector(frontEndSelects);
                frontEndFieldList.add(frontEndField);
                // 对frontEndFieldList进行排序，按照其中Field中的index即安卓端显示顺序
                frontEndFieldList.sort((o1, o2) -> {
                    if(Integer.valueOf(o1.getIndex()) < Integer.valueOf(o2.getIndex())){
                        return -1;
                    }
                    else if(o1.getIndex().equals(o2.getIndex())){
                        return 0;
                    }
                    else{
                        return 1;
                    }
                });
            }
            frontEndTable.setFields(frontEndFieldList);
            frontEndTables.add(frontEndTable);
        }
        frontEndConfig.setTables(frontEndTables);
        return JSON.toJSONString(frontEndConfig, SerializerFeature.DisableCircularReferenceDetect);
    }
}
