package com.zanxiang.game.module.base.util;

import com.zanxiang.module.util.DateUtil;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends DateUtil {

    /**
     * 验证当前时间是否为当天
     *
     * @param time 时间
     * @return boolean
     */
    public static boolean isToday(LocalDateTime time) {
        LocalDateTime startTime = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.now().with(LocalTime.MAX);
        //如果大于今天的开始日期，小于今天的结束日期
        return time.isAfter(startTime) && time.isBefore(endTime);
    }

    /**
     * 验证当前时间是否为当月
     *
     * @param time 时间
     * @return boolean
     */
    public static boolean isThisMonth(LocalDateTime time) {
        LocalDate localDate = time.toLocalDate();
        LocalDate now = LocalDate.now();
        return localDate.isAfter(now.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth())) &&
                localDate.isBefore(now.plusMonths(1).with(TemporalAdjusters.firstDayOfMonth()));
    }

    /**
     * 验证当前时间是否为本周
     *
     * @param time 时间
     * @return boolean
     */
    public static boolean isThisWeek(LocalDateTime time) {
        LocalDateTime now = LocalDateTime.now();
        int dayOfWeek = now.getDayOfWeek().getValue();
        LocalDateTime weekStart = now.minusDays(dayOfWeek - 1).with(LocalTime.MIN);
        LocalDateTime weekEnd = now.plusDays(7 - dayOfWeek).with(LocalTime.MAX);
        return time.isAfter(weekStart) && time.isBefore(weekEnd);
    }

    /**
     * 注册支付时间差异
     *
     * @param regTime 注册时间
     * @param payTime 支付时间
     * @return {@link Long}
     */
    public static Long getRegPayTimeDiff(LocalDateTime regTime, LocalDateTime payTime) {
        if (payTime == null || regTime == null) {
            return null;
        }
        // 间隔秒数
        return Duration.between(regTime, payTime).getSeconds();
    }
}
