package com.bonus.auth.factory;

import com.bonus.auth.config.VerificationCodeType;
import com.bonus.auth.service.LoginVerificationCodeSender;
import com.bonus.auth.service.RegisterVerificationCodeSender;
import com.bonus.auth.service.VerificationCodeStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bonus
 */
@Component
public class VerificationCodeStrategyFactory {
    private final Map<VerificationCodeType, VerificationCodeStrategy> strategyMap;

    @Autowired
    public VerificationCodeStrategyFactory(List<VerificationCodeStrategy> strategies) {
        strategyMap = new HashMap<>();
        // 通过类型查找对应的策略
        strategies.forEach(strategy -> {
            if (strategy instanceof LoginVerificationCodeSender) {
                strategyMap.put(VerificationCodeType.LOGIN, strategy);
            } else if (strategy instanceof RegisterVerificationCodeSender) {
                strategyMap.put(VerificationCodeType.REGISTER, strategy);
            }
            // 继续添加其他策略
        });
    }

    public VerificationCodeStrategy getStrategy(VerificationCodeType verificationCodeType) {
        return strategyMap.get(verificationCodeType);
    }
}
