package com.zjty.autotest.util;

import com.zjty.autotest.pojo.sjq.FileBean;
import com.zjty.autotest.pojo.sjq.FileType;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class FileHtmlUtil {
    public static int count = 0;
    public static int angularNum = 0;
    public static int jsNum = 0;
    public static int jspNum = 0;
    public static int vueNum = 0;
    private static List<String> routes = new ArrayList<>(80);
    private static List<String> urls = new ArrayList<>(200);
    private static HashMap<String, List<FileType>> alls = new HashMap<>(200);

    public static HashMap<String, List<FileType>> getAlls() {
        return alls;
    }

    public static void clearAll() {
        if (routes != null) {
            routes.clear();
            System.out.println("routes:=" + routes);
        }
        if (urls != null) {
            urls.clear();
            System.out.println("urls:=" + urls);
        }
        if (alls != null) {
            alls.clear();
            System.out.println("alls:=" + alls);
        }
        count = 0;
        jspNum = 0;
        jsNum = 0;
        vueNum = 0;
        angularNum = 0;
    }

    public static List<String> readTxtFile(String filePath, String pattern) {
        Pattern p;
        Matcher m;
        Set<String> set = new HashSet<String>();
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);// 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                StringBuilder sb = new StringBuilder();
                if (pattern != null) {
                    while ((lineTxt = bufferedReader.readLine()) != null) {
                        sb.append(lineTxt);
                    }
                }
                String s = sb.toString();
                p = Pattern.compile(pattern);
                m = p.matcher(s);
                while (m.find()) {
                    String pp = m.group().substring(0, m.group().length());
                    if (!pp.equals("//")&&!pp.contains("<")) {
                        set.add(pp.toLowerCase());
                    }
                }
                List<String> collect = set.stream().collect(Collectors.toList());
                read.close();

                return collect;
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return null;
    }


    public static void getFiles(String path, String dirName) {

        File file = new File(path);
        // 如果这个路径是文件夹
        if (file.isDirectory()) {
            // 获取路径下的所有文件
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                // 如果还是文件夹 递归获取里面的文件 文件夹
                if (files[i].isDirectory()) {
                    if (files[i].getName().contains("img") || files[i].getName().contains("plugins")
                            || files[i].getName().contains("css") || files[i].getName().contains("node_modules")) {
                        continue;
                    } else {
                        System.out.println("文件夹:" + files[i].getName());
                        getFiles(files[i].getPath(), files[i].getName());
                    }
                } else {

                    int pos = files[i].getName().lastIndexOf(".");
                    String extName = files[i].getName().substring(pos + 1).toLowerCase();
                    if (extName.equals("js") || extName.equals("html") || extName.equals("jsp")) {
                        System.out.println("文件:" + files[i].getName());
                        if (alls.get(dirName) != null) {
                            List<FileType> fileBeans = alls.get(dirName);
                            FileType fileBean = new FileType();
                            fileBean.setFileName(files[i].getName());
                            fileBean.setAddress(files[i].getAbsolutePath());
                            fileBean.setSuffix(extName);
                            fileBeans.add(fileBean);
                        } else {
                            List<FileType> fileBeans = new ArrayList<>();
                            FileType fileBean = new FileType();
                            fileBean.setFileName(files[i].getName());
                            fileBean.setAddress(files[i].getAbsolutePath());
                            fileBean.setSuffix(extName);
                            fileBeans.add(fileBean);
                            alls.put(dirName, fileBeans);
                        }
                    }
                }

            }
        } else {
            System.out.println("文件1：" + file.getPath());
        }
    }


    public static void getFileRoutes(String path) {

        File file = new File(path);
        // 如果这个路径是文件夹
        if (file.isDirectory()) {
            // 获取路径下的所有文件
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                // 如果还是文件夹 递归获取里面的文件 文件夹
                if (files[i].isDirectory()) {
                    if (files[i].getName().contains("img") || files[i].getName().contains("css")
                            || files[i].getName().contains("node_modules")) {
                        continue;
                    } else {
                        if (files[i].getName().contains("angular")) {
                            angularNum++;
                        }
//                        System.out.println("文件夹:"+files[i].getName());
                        getFileRoutes(files[i].getPath());
                    }

                } else {
//                    System.out.println("文件:"+files[i].getName());
                    int pos = files[i].getName().lastIndexOf(".");
                    String extName = files[i].getName().substring(pos + 1).toLowerCase();
                    if (extName.equals("vue")) {
                        vueNum++;
                    } else if (extName.equals("jsp")) {
                        jspNum++;
                    } else if (extName.equals("html")) {
                        count++;
                    } else if (extName.equals("js")) {
                        jsNum++;
                    }
                }

            }
        } else {

            System.out.println("文件：" + file.getPath());
        }
    }


}
