package com.stm.asset.utils;

import android.os.Environment;

import java.io.File;

/**
 * 类名称：FileUtils
 * 类描述：文件处理工具类
 * 创建人：story
 * 创建时间：2021-12-07 14:43
 */
public class FileUtils {

    public static final String SD_PATH = Environment.getExternalStorageDirectory() + "";
    public static final String FILE_PATH ="/Download/";

    public static String getBasePath() {
        String path = SD_PATH + FILE_PATH;
        createFileDir(path);
        return path;
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static String getDateFilePath(String dateStr) {
        String filePath = getBasePath() + dateStr;
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return filePath;
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    private static void createFileDir(String path){
        String dirPath = path.substring(0, path.lastIndexOf("/")+1);
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public static String getNewPathWithStr(String path, String str) {
        String result = path;
        if (!StringUtils.isEmptyOrNull(path)) {
            int suffixIndex = path.lastIndexOf(".");
            if (suffixIndex > 0) {
                result = path.substring(0, suffixIndex) + str + path.substring(suffixIndex);
                File file = new File(result);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (Exception e) {
                        result = "Exception:" + e.getMessage();
                        e.printStackTrace();
                    }
                }
            }
        }
        return result;
    }

}
