package org.jeecg.modules.stm.utils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class DirectoryList {
    private List<DirectoryItem> items;
    public DirectoryList() {
        items = new ArrayList<>();
    }
    public void addDirectoryItem(DirectoryItem item) {
        items.add(item);
    }
    public List<DirectoryItem> getItems() {
        return items;
    }
    private static List<String> readDocumentDirectory(String filePath) {
        List<String> directoryList = new ArrayList<>();
        String strTxt = pdfParserText(filePath);
        String[] arr = strTxt.split("\r\n");
        try {
            Pattern pattern = Pattern.compile("^\\d+\\.?\\d*\\s(.+)");
            boolean flag = false;
            for (String line : arr) {
                if (line.trim().equals("第二部分  科技管理")) {
                    flag = true;
                    continue;
                }
                if (line.trim().isEmpty() || !flag) {
                    continue;
                }
//                if (StringUtils.isNumeric(line.trim())) {
//                    int k = Integer.parseInt(line.trim());
//                    if (k == 158) {
//                        break;
//                    }
//                    continue;
//                }
                line = line.trim();
                directoryList.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return directoryList;
    }

    private static String pdfParserText(String filePath){
        try {
            File file = new File(filePath);
            PDDocument document = PDDocument.load(file);
            PDFTextStripper stripper = new PDFTextStripper();
            String str = stripper.getText(document);
            document.close();
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<DirectoryItem> getDataFromFile(String filePath) {

        DirectoryList directoryList = new DirectoryList();
        DirectoryItem currentItem = null;
       // String filePath = "d://abc/银行业金融机构信息科技风险监管现场检查手册.pdf";
        List<String> lines = readDocumentDirectory(filePath);
        int flag=0;
        int k=0;
        String subDomain = null;
        for (int i=0;i<lines.size();i++) {
            String line = lines.get(i);

            if (line.matches("^\\d+\\.\\s.*")) {     //领域 1级
                if (currentItem != null) {
                    directoryList.addDirectoryItem(currentItem);
                }
                currentItem = new DirectoryItem();
                currentItem.setTitle(line);
                subDomain = null;
                flag=0;
            } else if (line.matches("^\\d+\\.\\d\\s.*")) { //领域 2级
                Map<String,List<String[]>> tmpMap = currentItem.getSubItems();
                List<String[]> tmpListAry = new ArrayList<>();
                tmpMap.put(line,tmpListAry);
                subDomain = line;
                flag =1;
            } else if (line.matches("^\\s*检查项\\s*\\d*.*")) {
                if(subDomain==null) {
                    Map<String,List<String[]>> tmpMap = currentItem.getSubItems();
                    List<String[]> tmpListAry = new ArrayList<>();
                    tmpMap.put(currentItem.getTitle(),tmpListAry);
                    subDomain = currentItem.getTitle();
                    flag =1;
                }
                List<String[]> tmpList = currentItem.getSubItems().get(subDomain);
                if(tmpList==null) {
                    tmpList = new ArrayList<>();
                }
                String[] tmpAry = new String[3];
                tmpAry[0] = line;
                tmpAry[1] = "";
                tmpAry[2] = "";
                tmpList.add(tmpAry);;
                currentItem.getSubItems().put(subDomain,tmpList);
                k=0;
            } else if (line.matches("^\\s*基本要求.*：\\d*.*")) {
                List<String[]> tmpList = currentItem.getSubItems().get(subDomain);
                if(tmpList!=null) {
                    int len = tmpList.size();
                    tmpList.get(len - 1)[1] = line;
                }
                k=1;
            } else if (line.matches("^\\s*检查方法\\d*.*")) {
                List<String[]> tmpList = currentItem.getSubItems().get(subDomain);
                if(tmpList!=null) {
                    int len = tmpList.size();
                    tmpList.get(len-1)[2]= line;
                }

                k=2;
            } else {
                if(flag==0) {
                    if(currentItem==null)continue;
                    String tmpStr = currentItem.getContent();
                    tmpStr = tmpStr==null?line:tmpStr+line;
                    currentItem.setContent(tmpStr);
                } else {
                    List<String[]> tmpList = currentItem.getSubItems().get(subDomain);
                    if(tmpList!=null) {
                        int len = tmpList.size();
                        if(len>0) {
                            tmpList.get(len-1)[k] += line;
                        }
                    }

                }

            }
        }
        if (currentItem != null) {
            directoryList.addDirectoryItem(currentItem);
        }
        return directoryList.getItems();

    }


//        for (DirectoryItem item : directoryList.getItems()) {
//            System.out.println(item.getTitle());
//            System.out.println(item.getContent());
//            Map<String,List<String[]>> tmpMapCur = item.getSubItems();
//            for (String key : tmpMapCur.keySet()) {
//                System.out.println(key);
//                List<String[]> valueList = tmpMapCur.get(key);
//                if(valueList == null) continue;
//                for(int j=0;j<valueList.size();j++){
//                    String[] vals = valueList.get(j);
//                    System.out.println(vals[0]);
//                    System.out.println(vals[1]);
//                    System.out.println(vals[2]);
//                }
//            }
//
//        }

}