package org.xyzh.achievement.checker;

import org.xyzh.common.core.enums.AchievementConditionType;
import org.xyzh.common.core.enums.AchievementEventType;
import org.xyzh.common.core.event.AchievementEvent;
import org.xyzh.common.dto.usercenter.TbAchievement;

/**
 * @description 成就检测器抽象基类
 * @filename AbstractAchievementChecker.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-24
 */
public abstract class AbstractAchievementChecker implements AchievementChecker {

    /**
     * 支持的条件类型
     */
    protected abstract AchievementConditionType getSupportedConditionType();

    /**
     * 支持的事件类型（可多个）
     */
    protected abstract AchievementEventType[] getSupportedEventTypes();

    @Override
    public boolean supports(TbAchievement achievement) {
        if (achievement == null || achievement.getConditionType() == null) {
            return false;
        }
        return achievement.getConditionType().equals(getSupportedConditionType().getCode());
    }

    @Override
    public boolean supportsEventType(AchievementEventType eventType) {
        if (eventType == null) {
            return false;
        }
        for (AchievementEventType supportedType : getSupportedEventTypes()) {
            if (supportedType == eventType) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean check(AchievementEvent event, TbAchievement achievement, Integer currentProgress) {
        if (event == null || achievement == null) {
            return false;
        }
        
        Integer newProgress = calculateNewProgress(event, currentProgress);
        Integer targetValue = achievement.getConditionValue();
        
        return newProgress != null && targetValue != null && newProgress >= targetValue;
    }

    @Override
    public Integer calculateNewProgress(AchievementEvent event, Integer currentProgress) {
        if (currentProgress == null) {
            currentProgress = 0;
        }
        
        Integer eventValue = event.getEventValue();
        if (eventValue == null) {
            eventValue = 1; // 默认增加1
        }
        
        return currentProgress + eventValue;
    }
}

