package com.kfzs.duanduan.utils.dlg;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Dlg on 2015/7/2.
 */
public class FormatAny {

    private FormatAny() {

    }

    private static FormatAny mFormatAny;

    public synchronized static FormatAny getInstance() {
        if (mFormatAny == null) {
            mFormatAny = new FormatAny();
        }
        return mFormatAny;
    }

    /**
     * 将指定数值转换为保留N个小数的值
     *
     * @param numberstr
     * @param format    如这类：#.00
     * @return
     */
    public String formatNumber(String numberstr, String format) {
        if (numberstr == null || numberstr.equals(""))
            return "0";
        DecimalFormat df = new DecimalFormat(format);
        String result = df.format(Double.valueOf(numberstr));
        if (result.startsWith("."))
            result = "0" + result;
        return result;
    }

    /**
     * 秒数转为分钟或小时
     *
     * @param ptime 要显示的时间
     * @return
     */
    public String formatMicSecond(Date ptime) {
        long realtime = (System.currentTimeMillis() - ptime.getTime()) / 1000;
        if (realtime < 61)
            return "片刻前";
        else if (realtime < 3600) {
            return String.valueOf(realtime / 60) + "分钟前";
        } else if (realtime < 3600 * 48) {
            DecimalFormat df = new DecimalFormat("#.0");
            return df.format(realtime / 3600f) + "小时前";
        } else {
            return (realtime / 86400) + "天前";
        }
    }


    /**
     * 格式化距离，返回XX米或XX.xx公里
     *
     * @param distance
     * @return
     */
    public String formatDistance(long distance) {
        if (distance == 0) {
            return "未知距离";
        } else if (distance < 1000)
            return distance + "米";
        else {
            return formatNumber(String.valueOf(distance / 1000.0), "#.00") + "公里";
        }
    }


    /**
     * 把15343.34格式化为：15,343.34这种格式
     *
     * @param money
     * @return 失败时会返回0
     */
    public String formatMoney(String money) {
        if (TextUtils.isEmpty(money)) {
            return "0";
        }
        StringBuilder backStr = new StringBuilder();
        String highNum;
        String lowNum = "";
        //原始money格式化处理后
        String oldMoney = money.replace(",", "");
        while (oldMoney.startsWith("0")) {//过滤0开关的情况
            oldMoney = oldMoney.substring(1, oldMoney.length());
        }

        if (oldMoney.contains(".")) {
            lowNum = oldMoney.substring(money.indexOf("."), money.length());
            highNum = oldMoney.substring(0, money.indexOf("."));
        } else {
            highNum = oldMoney.replace(",", "");
        }
        int len = highNum.length();
        for (int i = 0; i < len; i++) {
            backStr.insert(0, highNum.charAt(len - i - 1));
            if (i % 3 == 2 && i != len - 1) {
                backStr.insert(0, ",");
            }
        }
        backStr.append(lowNum);
        return backStr.toString().equals("") ? "0" : backStr.toString();
    }


    /**
     * 把字节长度值转为类似KB文本
     *
     * @param fileS
     * @return
     */
    public String getFileSize(long fileS) {// 转换文件大小
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString;
        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";
        }
        if (fileSizeString.startsWith("."))
            fileSizeString = "0" + fileSizeString;
        return fileSizeString;
    }


    public String formatTime(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 默认的时间格式化处理
     */
    private static final String FORMAT_TIME_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    public String formatTime(Date date) {
        return new SimpleDateFormat(FORMAT_TIME_DEFAULT).format(date);
    }

    /**
     * 数字格式化
     */
    public String formatString(String value){
        try{
            return new DecimalFormat("#.##").format(Double.valueOf(value));
        }catch (Exception e){
            e.printStackTrace();
            return "0";
        }
    }

}
