package com.storlead.user.util;


import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.util.Locale;

/**
 * Spring 上下文 bean 获取工具
 *
 * @author blank
 * @date 2020-12-23 上午 9:37
 */
@Slf4j
@Lazy(false)
@Component
public class UserSpringContextHolder implements ApplicationContextAware, DisposableBean {


    @Getter
    public static ApplicationContext applicationContext;

    /**
     * 获取当前登录用户
     *
     * @return com.storlead.model.entity.User
     * @author blank
     * @date 2021-1-28 上午 11:19
     */
//    public static User getCurrentUser() {
//        IUserService userService = applicationContext.getBean(IUserService.class);
//        SecurityContext context = SecurityContextHolder.getContext();
//        Authentication authentication = context.getAuthentication();
//        if (authentication != null) {
//            return userService.getUserByUserName(authentication.getName());
//        } else {
//            return null;
//        }
//    }

    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        if (UserSpringContextHolder.applicationContext == null) {
            UserSpringContextHolder.applicationContext = applicationContext;
        }
    }

    /**
     * 获取 Bean
     *
     * @param beanName bean 名称
     * @return Bean 对象
     * @author blank
     * @since 2018-10-10 上午11:23
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName) {
        checkApplicationContext();
        return (T) applicationContext.getBean(beanName);
    }


    /**
     * 获取 Bean
     *
     * @param clazz bean 类
     * @return Bean 对象
     * @author blank
     * @since 2018-10-10 上午11:23
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        checkApplicationContext();
        return applicationContext.getBean(clazz);
    }


    /**
     * 国际化使用
     * 获取 message
     *
     * @param key key
     * @return key 对应的 value
     * @author blank
     * @since 2018-10-10 上午11:23
     */
    public static String getMessage(String key) {
        checkApplicationContext();
        return applicationContext.getMessage(key, null, Locale.getDefault());
    }


    /**
     * 获取当前环境
     *
     * @return 对应 profile name [dev,test,prod]
     * @author blank
     * @since 2018-10-10 上午11:24
     */
    public static String getActiveProfile() {
        checkApplicationContext();
        return applicationContext.getEnvironment().getActiveProfiles()[0];
    }


    @Override
    public void destroy() {
        UserSpringContextHolder.clearHolder();
    }

    private static void checkApplicationContext() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicationContext未注入,请在SpringBoot启动类中注册SpringContextHolder");
        }
    }

    /**
     * 清除SpringContextHolder中的ApplicationContext为Null
     */
    private static void clearHolder() {
        log.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
        applicationContext = null;
    }
}
