package com.ym.mec.thirdparty.user.realname;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.ym.mec.thirdparty.exception.ThirdpartyException;
import com.ym.mec.thirdparty.user.realname.provider.LinkfaceRealnameAuthenticationPlugin;

@Component
public class RealnameAuthenticationPluginContext implements ApplicationContextAware, InitializingBean {

	private ApplicationContext applicationContext;

	private final Map<String, String> mapper = new HashMap<String, String>() {

		/**
		 * 
		 */
		private static final long serialVersionUID = -7516427405929382747L;

		{
			put(LinkfaceRealnameAuthenticationPlugin.getName(), StringUtils.uncapitalize(LinkfaceRealnameAuthenticationPlugin.class.getSimpleName()));
		}
	};

	@Override
	public void afterPropertiesSet() throws Exception {

	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	public RealnameAuthenticationPlugin getRealnameAuthenticationPlugin(String vendors) {

		String beanId = mapper.get(vendors);

		if (StringUtils.isBlank(beanId)) {
			throw new ThirdpartyException("实名认证提供商不存在");
		}

		return applicationContext.getBean(beanId, RealnameAuthenticationPlugin.class);
	}
}
