package incheon.com.cmm.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Spring ApplicationContext를 정적으로 접근 가능하게 하는 유틸리티 클래스
 *
 * <p>주요 용도:
 * <ul>
 *   <li>JSP EL Function에서 Spring Bean 접근</li>
 *   <li>정적 컨텍스트에서 Spring Bean 조회</li>
 *   <li>Util 클래스에서 Service 호출</li>
 * </ul>
 *
 * @author Incheon City Development Team
 * @since 2025-01-15
 */
@Component
@Lazy(false)
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext context;

    /**
     * Spring이 ApplicationContext를 주입할 때 호출
     * @param applicationContext Spring ApplicationContext
     * @throws BeansException Bean 예외
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextProvider.context = applicationContext;
    }

    /**
     * ApplicationContext 조회
     * @return Spring ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return context;
    }

    /**
     * Bean 조회 (타입 기반)
     *
     * <p>사용 예시:
     * <pre>{@code
     * ComCdService service = ApplicationContextProvider.getBean(ComCdService.class);
     * String codeName = service.getCodeName("GNDR_CD", "M");
     * }</pre>
     *
     * @param <T> Bean 타입
     * @param beanClass Bean 클래스
     * @return Bean 인스턴스
     * @throws IllegalStateException ApplicationContext가 초기화되지 않은 경우
     */
    public static <T> T getBean(Class<T> beanClass) {
        if (context == null) {
            throw new IllegalStateException("ApplicationContext가 초기화되지 않았습니다.");
        }
        return context.getBean(beanClass);
    }

    /**
     * Bean 조회 (이름 기반)
     *
     * <p>사용 예시:
     * <pre>{@code
     * ComCdService service = (ComCdService) ApplicationContextProvider.getBean("comCdService");
     * }</pre>
     *
     * @param beanName Bean 이름
     * @return Bean 인스턴스
     * @throws IllegalStateException ApplicationContext가 초기화되지 않은 경우
     */
    public static Object getBean(String beanName) {
        if (context == null) {
            throw new IllegalStateException("ApplicationContext가 초기화되지 않았습니다.");
        }
        return context.getBean(beanName);
    }

    /**
     * Bean 조회 (이름 + 타입 기반)
     *
     * <p>사용 예시:
     * <pre>{@code
     * ComCdService service = ApplicationContextProvider.getBean("comCdService", ComCdService.class);
     * }</pre>
     *
     * @param <T> Bean 타입
     * @param beanName Bean 이름
     * @param beanClass Bean 클래스
     * @return Bean 인스턴스
     * @throws IllegalStateException ApplicationContext가 초기화되지 않은 경우
     */
    public static <T> T getBean(String beanName, Class<T> beanClass) {
        if (context == null) {
            throw new IllegalStateException("ApplicationContext가 초기화되지 않았습니다.");
        }
        return context.getBean(beanName, beanClass);
    }
}
