package com.jiayue.ipfcst.client.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 操作文件工具类
 *
 * @author tl
 * @date 2022-05-11 10:08:21
 */
@Slf4j
public class FileUtil {
    /*按⾏读取txt⽂件并返回⼀个List<String>集合
     *@param path
     *@return List<String>
     */
    public static List<String> getFileContent(File file) throws Exception {
        String encodingType = EfileUtil.getCharset(file);
        List<String> strList = new ArrayList();

        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            //将字节流向字符流转换
            inputStreamReader = new InputStreamReader(fileInputStream, encodingType);
            //创建字符流缓冲区
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            //按⾏读取
            while ((line = bufferedReader.readLine()) != null) {
                strList.add(line);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return strList;
        }
    }


    public static String getParsingPath() {
        return createParsingDir("parsing");
    }

    private static String createParsingDir(String dir) {
        String path = "";
        if (System.getProperties().getProperty("file.separator").equals("\\")) {
            path = new File(getResourceBasePath()).getParentFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + dir;
            try {
                path = URLDecoder.decode(path, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            File file = new File(path);
            if (!file.exists()) {
                boolean b = file.mkdirs();
                if (!b)
                    log.error("目录创建失败" + path);
            }
        } else {
            path = "/home/syjy/ipp/" + dir;
        }
        return path;
    }

    /**
     * 获取项目根路径
     *
     * @return
     */
    public static String getResourceBasePath() {
        // 获取跟目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());

        } catch (FileNotFoundException e) {
            // nothing to do
        }
        if (path == null || !path.exists()) {
            path = new File("");
        }

        String pathStr = path.getAbsolutePath();
        try {
            pathStr = URLDecoder.decode(pathStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 如果是在eclipse中运行，则和target同级目录,如果是jar部署到服务器，则默认和jar包同级
//    pathStr = pathStr.replace("\\target\\classes", "");

        return pathStr;
    }


    //把一个文件中的内容读取成一个String字符串
    public static String getStr(File jsonFile){
        String jsonStr = "";
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
