package com.base.orbit.utils;

import org.slf4j.Logger;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FileUtil {

    /**
     * 判断文件是否更新超过时间之后就返回false
     *
     * @param okFile  是否写入成功文件
     * @param massage 信息
     * @return boolean
     */
    public synchronized static boolean checkFileStatus(File okFile, String massage, Logger log) {
        boolean flag = false;
        boolean exists;
        do {
            exists = okFile.exists();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (!exists);
        FileInputStream fileInputStream;
        InputStreamReader inputStreamReader;
        BufferedReader bufferedReader;
        try {
            fileInputStream = new FileInputStream(okFile);
            inputStreamReader = new InputStreamReader(fileInputStream);
            bufferedReader = new BufferedReader(inputStreamReader);
            String result;
            while ((result = bufferedReader.readLine()) != null) {
                if (result.contains("success")) {
                    flag = true;
                } else {
                    log.error(massage + "ok文件读取出错");
                }
            }
            fileInputStream.close();
            inputStreamReader.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static List<String> getLineArray(String result) {
        List<String> r = new ArrayList<>();
        Arrays.stream(result.split(" ")).forEach(x -> {
            if (!"".equals(x)) {
                r.add(x);
            }
        });
        return r;
    }
}
