package com.qmth.exam.reserve.util;

import com.qmth.boot.core.exception.StatusException;
import com.qmth.exam.reserve.bean.Constants;
import com.qmth.exam.reserve.enums.FileUploadType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.file.Files;
import java.text.MessageFormat;
import java.util.Date;
import java.util.StringJoiner;

public class FileUtil {
    private static final Logger log = LoggerFactory.getLogger(FileUtil.class);

    public static String readFileContent(InputStream in) {
        StringBuilder content = new StringBuilder();
        InputStreamReader streamReader = null;
        BufferedReader bufferedReader = null;
        try {
            String encoding = "UTF-8";
            streamReader = new InputStreamReader(in, encoding);
            bufferedReader = new BufferedReader(streamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line);
            }
            return content.toString();
        } catch (IOException e) {
            throw new StatusException("出错", e);
        } finally {
            if (streamReader != null) {
                try {
                    streamReader.close();
                } catch (IOException ignored) {
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ignored) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            }
        }
    }

    /**
     * 获得文件的byte数组
     * 
     * @param filePath 文件路径
     * @return 获得文件的byte数组
     */
    public static byte[] getBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        }

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(((int) file.length()))) {
            BufferedInputStream in = null;
            in = new BufferedInputStream(Files.newInputStream(file.toPath()));
            int bufSize = 1024;
            byte[] buffer = new byte[bufSize];
            int len = 0;
            while (-1 != (len = in.read(buffer, 0, bufSize))) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        }
    }

    public static File getFileTemp(String suffix) throws IOException {
        File fileTmpDir = new File(System.getProperty(Constants.TMP_DIR));
        if (!fileTmpDir.exists()) {
            boolean flag = fileTmpDir.mkdirs();
            if (!flag) {
                log.warn("创建临时目录失败");
                return null;
            }
        }
        File file = File.createTempFile(Constants.TEMP_FILE_SUFFIX, suffix);
        log.info("getFileTemp_absolutePath:{}", file.getAbsolutePath());
        return file;
    }

    public static File getDir(String dirPath) {
        File fileTmpDir = new File(System.getProperty(Constants.TMP_DIR) + File.separator + dirPath);
        if (!fileTmpDir.exists()) {
            boolean flag = fileTmpDir.mkdirs();
            if (!flag) {
                log.warn("创建临时目录失败");
                return null;
            }
        }
        return fileTmpDir;
    }

    public static StringJoiner getDirName(FileUploadType fileUploadType, boolean fileSeparate) {
        StringJoiner stringJoiner = new StringJoiner("");
        stringJoiner.add(MessageFormat.format("{0}{1}{2}", fileUploadType.getTitle(), File.separator,
                DateUtil.format(new Date(), DateUtil.ShortDateStringWithSplit)));
        if (fileSeparate) {
            stringJoiner.add(File.separator);
        }
        return stringJoiner;
    }

    public static void main(String[] args) {
        StringJoiner dirName = getDirName(FileUploadType.UPLOAD, true);
        System.out.println(dirName.toString());
    }


}
