package com.bonus.auth.factory;

import com.bonus.auth.config.LoginType;
import com.bonus.auth.service.*;
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 LoginStrategyFactory {
    private final Map<LoginType, LoginStrategy> strategyMap;

    @Autowired
    public LoginStrategyFactory(List<LoginStrategy> strategies) {
        strategyMap = new HashMap<>();
        // 通过类型查找对应的策略
        strategies.forEach(strategy -> {
            if (strategy instanceof UsernamePasswordLoginStrategy) {
                strategyMap.put(LoginType.USERNAME_PASSWORD, strategy);
            } else if (strategy instanceof PhonePasswordLoginStrategy) {
                strategyMap.put(LoginType.PHONE_PASSWORD, strategy);
            } else if (strategy instanceof PhoneOtpLoginStrategy) {
                strategyMap.put(LoginType.PHONE_OTP, strategy);
            } else if (strategy instanceof EmailPasswordLoginStrategy) {
                strategyMap.put(LoginType.EMAIL_PASSWORD, strategy);
            } else if (strategy instanceof EmailOtpLoginStrategy) {
                strategyMap.put(LoginType.EMAIL_OTP, strategy);
            } else if (strategy instanceof KitchenPhonePasswordLoginStrategy) {
                strategyMap.put(LoginType.KITCHEN_PHONE_PASSWORD, strategy);
            }
            // 继续添加其他策略
        });
    }

    public LoginStrategy getStrategy(LoginType loginType) {
        return strategyMap.get(loginType);
    }
}

