package com.ywt.alipaympapi.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    private static final String[] WEEK_DAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

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

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

    public static final String DADE_FROMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";

    public static final String DADE_FROMAT_CYMD = "yyyy年M月d日";

    public static final String DADE_FROMAT_CYMDHM = "yyyy年M月d日 HH:mm";


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

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

        if (StringHelper.isNullOrEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }

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

    /**
     * 将时间戳转换成日期类型
     *
     * @param timestamp 时间戳
     * @return
     */
    public static Date convertTimestampToDate(long timestamp) {
        return new Date(timestamp);
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param pattern
     * @return
     */
    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);
    }

    /**
     * 当前日期是星期几
     *
     * @param date
     * @return
     */
    public static String getWeekOfDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        return sdf.format(date);
    }

    /**
     * 当前日期是星期几
     *
     * @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];
    }

    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;
        }
    }

    public static LocalDate convertToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return instant.atZone(zoneId).toLocalDate();
    }

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

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

    /**
     * 得到相减的天数
     *
     * @param beginDateStr 开始时间
     * @param endDateStr   截止时间
     * @return 天数
     */
    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(DADE_FROMAT_YMD);
        java.util.Date beginDate;
        java.util.Date endDate;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 得到距离当前的天数
     *
     * @param beginDateStr 开始日期
     * @return 天数
     */
    public static long getCurrentDay(String beginDateStr) {
        return getDaySub(beginDateStr, new SimpleDateFormat(DADE_FROMAT_YMD).format(new Date()));
    }

    /**
     * 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;
    }

    /**
     * 修改日期，得到当前
     *
     * @param startDate 开始的日期
     * @param num       修改的天数
     * @return 修改后的日期 {@link Date}
     */
    public static Date getChangeDayOfDate(String startDate, int num, String pattern) {
        Date date = stringToDate(startDate, pattern);
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DAY_OF_YEAR, num);
        //最后一天格式化
        return ca.getTime();
    }

    /**
     * 修改日期，得到当前
     *
     * @param startDate 开始的日期
     * @param num       修改的天数
     * @return 修改后的日期 {@link String}
     */
    public static String getChangeDayOfDateStr(String startDate, int num, String pattern) {
        Date date = getChangeDayOfDate(startDate, num, pattern);
        return formatDate(date, pattern);
    }
}
