package com.fdkankan.common.util;

import com.fdkankan.common.constant.FileSizeUnitType;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Objects;

public class FileSizeUtil {

    private static final String TAG=FileSizeUtil.class.getSimpleName();

    public static final int SIZETYPE_B = 1;//获取文件大小单位为B的double值
    public static final int SIZETYPE_KB = 2;//获取文件大小单位为KB的double值
    public static final int SIZETYPE_MB = 3;//获取文件大小单位为MB的double值
    public static final int SIZETYPE_GB = 4;//获取文件大小单位为GB的double值

    /**
     * 获取文件指定文件的指定单位的大小
     *
     * @param filePath 文件路径
     * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
     * @return double值的大小
     */
    public static double getFileOrFilesSize(String filePath, int sizeType) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formetFileSize(blockSize, sizeType);
    }

    /**
     * 调用此方法自动计算指定文件或指定文件夹的大小
     *
     * @param filePath 文件路径
     * @return 计算好的带B、KB、MB、GB的字符串
     */
    public static String getAutoFileOrFilesSize(String filePath) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return formatFileSize(blockSize);
    }

    /**
     * 获取指定文件大小
     */
    private static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
        }
        return size;
    }

    /**
     * 获取指定文件夹
     */
    private static long getFileSizes(File f) throws Exception {
        long size = 0;
        File flist[] = f.listFiles();
        assert flist != null;
        for (File file : flist) {
            if (file.isDirectory()) {
                size = size + getFileSizes(file);
            } else {
                size = size + getFileSize(file);
            }
        }
        return size;
    }

    /**
     * 转换文件大小
     */
    public static String formatFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else if (fileS < 1099511627776L){
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        } else if (fileS < 1125899906842624L){
            fileSizeString = df.format((double) fileS / 1099511627776L) + "TB";
        } else {
            fileSizeString = df.format((double) fileS / 1125899906842624L) + "PB";
        }
        return fileSizeString;
    }

    /**
     * 文件大小单位转换
     * @param size 原始大小，单位B
     * @param unit 需要转换的目标单位 B KB MB GB TB PB
     * @return
     */
    public static Integer convert(Long size, String unit) throws Exception {
        FileSizeUnitType fileSizeUnitType = FileSizeUnitType.get(unit);
        if(Objects.isNull(fileSizeUnitType)){
            throw new Exception("单位错误!");
        }
        if(size == null || size == 0){
            return 0;
        }
        BigDecimal bigDecimal = new BigDecimal(size);
        long divisor = 1L;
        switch (fileSizeUnitType){
            case KB:
                divisor = 1024L;
                break;
            case MB:
                divisor = 1048576L;
                break;
            case GB:
                divisor = 1073741824L;
                break;
            case TB:
                divisor = 1099511627776L;
                break;
            case PB:
                divisor = 1125899906842624L;
                break;
        }
        return bigDecimal.divide(new BigDecimal(divisor)).setScale(0, BigDecimal.ROUND_UP).intValue();
    }

    /**
     * 转换文件大小,指定转换的类型
     */
    public static double formetFileSize(long fileS, int sizeType) {
        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
            case SIZETYPE_B:
                fileSizeLong = Double.valueOf(df.format((double) fileS));
                break;
            case SIZETYPE_KB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
                break;
            case SIZETYPE_MB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
                break;
            case SIZETYPE_GB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
                break;
            default:
                break;
        }
        return fileSizeLong;
    }
}