package api.common.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class TimeUtil {

    public static String dateToString(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }
    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }


    /**
     * @param date 日期
     * @return 13 位时间戳
     */
    public static long dateToTimestamp(Date date) {
        return date.getTime();
    }

    private static final String[] dateFmtArr = new String[]{"yyyyMMdd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HHmmssSS", "HH:mm:ss", "yyyyMMddHHmmss"};

    public static long getNow() {
        return System.currentTimeMillis();
    }

    public static long getNowLong() {
        return System.currentTimeMillis();
    }

    public static String getNowString() {
        return System.currentTimeMillis() + "";
    }

    public static Timestamp getNowForMysql() {
        return new Timestamp(System.currentTimeMillis());
    }

    public static long zeroOfToday() {
        return System.currentTimeMillis() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
    }

    //获取时间类型格式转化
    public static Integer getRq(Date date, int index) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFmtArr[index]);
        if (date == null) {
            date = new Date();
        }
        return Integer.valueOf(sdf.format(date));

    }

    //获取时间类型格式转化
    public static String getToStringDate(Date date, int index) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFmtArr[index]);
        if (date == null) {
            date = new Date();
        }
        return sdf.format(date);

    }

    //获取过去某天的起始日期
    public static Map<String, Object> getPastTime(Integer num) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -num);
        Date d = c.getTime();
        String startDate = getToStringDate(d, 2) + " 00:00:00";
        String endDate = getToStringDate(d, 2) + " 23:59:59";
        Map<String, Object> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("toDate", getToStringDate(d, 2));
        return map;
    }

    //获取间隔 n 秒后的时间
    public static Timestamp getPostTimestamp(int secends) {
        return new Timestamp(System.currentTimeMillis() + secends * 1000L);
    }

    //获取两个时间间隔
    public static String getTimeDifference(Date dateStart, Date dateStop) {
        String message = "";
        try {
            //毫秒ms
            long diff = dateStop.getTime() - dateStart.getTime();
            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            if (diffDays > 0) {
                message = diffDays + "天" + diffHours + "小时" + diffMinutes + "分钟" + diffSeconds + "秒";
            } else if (diffDays == 0 && diffHours != 0) {
                message = diffHours + "小时" + diffMinutes + "分钟" + diffSeconds + "秒";
            } else if (diffDays == 0 && diffMinutes != 0) {
                message = diffMinutes + "分钟" + diffSeconds + "秒";
            } else if (diffDays == 0 && diffSeconds != 0) {
                message = diffSeconds + "秒";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

    //获取 n 天前的日期
    public static String getMinusDaysStr(int days) {
        LocalDate before = LocalDate.now().minusDays(days);
        return before.toString();
    }


    public static String changeTimeFormat(int seconds) {
        if (seconds > 0 && seconds < 60) {//小于1分钟
            return seconds + "秒";
        } else if (seconds >= 60 && seconds < 3600) {//大于等于1分钟小于1小时
            int changeM = (int) Math.floor(seconds / 60);//整分钟数
            int surplusM = (int) Math.floor(seconds % 60);//余下的秒数
            if (surplusM > 0) {//余数不为0秒
                return changeM + "分" + surplusM + "秒";
            } else {//整分钟，没有余数
                return changeM + "分";
            }
        } else if (seconds >= 3600) {//大于1小时
            int changeH = (int) Math.floor(seconds / 3600);//整小时数
            int surplusH = (int) Math.floor(seconds % 3600);//剩下的秒数
            if (surplusH >= 60) {//余数大于大于1分钟
                int changeM = (int) Math.floor(surplusH / 60);
                int surplusM = (int) Math.floor(surplusH % 60);
                if (surplusM > 0) {//余数大于0
                    return changeH + "小时" + changeM + "分" + surplusM + "秒";
                } else {//整分钟，没有余数
                    return changeH + "小时" + changeM + "分";
                }
            } else if (surplusH < 60 && surplusH > 0) {//余数小于1分钟，大于0秒
//                int surplusM = (int) Math.floor(surplusH % 1);
                return changeH + "小时" + surplusH + "秒";
            } else {
                return changeH + "小时";
            }
        }
        return "暂无数据";
    }

    public static void main(String[] args) {
        System.out.println(changeTimeFormat(3601));
    }
}
