package com.fs.store.service.channel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class PaymentHandlerHolder {

    private static ApplicationContext applicationContext;

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext) {
        PaymentHandlerHolder.applicationContext = applicationContext;
    }

    public static PaymentHandler findBest(String code) {
        String[] beanNames = applicationContext.getBeanNamesForType(PaymentHandler.class);

        for (String beanName : beanNames) {
            PaymentHandler handler = (PaymentHandler) applicationContext.getBean(beanName);
            if (handler.support(code)) {
                return handler;
            }
        }
        return null;
    }
}
