package com.renrenliao.im.utils;

import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import com.renrenliao.im.R;
import com.renrenliao.im.utils.ntp.TrueTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * *@ClassName MDateUtils
 * *@Description TODO
 * *@Author ganzhe
 * *@Date 2022/5/21 16:08
 **/
public class MDateUtils {
    private static final String TAG=MDateUtils.class.getSimpleName();
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd  EEEE");
    private static SimpleDateFormat sdf_hh_mm = new SimpleDateFormat("HH:mm");
    private static SimpleDateFormat sdf_yyyy_MM_dd = new SimpleDateFormat("yyyy.MM.dd");

    public static synchronized long currentTimMillis()
    {
//        return System.currentTimeMillis();
        long currentTime = TrueTime.currentTime();
        if (currentTime==0|| SystemClock.elapsedRealtime()- TrueTime.getCachedDeviceUptime()>6*60*60*1000){
            return System.currentTimeMillis();
        }else {
            Log.d(TAG,currentTime+"");
            return currentTime;
        }
    }
    /*
     * 将时间戳转为字符串 ，格式：yyyy-MM-dd HH:mm
     */
    public static String getStrTime_ymd_hm(String cc_time) {
        String re_StrTime = "";
        if (TextUtils.isEmpty(cc_time) || "null".equals(cc_time)) {
            return re_StrTime;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;

    }

    /*
     * 将时间戳转为字符串 ，格式：yyyy-MM-dd HH:mm:ss
     */
    public static String getStrTime_ymd_hms(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;

    }

    /*
     * 将时间戳转为字符串 ，格式：yyyy.MM.dd
     */
    public static String getStrTime_ymd(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        // 例如：cc_time=1291778220
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));
        return re_StrTime;
    }

    public static String getTimeString(Date dt, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            sdf.setTimeZone(TimeZone.getDefault());
            return sdf.format(dt);
        } catch (Exception var3) {
            return "";
        }
    }

    public static String getTimeStr_auto(long timemills, boolean mustIncludeTime) {
        String ret = "";
        Date srcDate = new Date(timemills);
        try {
            GregorianCalendar gcCurrent = new GregorianCalendar();
            gcCurrent.setTime(new Date());
            int currentYear = gcCurrent.get(Calendar.YEAR);
            int currentMonth = gcCurrent.get(Calendar.MONTH) + 1;
            int currentDay = gcCurrent.get(Calendar.DATE);
            GregorianCalendar gcSrc = new GregorianCalendar();
            gcSrc.setTime(srcDate);
            int srcYear = gcSrc.get(Calendar.YEAR);
            int srcMonth = gcSrc.get(Calendar.MONTH) + 1;
            int srcDay = gcSrc.get(Calendar.DATE);
            String timeExtraStr = mustIncludeTime ? " " + getTimeString(srcDate, "HH:mm") : "";
            if (currentYear == srcYear) {
                long currentTimestamp = MDateUtils.currentTimMillis();
                long srcTimestamp = gcSrc.getTimeInMillis();
                long delta = currentTimestamp - srcTimestamp;
                if (currentMonth == srcMonth && currentDay == srcDay) {
                    if (delta < 60000L) {
                        ret = "刚刚";
                    } else {
                        ret = getTimeString(srcDate, "HH:mm");
                    }
                } else {
                    GregorianCalendar yesterdayDate = new GregorianCalendar();
                    yesterdayDate.add(Calendar.DATE, -1);
                    GregorianCalendar beforeYesterdayDate = new GregorianCalendar();
                    beforeYesterdayDate.add(Calendar.DATE, -2);
                    if (srcMonth == yesterdayDate.get(Calendar.MONTH) + 1 && srcDay == yesterdayDate.get(Calendar.DATE)) {
                        ret = "昨天" + timeExtraStr;
                    } else if (srcMonth == beforeYesterdayDate.get(Calendar.MONTH) + 1 && srcDay == beforeYesterdayDate.get(Calendar.DATE)) {
                        ret = "前天"+ timeExtraStr;
                    } else {
                        long deltaHour = delta / 3600000L;
                        if (deltaHour < 168L) {
                            String[] weekday = new String[]{
                                   "星期日",
                                    "星期一",
                                    "星期二",
                                    "星期二",
                                    "星期三",
                                    "星期四",
                                    "星期五"};
                            String weedayDesc = weekday[gcSrc.get(Calendar.DAY_OF_WEEK) - 1];
                            ret = weedayDesc + timeExtraStr;
                        } else {
                            ret = getTimeString(srcDate, "yyyy/M/d") + timeExtraStr;
                        }
                    }
                }
            } else {
                ret = getTimeString(srcDate, "yyyy/M/d") + timeExtraStr;
            }
        } catch (Exception var24) {
            System.err.println("【DEBUG-getTimeStringAutoShort】计算出错：" + var24.getMessage() + " 【NO】");
        }

        return ret;
    }
}
