package com.fxy.utils;

import android.os.Environment;
import android.os.StatFs;

import com.fxy.baselibrary.util.StringUtils;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;

import static com.fxy.baselibrary.util.RxFileTool.getSDCardPath;


/**
 * SD卡目录工具类
 *
 * @author WuRS
 */
public class FilePathUtil {


    public static ArrayList<String> getPhotoRemotePaths(String path, String images) {
        if (!StringUtils.isEmpty(images)) {
            String[] photos = images.split(",");
            ArrayList<String> list = new ArrayList<String>();
            for (int j = 0; j < photos.length; j++) {
                if (!StringUtils.isEmpty(photos[j])) {
                    list.add(FilePathUtil.getPhotoRemotePath(path, photos[j]));
                }
            }
            return list;
        }
        return null;
    }

    public static String getPhotoRemotePath(String path, String photoName) {
        return path + photoName;
    }

    /**
     * 文件主目录
     *
     * @return
     */
    public static String getMainPath() throws Exception {
        try {
            String path = getSDCardPath() + "fingerstorage/";
            File dir = new File(path);
            if (!dir.exists()) {
                if (!dir.mkdirs()) {
                    path = "/rows/rows" + "/com.cargo.fingerstorage/";
                    dir = new File(path);
                    if (!dir.exists())
                        dir.mkdirs();
                }
            }
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    public static File getRecorderFile() throws Exception {
        try {
            String path = getMainPath() + "recorder/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return dir;
        } catch (Exception e) {
            throw e;
        }
    }

    public static File getVideoFile() throws Exception {
        try {
            String path = getMainPath() + "video/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return dir;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * @return 语音路径
     * @throws Exception
     */
    public static String getRecorderPath() throws Exception {
        try {
            String path = getMainPath() + "recorder/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 格式化单位
     *
     * @param size
     * @return
     */
    public static String getFormatSize(double size) {
        double kiloByte = size / 1024;
        if (kiloByte < 1) {
            return String.valueOf(size) + "B";
//            return "0K";
        }
        double megaByte = kiloByte / 1024;
        if (megaByte < 1) {
            BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "KB";
        }

        double gigaByte = megaByte / 1024;
        if (gigaByte < 1) {
            BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "MB";
        }

        double teraBytes = gigaByte / 1024;
        if (teraBytes < 1) {
            BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
                    .toPlainString() + "GB";
        }
        BigDecimal result4 = new BigDecimal(teraBytes);
        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
                + "TB";
    }

    /**
     * 获取错误日志文件夹
     *
     * @return
     * @throws Exception
     */
    public static String getErrorPath() throws Exception {
        try {
            String path = getMainPath() + "error/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 获取截图文件夹
     *
     * @return
     * @throws Exception
     */
    public static String getScreenshotPath() throws Exception {
        try {
            String path = getMainPath() + "screenshot/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 临时目录
     *
     * @return
     */
    public static String getTempPath() throws Exception {
        try {
            String path = getMainPath() + "temp/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 获取临时照片文件路径
     *
     * @return
     * @throws Exception
     */
    public static String getTempImageFilePath() throws Exception {
        try {
            return FilePathUtil.getTempPath() + "temp.jpeg";
        } catch (Exception e) {
            throw e;
        }
    }
    /**
     * 删除临时照片文件文件
     */
    public static void deleteTempImageFile() {
        try {
            deleteFile(getTempImageFilePath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 文件上传目录
     *
     * @return
     * @throws Exception
     */
    public static String getUploadPath() throws Exception {
        try {
            String path = getMainPath() + "upload/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * @return 监控照片目录
     * @throws Exception
     */
    public static String getMonitorPicPath() throws Exception {
        try {
            String path = getMainPath() + "monitorPic/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * @return 下载路径
     * @throws Exception
     */
    public static String getDownLoadPath() throws Exception {
        try {
            String path = getMainPath() + "download/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    public static void deleteMonitorUploadFiles() {
        try {
            File monitorDir = new File(getMonitorPicPath());
            if (monitorDir.exists() && monitorDir.isDirectory()) {
                File[] files = monitorDir.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        if (files[i].exists() && files[i].isFile()) {
                            files[i].delete();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @return 公司认证照片目录
     * @throws Exception
     */
    public static String getCompanyPicPath() throws Exception {
        try {
            String path = getMainPath() + "CompanyPic/";
            File dir = new File(path);
            if (!dir.exists())
                dir.mkdirs();
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    public static void deleteCompanyFiles() {
        try {
            File companyDir = new File(getCompanyPicPath());
            if (companyDir.exists() && companyDir.isDirectory()) {
                File[] files = companyDir.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        if (files[i].exists() && files[i].isFile()) {
                            files[i].delete();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除上传文件中的所有文件
     */
    public static void deleteUploadFiles() {
        try {
            File uploadDir = new File(getUploadPath());
            if (uploadDir.exists() && uploadDir.isDirectory()) {
                File[] files = uploadDir.listFiles();
                if (files != null) {
                    for (int i = 0; i < files.length; i++) {
                        if (files[i].exists() && files[i].isFile()) {
                            files[i].delete();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param filePath
     * @return 删除文件
     */
    public static boolean deleteFile(String filePath) {
        if (filePath == null) {
            return false;
        }
        return deleteFile(new File(filePath));
    }

    /**
     * @param file
     * @return 删除文件
     */
    public static boolean deleteFile(File file) {
        if (file != null && file.exists() && file.isFile()) {
            return file.delete();
        }
        return false;
    }

    /**
     * 头像文件目录
     *
     * @return
     */
    public static String getPersonIconPath() throws Exception {
        try {
            String path = getMainPath() + "heads/";
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            return path;
        } catch (Exception e) {
            throw e;
        }
    }

    // 取SD卡路径
    public static String getSDPath() throws Exception {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory(); // 获取根目录
        }
        if (sdDir != null) {
            return sdDir.getAbsolutePath();
        } else {
            throw new Exception("SDcard不存在");
        }
    }

    // 判断所传入的文件是否大于SDcard剩余的容量
    public static boolean isSDSizeEnough(long fileSize) throws Exception {
        try {
            if ("".equals(getSDPath())) {
                return false;
            }
        } catch (Exception e) {
            throw e;
        }
        File path = Environment.getExternalStorageDirectory();
        StatFs statfs = new StatFs(path.getPath());
        long blockSize = statfs.getBlockSize();
        long availableBlocks = statfs.getAvailableBlocks();
        if (availableBlocks * blockSize > fileSize)
            return true;
        else
            return false;
    }

}
