package com.persagy.bdtp.adm.test.data;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.persagy.adm.server.custom.entity.db.AdmRelsConfig;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RelCfgs {

    public static void main(String[] args) throws Exception {
        new RelCfgs().read("ff");
    }

    private ObjectMapper objectMapper = new ObjectMapper();;
    private List<AdmRelsConfig> list = new ArrayList<>();
    private AdmRelsConfig cfg;
    private String classCodeFrom;
    private String graphCode;
    private String graphName;
    private String major;
    private List<Map> graphDef;
    private List<Map> relDef;
    private List<Map> relsDef;

    public List<AdmRelsConfig> read(String type){
        major = type.toUpperCase();
        try{
            graphDef = objectMapper.readValue(new File("D:\\work_area\\adm_1015\\graph_def.json"), List.class);
            relDef = objectMapper.readValue(new File("D:\\work_area\\adm_1015\\rel_def.json"), List.class);
            List<Map> list = objectMapper.readValue(new File("D:\\work_area\\adm_1015\\rels_def.json"), List.class);
            relsDef = new ArrayList<>();
            for(Map m : list){
                relsDef.addAll((List)m.get("children"));
            }
        }catch (Exception e){

        }
        try (BufferedReader r = new BufferedReader(new FileReader("D:\\work_area\\adm_1015\\" + type + "_rels.txt"))){
            String tmp;
            while ((tmp = r.readLine()) != null){
                readLine(tmp);
            }

            String json = objectMapper.writeValueAsString(list);
            System.out.println(json);

            return list;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void readLine(String line){
        if(StrUtil.isBlank(line))
            return;
        int preS = preS(line);
        String[] arr = line.trim().split("\\s+");
        readLine(preS, arr);
    }

    private void readLine(int preS, String[] arr) {
        if (preS <= 3)
            classCodeFrom = arr[3 - preS];
        if (preS <= 4) {
            graphName = arr[4 - preS];
            graphCode = findGraphCode(graphName);
        }

        cfg = new AdmRelsConfig();
        cfg.setId(IdUtil.simpleUUID());
        cfg.setObjTypeFrom("equipment");
        cfg.setClassCodeFrom(classCodeFrom);
        cfg.setGraphCode(graphCode);
        cfg.setGraphName(graphName);
        cfg.setRelCode(findRelCode(arr[5 - preS], graphCode));
        cfg.setRelName(arr[5 - preS]);
        if(arr.length >= 8 - preS)
            cfg.setClassCodeTo(arr[7 - preS]);
        if(cfg.getClassCodeTo() != null){
            switch (cfg.getClassCodeTo()){
                case "CMZ":
                    cfg.setClassCodeTo("GeneralZone");
                    break;
                case "ACZ":
                    cfg.setClassCodeTo("AirConditioningZone");
                    break;
                //TODO
            }
            if(cfg.getClassCodeFrom().length() == 4){
                if(cfg.getClassCodeTo().length() == 2)
                    cfg.setObjTypeTo("system");
                else if(cfg.getClassCodeTo().length() == 4)
                    cfg.setObjTypeTo("equipment");
            } else if(cfg.getClassCodeFrom().length() == 6){
                if(cfg.getClassCodeTo().length() == 4)
                    cfg.setObjTypeTo("system");
                else if(cfg.getClassCodeTo().length() == 6)
                    cfg.setObjTypeTo("equipment");
            }
            if(cfg.getObjTypeTo() == null) {
                if(cfg.getRelCode().endsWith("2Fl"))
                    cfg.setObjTypeTo("floor");
                else if(cfg.getRelCode().endsWith("2Sh"))
                    cfg.setObjTypeTo("shaft");
                else if(cfg.getRelCode().endsWith("2Sp"))
                    cfg.setObjTypeTo("space");
                else {
                    cfg.setObjTypeTo("unknown"); //TODO
                }
            }
            list.add(cfg);
        } else {
            //TODO
        }
    }

    private String findGraphCode(String graphName){
        for(Map map : graphDef) {
            if(graphName.equals(map.get("name")))
                return (String) map.get("code");
        }
        for (Map map : relsDef) {
            if(graphName.equals(map.get("name")))
                return (String) map.get("code");
        }
        return graphName;
    }

    private String findRelCode(String relName, String graphCode){
        for(Map map : relDef) {
            if(relName.equals(map.get("name")))
                return (String) map.get("code");
        }
        for (Map map : relsDef) {
            if(graphCode.equals(map.get("code"))) {
                List<Map> children = (List)map.get("children");
                for(Map cm : children) {
                    if(relName.equals(cm.get("code")))
                        return (String) cm.get("name");
                }
            }
        }
        return relName;
    }

    private int preS(String line){
        int s = 0;
        while (s < line.length()){
            if(!Character.isWhitespace(line.charAt(s)))
                break;
            s++;
        }
        return s;
    }

}
