package com.fshark.overseas.advert.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zhouml
 * @date 2018-11-29
 */
public class DateUtils {

    static DateTimeFormatter YMDHMS_FORMATTER = DateTimeFormatter.ofPattern(Constants.YMDHMS);
    static DateTimeFormatter YMDHMS_MILLIS_FORMATTER = DateTimeFormatter.ofPattern(Constants.YMDHMS_MILLIS);
    static DateTimeFormatter HH_FORMATTER = DateTimeFormatter.ofPattern(Constants.HH);
    static DateTimeFormatter YMD_FORMATTER = DateTimeFormatter.ofPattern(Constants.YMD);


    public static String formatAsYMD(Date ts) {
        if (ts == null){
            return "-1";
        }
        LocalDateTime localDateTime = ts.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return localDateTime.format(YMD_FORMATTER);
    }

    public static String formatAsYMDHMS(Date ts) {
        LocalDateTime localDateTime = ts.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return localDateTime.format(YMDHMS_FORMATTER);
    }


    /**
     * 将 yyyy-MM-dd HH:mm:ss数据转换为yyyyMMdd
     *
     * @param ts
     * @return
     */
    public static String formatAsYMD(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_FORMATTER);
        return parse.format(YMD_FORMATTER);
    }


    public static String formatAsHH(Date ts) {
        LocalDateTime localDateTime = ts.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return localDateTime.format(HH_FORMATTER);
    }

    public static String formatAsHH(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_FORMATTER);
        return parse.format(HH_FORMATTER);
    }

    public static Date formatAsDate(String ts) {
        if (ts.length() > 19) {
            return formatHMSSAsDate(ts);
        }
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_FORMATTER);
        return Date.from(parse.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date formatHMSSAsDate(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_MILLIS_FORMATTER);
        return Date.from(parse.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String formatHMSSAsYMD(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_MILLIS_FORMATTER);
        return parse.format(YMD_FORMATTER);
    }

    public static String formatHMSSAsHH(String ts) {
        LocalDateTime parse = LocalDateTime.parse(ts, YMDHMS_MILLIS_FORMATTER);
        return parse.format(HH_FORMATTER);
    }


    /**
     * @return 相差天数
     */
    public static long daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static long daysBetween(LocalDateTime smdate, Date bdate) throws ParseException {
        Duration between = Duration.between(smdate, dateToLocalDateTime(bdate));
        return between.toDays();
    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        if (localDateTime == null) return null;
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        if (date == null) return null;
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


}

