package jnpf.attendance.service.filter.impl;

import jnpf.attendance.service.filter.RuleFilter;
import jnpf.attendance.service.filter.RuleFilterContext;
import jnpf.attendance.service.filter.RuleFilterResult;
import jnpf.base.UserInfo;
import jnpf.entity.attendance.FtbAttendanceDailyRule;
import jnpf.enums.attendance.AttendanceTypeEnum;
import jnpf.util.ConstantUtil;
import jnpf.util.DateDetail;
import org.springframework.stereotype.Component;

/**
 * 跨日过滤器
 *
 * @author yanwenfu
 * @create 2025-09-25
 */
@Component
public class CrossDayFilter implements RuleFilter {

    @Override
    public RuleFilterResult keepRule(FtbAttendanceDailyRule rule, RuleFilterContext context, UserInfo userInfo) {

        if (rule.getAttendanceType().equals(AttendanceTypeEnum.LEAVE.getCode())) {
            return RuleFilterResult.discard();
        }
        // 昨日的休不展示
        if (rule.getAttendanceType().equals(AttendanceTypeEnum.REST.getCode()) && !DateDetail.checkSameDay(context.getCurDate(), rule.getDay())) {
            return RuleFilterResult.discard();
        }
        // 今日的休
        if (DateDetail.checkSameDay(context.getCurDate(), rule.getDay())) {
            if (rule.getAttendanceType().equals(AttendanceTypeEnum.DEFAULT.getCode()) || rule.getAttendanceType().equals(AttendanceTypeEnum.REST.getCode())) {
                return RuleFilterResult.keep();
            }
        }
        // 上班打卡时间是今天的, 都留下
        if (DateDetail.checkSameDay(context.getCurDate(), rule.getInPoint())) {
            return RuleFilterResult.keep();
        }
        // 下班结束时间是今天的, 都留下
        if (null != rule.getOutLackPoint() && DateDetail.checkSameDay(context.getCurDate(), rule.getOutLackPoint())) {
            if (!DateDetail.checkSameDay(context.getCurDate(), rule.getDay()) && rule.getAttendanceType().equals(ConstantUtil.RULE_TYPE_REST)) {
                return RuleFilterResult.discard();
            }
            return RuleFilterResult.keep();
        }
        /*if (DateDetail.checkSameDay(rule.getDay(), context.getCurDate())) {
            return RuleFilterResult.terminate();
        }*/
        return RuleFilterResult.discard();
    }

    @Override
    public int getOrder() {
        return 3;
    }
}
