package com.ywt.outpatient.taihe.rpc.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 *
 * @author johnson lin
 * @date 2019/4/29 9:51 PM
 */
public final class DateUtil {
    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    private static final String[] WEEK_DAYS_SHORT = {"日", "一", "二", "三", "四", "五", "六"};

    private static final String[] WEEK_DAYS_MIDDLE = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    public static final String DADE_FROMAT_YMD = "yyyy-MM-dd";

    private DateUtil() {

    }

    /**
     * 将时间戳转换成日期字符串格式
     *
     * @param timestamp 时间戳
     * @param pattern   日期格式
     * @return
     */
    public static String convertTimestampToDateString(long timestamp, String pattern) {
        Date date = new Date(timestamp);
        return convertToString(date, pattern);
    }

    public static String convertToString(Date date) {
        return convertToString(date, null);
    }

    public static String convertToString(Date date, String pattern) {
        if (StringHelper.isNullOrEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 计算当前时间和指定时间之间相差的小时
     *
     * @param smdate 指定的时间
     * @throws ParseException
     */
    public static int hourBetween(String smdate) {
        Date start = new Date();
        Date end = stringToDate(smdate);
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        long time1 = cal.getTimeInMillis();
        cal.setTime(end);
        long time2 = cal.getTimeInMillis();

        long between_days = (time2 - time1) / (1000 * 3600);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * String转Date
     *
     * @param strDate
     * @return
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (null == strDate) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(strDate);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return null;
    }

    public static Date stringToDate(String strDate) {
        return stringToDate(strDate, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 当前日期是星期几
     *
     * @param calendar
     * @return
     */
    public static String getDayOfWeek(Calendar calendar) {
        int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (index < 0) {
            index = 0;
        }
        return WEEK_DAYS[index];
    }

    /**
     * 当前日期是星期（缩略）
     *
     * @param calendar
     * @return
     */
    public static String getDayOfWeekShort(Calendar calendar) {
        int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (index < 0) {
            index = 0;
        }
        return WEEK_DAYS_SHORT[index];
    }

    /**
     * 当前日期是星期（缩略）
     *
     * @param calendar
     * @return
     */
    public static String getDayOfWeekMiddle(Calendar calendar) {
        int index = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (index < 0) {
            index = 0;
        }
        return WEEK_DAYS_MIDDLE[index];
    }

    public static String getDateString4Comments(long timeInMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeInMillis);
        Calendar today = Calendar.getInstance();
        long delta = today.getTimeInMillis() - timeInMillis;
        final long ONE_MINUTE = 1000 * 60;
        final long ONE_HOUR = ONE_MINUTE * 60;
        final long ONE_DAY = ONE_HOUR * 24;
        final long ONE_YEAR = ONE_DAY * 365;
        int days = (int) (delta / ONE_DAY);
        if (days >= 365) {
            int years = (int) Math.floor((double) delta / ONE_YEAR);
            return String.format("%d年前", years);
        } else if (days <= 0) {
            int hours = (int) Math.floor((double) delta / ONE_HOUR);
            if (hours > 0) {
                return String.format("%d小时前", hours);
            } else {
                int minutes = (int) (delta / ONE_MINUTE);
                return minutes > 0 ? String.format("%d分钟前", minutes) : "刚刚";
            }
        } else {
            return String.format("%d天前", days);
        }
    }

    public static Date parseDate(String str, String pattern) {
        if (StringHelper.isNullOrWhiteSpace(str)) {
            return null;
        }

        if (StringHelper.isNullOrWhiteSpace(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";    //默认日期格式
        }

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            format.setLenient(false);
            return format.parse(str);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 将日期类型 LocalDate 转换成 Date 类型
     *
     * @param localDate 日期类型 LocalDate
     * @return 日期类型 Date
     */
    public static Date convertToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 将日期类型 LocalDateTime 转换成 Date 类型
     *
     * @param localDateTime 日期类型 LocalDateTime
     * @return 日期类型 Date
     */
    public static Date convertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 将日期类型 Date 转换成 LocalDate 类型
     *
     * @param date 日期类型 Date
     * @return 日期类型 LocalDate
     */
    public static LocalDate convertToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 将时间戮转换成 LocalDate 类型
     *
     * @param timestamp 时间戮
     * @return 日期类型 LocalDate
     */
    public static LocalDate convertToLocalDate(long timestamp) {
        return Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    /**
     * 将日期类型 Date 转换成 LocalDateTime 类型
     *
     * @param date 日期类型 Date
     * @return 日期类型 LocalDateTime
     */
    public static LocalDateTime convertToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 将时间戮转换成 LocalDateTime 类型
     *
     * @param timestamp 时间戮
     * @return 日期类型 LocalDateTime
     */
    public static LocalDateTime convertToLocalDateTime(long timestamp) {
        return Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    /**
     * 将日期类型 LocalDateTime 转换成时间戮
     *
     * @param localDateTime 日期类型 LocalDateTime
     * @return 时间戮
     */
    public static long convertToTimestamp(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
    }

    /**
     * 将日期类型 LocalDate 转换成时间戮
     *
     * @param localDate 日期类型 LocalDate
     * @return 时间戮
     */
    public static long convertToTimestamp(LocalDate localDate) {
        return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static String formatDate(Date date, String pattern) {
        if (StringHelper.isNullOrEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

}
