package shop.alien.store.strategy.merchantPayment;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 商户支付策略工厂（预订订单等，按门店配置）
 *
 * @author system
 */
@Slf4j
@Component
public class MerchantPaymentStrategyFactory {

    @Autowired
    private List<MerchantPaymentStrategy> merchantPaymentStrategies;

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

    @PostConstruct
    public void init() {
        if (merchantPaymentStrategies != null && !merchantPaymentStrategies.isEmpty()) {
            for (MerchantPaymentStrategy strategy : merchantPaymentStrategies) {
                strategyMap.put(strategy.getType(), strategy);
                log.info("注册商户支付策略: {} -> {}", strategy.getType(), strategy.getClass().getSimpleName());
            }
        }
    }

    public MerchantPaymentStrategy getStrategy(String type) {
        MerchantPaymentStrategy strategy = strategyMap.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("不支持的商户支付类型: " + type);
        }
        return strategy;
    }

    public boolean supports(String type) {
        return strategyMap.containsKey(type);
    }
}
