package com.leanwo.activiti.util;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import com.leanwo.activiti.exception.BeanNotInjectException;

@Configuration
public class SpringUtil implements ApplicationContextAware {
	private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
	private static ApplicationContext applicationContext;

	/**
	 * 是否初始化成功
	 * @return
	 */
	public static boolean initSuccess() {
		return applicationContext != null;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		applicationContext = arg0;
		String[] names = beanNames();
		// for(String name : names){
		// logger.info(name );
		// }
		logger.info("IOC容器共管理:" + names.length + "个对象。");
		logger.info("Spring容器初始化成功。");

		logger.info("开始初始化工作流");
		logger.info("Activiti初始化成功。");
	}

	public static ApplicationContext getApplicationContext() {
		checkApplicationContext();
		return applicationContext;
	}

	/**
	 * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		checkApplicationContext();
		return (T) applicationContext.getBean(name);
	}

	/**
	 * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(Class<T> clazz) {
		checkApplicationContext();
		return (T) applicationContext.getBeansOfType(clazz);
	}

	/**
	 * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	public static Object getSingleBean(Class clazz) {
		checkApplicationContext();
		Map<String, Object> map = applicationContext.getBeansOfType(clazz);
		if (map == null || map.size() == 0) {
			throw new BeanNotInjectException(clazz.getName() + "未注入。");
		} else if (map.size() > 1) {
			throw new IllegalStateException(clazz.getName() + "存在" + map.size() + "个对象。");
		} else {
			Iterator iterator = map.entrySet().iterator();
			Map.Entry entry = (Map.Entry) iterator.next();
			return entry.getValue();
		}
	}
	
	/**
	 * 从静态变量ApplicationContext中取得Bean的数量
	 */
	public static Integer countBean(Class clazz) {
		
		Map<String, Object> map = applicationContext.getBeansOfType(clazz);
		if (map == null || map.size() == 0) {
			return 0;
		} else {
			return map.size();
		}
	}

	/**
	 * 从当前IOC获取标识了annotation注释的对象
	 * 
	 * @param annotation
	 * @return
	 */
	public static Collection<Object> beansByAnnotation(Class<? extends Annotation> annotation) {
		checkApplicationContext();
		return applicationContext.getBeansWithAnnotation(annotation).values();
	}

	/**
	 * 获取当前IOC所有Bean的名称
	 * 
	 * @return
	 */
	public static String[] beanNames() {
		checkApplicationContext();
		return applicationContext.getBeanDefinitionNames();

	}

	/**
	 * 获取当前IOC中beanName对应的类
	 * 
	 * @param beanName
	 * @return
	 */
	public static Class getType(String beanName) {
		checkApplicationContext();
		return applicationContext.getType(beanName);
	}

	/**
	 * 从当前IOC获取标识了annotation注释的对象
	 * 
	 * @param id
	 * @return
	 */
	public static Object getObject(String id) {
		checkApplicationContext();
		return applicationContext.getBean(id);
	}

	private static void checkApplicationContext() {
		if (applicationContext == null) {
			throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
		}
	}
	
	public static <T> Collection<T> getBeansByClass(Class<T> clazz) {
		checkApplicationContext();
		Map<String, T> map = applicationContext.getBeansOfType(clazz);
		if(map == null || map.isEmpty()) {
			return null;
		}
		return map.values();
	}
	
	 public static <T> T getObject(Class<T> clazz){
         return applicationContext.getBean(clazz);
     }
}