package com.zhaohaoting.framework.utils.format;

import android.text.TextUtils;

/**
 * author：Created by ZHT on 2016/9/9 15:58
 * email：526309416@qq.com
 * desc：毫秒转化
 */
public class MsTransformation {

    private static String strDay;
    private static String strHour;
    private static String strMaxHour;
    private static String strMinute;
    private static String strSecond;
    private static String strMilliSecond;

    /**
     * 毫秒转化
     */
    public static void formatTime(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long maxhour = ms / hh;

        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        //天
        strDay = day < 10 ? "0" + day : "" + day;
        //不足一天后 转换小时
        strHour = hour < 10 ? "0" + hour : "" + hour;

        //ms直接转换小时
        strMaxHour = maxhour < 10 ? "0" + maxhour : "" + maxhour;


        //分钟
        strMinute = minute < 10 ? "0" + minute : "" + minute;
        //秒
        strSecond = second < 10 ? "0" + second : "" + second;
        //毫秒
        strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
    }

    public static String formatTimeLong(long ms) {
        formatTime(ms);
        return strMaxHour + ":" + strMinute + ":" + strSecond;
    }

    public static String formatTimeMinute(long ms) {
        formatTime(ms);
        if (0 < Integer.parseInt(strDay)) {
            return strDay + "天";
        }
        if (0 == Integer.parseInt(strDay) && 0 < Integer.parseInt(strMaxHour)) {
            return strMaxHour + "时";
        }
        if (Integer.parseInt(strMaxHour) == 0 && 0 < Integer.parseInt(strMinute)) {
            return strMinute + "分";
        }
        if (0 == Integer.parseInt(strMinute) && 0 < Integer.parseInt(strSecond)) {
            return 1 + "分";
        }
        return "已结束";
    }

    public static String formatTimes(long ms) {
        formatTime(ms);
        if (0 < Integer.parseInt(strDay)) {
            return Integer.parseInt(strDay) + "天";
        } else if (Integer.parseInt(strHour) > 0) {
            return Integer.parseInt(strHour) + "小时";
        } else if (Integer.parseInt(strMinute) > 0) {
            return Integer.parseInt(strMinute) + "分钟";
        } else if (Integer.parseInt(strSecond) > 0) {
            return 1 + "分钟";
        }
//        return strDay + "天" + strHour + "小时" + strMinute + "分钟";
        return strMinute + "分钟";
    }

    public static String formatTimesBefore1(long ms, long currentMs) {
        if (currentMs < ms) return "";
        formatTime(currentMs - ms);
        if (Integer.parseInt(strDay) > 365) {
            int year = Integer.parseInt(strDay) / 365;
            return year + "年前";
        } else if (Integer.parseInt(strDay) > 30) {
            int months = Integer.parseInt(strDay) / 30;
            return months + "月前";
        } else if (Integer.parseInt(strDay) > 0) {
            return Integer.parseInt(strDay) + "天前";
        } else if (Integer.parseInt(strHour) > 0) {
            return Integer.parseInt(strHour) + "小时前";
        } else if (Integer.parseInt(strMinute) > 0) {
            return Integer.parseInt(strMinute) + "分钟前";
        } else if (Integer.parseInt(strSecond) > 0) {
            return "刚刚";
        }
//        return strDay + "天" + strHour + "小时" + strMinute + "分钟";
        return "刚刚";
    }

    public static String formatTimesBefore2(long ms, long currentMs) {
        if (currentMs < ms) return "";
        formatTime(currentMs - ms);
        StringBuilder stringBuilder = new StringBuilder();
        if (Integer.parseInt(strDay) > 0) {
            stringBuilder.append(strDay + "天");
        }
        if (Integer.parseInt(strHour) > 0) {
            stringBuilder.append(strHour + "小时");
        }
        if (Integer.parseInt(strMinute) > 0) {
            stringBuilder.append(strMinute + "分钟");
        }
        return stringBuilder.toString();
    }

    public static String formatTimesBefore2(long mss) {
        formatTime(mss);
        StringBuilder stringBuilder = new StringBuilder();
        if (Integer.parseInt(strDay) > 0) {
            stringBuilder.append(strDay + "天");
        }
        if (Integer.parseInt(strHour) > 0) {
            stringBuilder.append(strHour + "小时");
        }
        if (Integer.parseInt(strMinute) > 0) {
            stringBuilder.append(strMinute + "分钟");
        }
        if (TextUtils.isEmpty(stringBuilder.toString())) {
            return TextUtils.isEmpty(strSecond) ? "0" : strSecond + "秒";
        }
        return stringBuilder.toString();
    }
}
