package com.duben.microtribe.utils;

import android.util.Pair;

/**
 * Creator:       xuzhihui
 * Create Time:   2017/11/15 18:22
 * Description:   TODO
 */
public class SizeUtil {

    public static String formatFileSize(long fileS) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS == 0) {
            fileSizeString = "0.00 B";
        } else 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 {
            fileSizeString = df.format((double) fileS / 1073741824) + " GB";
        }
        return fileSizeString;
    }

    public static String specialFormatFileSize(long fileS) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS == 0) {
            fileSizeString = "0.00B";
        } else 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 {
            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
        }
        return fileSizeString;
    }


    public static Pair<String,String> formatFileSizeToPair(long fileS) {
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        String fileSizeString = "";
        String sign="";
        if (fileS == 0) {
            fileSizeString = "0.00 B";
        } else if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) ;
            sign=" B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) ;
            sign=" KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576);
            sign=" MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824);
            sign=" GB";
        }
        Pair<String,String> result= new Pair<>(fileSizeString,sign);
        return result;
    }


    public static String formatNetSpeed(long speed){
        String netSpeed = "";
        if (speed < 1024) {
            netSpeed = String.format("%d B/s", speed);
        } else if (speed < (1<<20)) {
            netSpeed = String.format("%3.1f KB/s", speed/(1024f));
        } else if (speed < (1<<30)) {
            netSpeed = String.format("%3.1f MB/s", speed/((float)(1<<20)));
        } else {
            netSpeed = String.format("%3.1f GB/s", speed/((float)(1<<30)));
        }
        return netSpeed;
    }
}
