package shop.alien.lawyer.payment;

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 lyx
 * @date 2025/11/20
 */
@Slf4j
@Component
public class PaymentStrategyFactory {

    @Autowired
    private List<PaymentStrategy> paymentStrategies;

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

        /**
         * 初始化策略映射
         */
        @PostConstruct
        public void init() {
            if (paymentStrategies != null && !paymentStrategies.isEmpty()) {
                for (PaymentStrategy strategy : paymentStrategies) {
                    strategyMap.put(strategy.getType(), strategy);
                    log.info("注册支付策略: {} -> {}", strategy.getType(), strategy.getClass().getSimpleName());
                }
            }
        }

    /**
     * 根据类型获取OCR策略
     *
     * @param type OCR类型
     * @return OCR策略实例
     * @throws IllegalArgumentException 如果类型不存在
     */
    public PaymentStrategy getStrategy(String type) {
        PaymentStrategy strategy = strategyMap.get(type);
        if (strategy == null) {
            throw new IllegalArgumentException("不支持的支付类型: " + type);
        }
        return strategy;
    }

    /**
     * 检查是否支持指定的OCR类型
     *
     * @param type OCR类型
     * @return 是否支持
     */
    public boolean supports(String type) {
        return strategyMap.containsKey(type);
    }
}
