package com.naz.sdkdemo.utils;


import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 时间格式化工具
 * UTC: 世界协调时间
 * CST: 中央标准时间
 * GMT: 时区
 * @created on: 2018/3/30 14:15
 */

public class TimeUtils {

    public final static int DAY_OF_SECOND = 24 * 60 * 60;
    public final static int HOUR_OF_SECOND = 60 * 60;
    public final static int MINUTE_OF_SECOND = 60;

    /**
     * 把字符串格式的日期转化为指定的常用日期格式
     *
     * @param date
     * @return
     */
    public static String parseFormatDate(String date) {
        return parseFormatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 把字符串格式的日期转化为指定的常用日期格式
     *
     * @param date
     * @return
     */
    public static String parseFormatDate(String date, String format) {
        Date d = parseToDateObj(date);
        if (null == d) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            return df.format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 把字符串格式的日期转化为Date对象
     *
     * @param date
     * @return
     */
    public static Date parseToDateObj(String date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (isLongDateFormat(date)) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if (isShortDateFormat(date)) {
            df = new SimpleDateFormat("yyyy-MM-dd");
        } else if (isNumberFormat(date)) {
            return new Date(Long.parseLong(date));
        } else if (isLongDateFormatNoSS(date)) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        } else if (isUTCFormat(date)) {
            //date = date.replace("Z", "UTC");
            df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
        } else {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.ENGLISH);
        }
        try {
            return df.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断当前的日期是否是长日期格式(yyyy-MM-dd HH:mm:ss)
     *
     * @param date
     * @return
     */
    public static boolean isLongDateFormat(String date) {
        if (TextUtils.isEmpty(date)) {
            return false;
        }
        Pattern p = Pattern.compile("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$");
        Matcher m = p.matcher(date);
        return m.matches();
    }

    /**
     * 判断当前日期格式是否是简易日期格式(即：yyyy-MM-dd)
     *
     * @param date
     * @return
     */
    public static boolean isShortDateFormat(String date) {
        if (TextUtils.isEmpty(date)) {
            return false;
        }
        Pattern p = Pattern.compile("^\\d{4}-\\d{2}-\\d{2}$");
        Matcher m = p.matcher(date);
        return m.matches();
    }

    /**
     * 判断当前的日期是否是没有秒的长日期格式(yyyy-MM-dd HH:mm)
     * @param date
     * @return
     */
    public static boolean isLongDateFormatNoSS(String date){
        if(TextUtils.isEmpty(date)){
            return false;
        }
        Pattern p = Pattern.compile("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$");
        Matcher m = p.matcher(date);
        return m.matches();
    }

    /**
     * 判断当前给定的字符是不是全部由数字组成
     * @param date
     * @return
     */
    private static boolean isNumberFormat(String date){
        if(TextUtils.isEmpty(date)){
            return false;
        }
        Pattern p = Pattern.compile("^(-)?[0-9]*[0-9]$");
        Matcher m = p.matcher(date);
        return m.find();
    }

    /**
     * 判断是否为utc格式日志 格式xxxxTxxxxZ
     * @param date
     * @return
     */
    private static boolean isUTCFormat(String date){
        if(TextUtils.isEmpty(date)){
            return false;
        }
        return date.contains("T") && date.endsWith("Z");
    }

    public static boolean afterOfCurrentTime(String time){
        Date date = parseToDateObj(time);
        return null != date && date.getTime() > System.currentTimeMillis();
    }

    public static long diffMilliseconds(String firstDate, String secondDate) {
        Date first = parseToDateObj(firstDate);
        Date second = parseToDateObj(secondDate);
        if (null != first && null != second) {
            return second.getTime() - first.getTime();
        }
        return -1;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return long[] 返回值为：{天, 时, 分, 秒}
     */
    public static long[] diffTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    public static String diffTimeCN(String str1, String str2, boolean needSeconds) {
        long[] times = diffTime(str1, str2);
        if (times == null || times.length < 4) {
            return null;
        }
        long day = times[0];
        long hour = times[1];
        long min = times[2];
        long sec = times[3];
        StringBuilder sb = new StringBuilder();
        sb.append((day < 9 ? "0": "")).append(day).append("天");
        sb.append((hour < 9 ? "0": "")).append(hour).append("小时");
        sb.append((min < 9 ? "0": "")).append(min).append("分钟");
        if (needSeconds) {
            sb.append((sec < 9 ? "0" : "")).append(sec).append("秒");
        }
        return sb.toString();
    }

    public static String diffTimeCN(long milliseconds, int format, boolean cn) {
        long left = milliseconds;
        long day = left / (24 * 60 * 60 * 1000);
        left -= 24 * 60 * 60 * 1000 * day;
        long hour = left / (60 * 60 * 1000);
        left -= 60 * 60 * 1000 * hour;
        long min = left / (60 * 1000);
        left -= 60 * 1000 * min;
        long sec = left/1000;
        StringBuilder sb = new StringBuilder();
        if (format >= 4) {
            sb.append((day < 10 ? "0" : "")).append(day).append(cn ? "天" : ":");
        }
        if (format >= 3) {
            sb.append((hour < 10 ? "0" : "")).append(hour).append(cn ? "小时" : ":");
        }
        if (format >= 2) {
            sb.append((min < 10 ? "0" : "")).append(min).append(cn ? "分钟" : ":");
        }
        if (format >= 1) {
            sb.append((sec < 10 ? "0" : "")).append(sec).append(cn ? "秒" : "");
        }
        return sb.toString();
    }

    public static String getDayOfWeekCN(String time) {
        Date date = parseToDateObj(time);
        if (null != date) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            switch (day) {
                case Calendar.SUNDAY:
                    return "周日";
                case Calendar.MONDAY:
                    return "周一";
                case Calendar.TUESDAY:
                    return "周二";
                case Calendar.WEDNESDAY:
                    return "周三";
                case Calendar.THURSDAY:
                    return "周四";
                case Calendar.FRIDAY:
                    return "周五";
                case Calendar.SATURDAY:
                    return "周六";
            }
        }
        return null;
    }
}

