package com.xydl.cac.util;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateUtil {
    public final static String defaultDatePattern = "yyyy-MM-dd HH:mm:ss";
    public static DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern(defaultDatePattern);

    /**
     * 获得默认的 date pattern
     */
    public static String getDatePattern() {
        return defaultDatePattern;
    }

    /**
     * 使用预设Format格式化Date成字符串
     */
    public static String format(Date date) {
        return format(date, getDatePattern());
    }

    /**
     * 使用参数Format格式化Date成字符串
     */
    public static String format(Date date, String pattern) {
        String returnValue = "";

        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }

        return (returnValue);
    }

    /**
     * 使用预设格式将字符串转为Date
     */
    public static Date parse(String strDate) throws ParseException {
        return parse(strDate, getDatePattern());
    }

    /**
     * 使用参数Format将字符串转为Date
     */
    public static Date parse(String strDate, String pattern) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.parse(strDate);
    }

    public static Date getTodayZero() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String str = df.format(new Date());
        return df.parse(str);
    }

    public static Date getDayZero(Date date) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String str = df.format(date);
        return df.parse(str);
    }

    public static Date getMonthZero() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        String str = df.format(new Date());
        return df.parse(str);
    }

    /**
     * 在日期上增加数个整日(n为负数则是减少数日)
     */
    public static Date addDay(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, n);
        return cal.getTime();
    }

    public static Date addMonth(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, n);
        return cal.getTime();
    }

    public static Date addHour(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, n);
        return cal.getTime();
    }

    public static long getDifferenceInDays(Date startDate, Date endDate) {
        LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return ChronoUnit.DAYS.between(startLocalDate, endLocalDate);
    }

    public static String fromZoneUTCToLocal(String str) throws ParseException {
        String time = str.replace("T", " ")
                .replace("Z", "").replace("z", "");
        Date date = parse(time);
        time = format(date);
        LocalDateTime localtime = LocalDateTime.parse(time, defaultFormatter);
        if (localtime.getYear() < 2000) {
            return null;
        }
        ZonedDateTime zonedDateTime = localtime.atZone(ZoneOffset.UTC);
        ZonedDateTime convertedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault());
        String result = convertedDateTime.format(defaultFormatter);
        return result;
    }

    public static long secondDiff(String str1, String str2) {
        try {
            Date date1 = parse(str1);
            Date date2 = parse(str2);
            long sec1 = date1.getTime();
            long sec2 = date2.getTime();
            if (sec1 >= sec2) {
                long sec = sec1 - sec2;
                return sec / 1000;
            } else {
                long sec = sec2 - sec1;
                return sec / 1000;
            }
        } catch (Exception e) {
            return -1;
        }
    }

}
