package com.zjty.autotest.util;

import com.zjty.autotest.pojo.sjq.FileBean;
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 {
    private  static List<String> routes=new ArrayList<>(80);
    private  static List<String> urls=new ArrayList<>(200);
    private  static HashMap<String,List<FileBean>> alls=new HashMap<>(200);

    public static HashMap<String,List<FileBean>> 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);
        }
    }
    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("//")) {
                        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 main(String[] args) {
        String str="import Vue from 'vue'\n" +
                "import Router from 'vue-router'\n" +
                "import Index from '@/Index'\n" +
                "import SocialIndex from '@/SocialIndex'\n" +
                "import SocialDetail from '@/SocialDetail'\n" +
                "import SocialPost from '@/SocialPost'\n" +
                "\n" +
                "Vue.use(Router)\n" +
                "\n" +
                "export default new Router({\n" +
                "  // mode: 'history',\n" +
                "  // hashbang: false,\n" +
                "  // history: false,\n" +
                "  routes: [\n" +
                "    {\n" +
                "      path: '/',\n" +
                "      name: 'index',\n" +
                "      component: Index,\n" +
                "      meta: {\n" +
                "        title: '这是个圈子',\n" +
                "        keepAlive: false\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      path: '/index',\n" +
                "      name: 'index',\n" +
                "      component: Index,\n" +
                "      meta: {\n" +
                "        title: '这是个圈子',\n" +
                "        keepAlive: false\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      path: '/circle/:circleId',\n" +
                "      name: 'circle',\n" +
                "      component: SocialIndex,\n" +
                "      meta: {\n" +
                "        title: '这是个圈子',\n" +
                "        keepAlive: false\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      path: '/post/:circleId/:postId/:onPage',\n" +
                "      name: 'post',\n" +
                "      component: SocialDetail,\n" +
                "      meta: {\n" +
                "        title: '这是个圈子',\n" +
                "        keepAlive: false\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      path: '/SocialPost/:circleId',\n" +
                "      name: 'SocialPost',\n" +
                "      component: SocialPost,\n" +
                "      meta: {\n" +
                "        title: '这是个圈子',\n" +
                "        keepAlive: false\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "})\n";
        Set<String> set=new HashSet<String>();
        Pattern p = Pattern.compile("(\\/:*[a-zA-Z0-9]*)+");
        Matcher m = p.matcher(str);
        while(m.find()){
            String substring = m.group().substring(0, m.group().length());
            if(!substring.equals("//")) {
                set.add(substring.toLowerCase());
            }
//            System.out.println(m.group().substring(0, m.group().length()));
        }
        for (String o : set) {
            System.out.println(o);
        }
    }
    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().equalsIgnoreCase("img")||!files[i].getName().equalsIgnoreCase("plugins")
                            ||!files[i].getName().equalsIgnoreCase("css")||!files[i].getName().equalsIgnoreCase("node_modules")
                    ) {
//                        System.out.println("routes:" + files[i].getPath());
                        getFiles(files[i].getPath(), files[i].getName());
                    }
                } else {
                    if(alls.get(dirName)!=null){
                        List<FileBean> fileBeans = alls.get(dirName);
                        FileBean fileBean=new FileBean();
                        fileBean.setFileType(files[i].getName());
                        fileBean.setUrl(files[i].getAbsolutePath());
                        fileBeans.add(fileBean);
                    }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")) {
                            List<FileBean> fileBeans = new ArrayList<>();
                            FileBean fileBean = new FileBean();
                            fileBean.setFileType(files[i].getName());
                            fileBean.setUrl(files[i].getAbsolutePath());
                            fileBeans.add(fileBean);
                            alls.put(dirName, fileBeans);
//                            System.out.println("文件名：" + files[i].getName());
//                            System.out.println("文件：" + files[i].getAbsolutePath());
                        }
                    }
                }

            }
        } else {
            System.out.println("文件1：" + file.getPath());
        }
    }
    public static String 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().equalsIgnoreCase("img") ||!files[i].getName().equalsIgnoreCase("css")
                            ||!files[i].getName().equalsIgnoreCase("node_modules")){
                        if(files[i].getName().contains("angular")){
                            return "angular";
                        }
                        if(files[i].getName().contains("vue")){
                            return "vue";
                        }
                        getFileRoutes(files[i].getPath());
                    }

                } else {
                    int pos = files[i].getName().lastIndexOf(".");
                    String extName = files[i].getName().substring(pos + 1).toLowerCase();
                    if(extName.equals("vue")){
                        return "vue";
                    }
                    if(extName.equals("jsp")){
                        return "jsp";
                    }
                }

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


}
