package shop.alien.store.strategy.platformMembershipPayment;

import org.springframework.stereotype.Component;
import shop.alien.util.common.constant.PaymentEnum;

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

@Component
public class PlatformMembershipPaymentStrategyFactory {

    private final Map<String, PlatformMembershipPaymentStrategy> strategyMap = new HashMap<>();

    public PlatformMembershipPaymentStrategyFactory(List<PlatformMembershipPaymentStrategy> strategies) {
        if (strategies != null) {
            for (PlatformMembershipPaymentStrategy s : strategies) {
                strategyMap.put(s.getType(), s);
            }
        }
    }

    public PlatformMembershipPaymentStrategy getStrategy(String type) {
        if (type == null || type.trim().isEmpty()) {
            type = PaymentEnum.ALIPAY.getType();
        }
        PlatformMembershipPaymentStrategy s = strategyMap.get(type);
        if (s == null) {
            throw new IllegalArgumentException("不支持的支付方式: " + type);
        }
        return s;
    }
}
