package com.izouma.nineth.utils;

import com.izouma.nineth.exception.BusinessException;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

public class DateTimeUtils {
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String T_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
    public static final String DATE_FORMAT      = "yyyy-MM-dd";

    public static LocalDate toLocalDate(long ts) {
        if (String.valueOf(ts).length() > 10) {
            return LocalDate.from(Instant.ofEpochMilli(ts));
        } else {
            return LocalDate.from(Instant.ofEpochSecond(ts));
        }
    }

    public static LocalDateTime toLocalDateTime(long ts) {
        if (String.valueOf(ts).length() > 10) {
            return LocalDateTime.from(Instant.ofEpochMilli(ts));
        } else {
            return LocalDateTime.from(Instant.ofEpochSecond(ts));
        }
    }

    public static LocalDate toLocalDate(String str, String fmt) {
        return LocalDate.from(DateTimeFormatter.ofPattern(fmt).parse(str));
    }

    public static LocalDateTime toLocalDateTime(String str, String fmt) {
        return LocalDateTime.from(DateTimeFormatter.ofPattern(fmt).parse(str));
    }

    public static String format(TemporalAccessor temporal, String fmt) {
        return DateTimeFormatter.ofPattern(fmt).format(temporal);
    }

    /**
     * 获取指定日期到下一天的秒数
     * @param currentDate
     * @return
     */
    public static Integer getRemainSecondsOneDay(Date currentDate) {
        //使用plusDays加传入的时间加1天，将时分秒设置成0
        LocalDateTime midnight = LocalDateTime.ofInstant(new Date().toInstant(),
                        ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault());
        //使用ChronoUnit.SECONDS.between方法，传入两个LocalDateTime对象即可得到相差的秒数
        long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
        return (int) seconds;
    }

    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static long toTimestamp(TemporalAccessor temporal) {
        return Instant.from(temporal).toEpochMilli();
    }

    public static int getAge(String idNo) {
        if (!StringUtils.hasText(idNo)){
            throw new BusinessException("身份证号码不能为空");
        }
        // 得到年份
        String year = idNo.substring(6).substring(0, 4);
        // 得到月份
        String month = idNo.substring(10).substring(0, 2);
        //得到日
        String day = idNo.substring(12).substring(0, 2);
        Date date = new Date();// 得到当前的系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 当前年份
        String nowYear = format.format(date).substring(0, 4);
        // 月份
        String nowMonth = format.format(date).substring(5, 7);
        String nowDay = format.format(date).substring(8, 10);
        int age = Integer.parseInt(nowYear) - Integer.parseInt(year);
        if (Integer.parseInt(month) < Integer.parseInt(nowMonth)) {
            // 当前月份大于用户出身的月份表示已过生
            age = age + 1;
        } else if (Integer.parseInt(month) == Integer.parseInt(nowMonth) && Integer.parseInt(nowDay) > Integer.parseInt(day)) {
            age = age + 1;
        }
        return age;
    }

}
