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.common.AdmConst;
import com.persagy.adm.server.custom.entity.db.AdmPipeConfig;

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

public class PipeCfgs {

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

    private ObjectMapper objectMapper = new ObjectMapper();;
    private List<AdmPipeConfig> list = new ArrayList<>();
    private AdmPipeConfig cfg;
    private String classCodeFrom;
    private String pipeClassCode;
    private String pipeSubType;
    private String major;


    public List<AdmPipeConfig> read(String type){
        major = type.toUpperCase();

        try (BufferedReader r = new BufferedReader(new FileReader("D:\\work_area\\adm_1015\\" + type + "_pipe.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];

        cfg = new AdmPipeConfig();
        cfg.setId(IdUtil.simpleUUID());
        cfg.setClassCodeFrom(classCodeFrom);

        //debug
        if(7 - preS >= arr.length)
            System.out.println();

        if (preS <= 4)
            pipeClassCode = getPipeClassCode(arr[4 - preS]);
        if (preS <= 5)
            pipeSubType = arr[5 - preS];

        cfg.setPipeType(pipeClassCode);
        cfg.setPipeSubType(pipeSubType);
        cfg.setClassCodeTo(arr[7 - preS]);

        //debug
        if(8 - preS >= arr.length)
            System.out.println();

        cfg.setPipeDirection(getDirection(arr[8 - preS]));

        list.add(cfg);
    }

    private String getPipeClassCode(String name){
        if("液体管道".equals(name))
            return "OTLAPL";
        if("气体管道".equals(name))
            return "OTAAPL";
        return name;
    }

    private int getDirection(String label) {
        if("→".equals(label))
            return AdmConst.PIPE_DIRECTION_FORWARD;
        if("←".equals(label))
            return AdmConst.PIPE_DIRECTION_BACK;
        return AdmConst.PIPE_DIRECTION_NONE;
    }

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

}
