package com.ycwl.basic.service.pc.strategy;

import com.ycwl.basic.service.pc.strategy.impl.DefaultRematchStrategy;
import com.ycwl.basic.service.pc.strategy.impl.RematchMode1Strategy;
import com.ycwl.basic.service.pc.strategy.impl.RematchMode5Strategy;
import com.ycwl.basic.service.pc.strategy.impl.RematchMode9Strategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 重匹配策略工厂
 * 根据模式值获取对应的策略实例
 *
 * @author longbinbin
 * @date 2025-01-31
 */
@Slf4j
@Component
public class RematchStrategyFactory {

    @Autowired
    private List<RematchModeStrategy> strategies;

    private final Map<Integer, RematchModeStrategy> strategyMap = new HashMap<>();

    @PostConstruct
    public void init() {
        for (RematchModeStrategy strategy : strategies) {
            strategyMap.put(strategy.getMode(), strategy);
            log.debug("注册重匹配策略: mode={}, class={}",
                     strategy.getMode(), strategy.getClass().getSimpleName());
        }
    }

    /**
     * 根据模式值获取对应的策略
     *
     * @param mode 模式值(0-默认, 1-模式1, 5-模式5, 9-模式9)
     * @return 对应的策略实例,如果没有找到则返回默认策略
     */
    public RematchModeStrategy getStrategy(Integer mode) {
        if (mode == null) {
            return strategyMap.getOrDefault(0, new DefaultRematchStrategy());
        }

        RematchModeStrategy strategy = strategyMap.get(mode);
        if (strategy == null) {
            log.warn("未找到重匹配模式{}对应的策略,使用默认策略", mode);
            return strategyMap.getOrDefault(0, new DefaultRematchStrategy());
        }

        return strategy;
    }
}
