package com.elab.spring.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.ArrayUtils;
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.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * Spring拓展的工具类
 *
 * @author liukx
 */
@Component
public class SpringUtils implements ApplicationContextAware {
    private static Logger logger = LoggerFactory.getLogger(SpringUtils.class);
    private static ApplicationContext applicationContext;

    public SpringUtils() {
    }

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

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(String name) {
        return (T)applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }

    public static <T> Map<String, T> getBeansOfType(Class<T> type) {
        return applicationContext.getBeansOfType(type);
    }

    public static String[] getBeanNamesForType(Class<?> type) {
        return applicationContext.getBeanNamesForType(type);
    }

    public static String getProperty(String key) {
        return applicationContext.getEnvironment().getProperty(key);
    }

    public static String[] getActiveProfiles() {
        return applicationContext.getEnvironment().getActiveProfiles();
    }

    public static String getActiveProfile() {
        String[] activeProfiles = getActiveProfiles();
        return ArrayUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
    }

    /**
     * 获取对象的属性字段的值并转换成json
     *
     * @param clazz
     * @param field
     * @return
     */
    public static <T> T getObjectFieldValue(Class<?> clazz, String field, Class<T> valueClazz) {
        Object bean = null;
        try {
            bean = getBean(clazz);
        } catch (Exception e) {
            logger.warn("获取bean失败 : " + e.getMessage());
            return null;
        }
        return getFieldObjectValue(bean, field, valueClazz);
    }

    public static <T> T getFieldObjectValue(Object bean, String field, Class<T> valueClazz) {
        try {
            Field declaredField = bean.getClass().getDeclaredField(field);
            declaredField.setAccessible(true);
            return (T)declaredField.get(bean);
        } catch (NoSuchFieldException e) {
            logger.warn("找不到对应的字段" + e.getMessage());
        } catch (Exception e) {
            logger.warn("获取字段异常:" + e.getMessage());
        }
        return null;
    }

}
