package com.brilliance.mda.runtime.mda.driver.compile.processor;

import com.brilliance.mda.runtime.mda.IModule;
import com.brilliance.mda.runtime.mda.driver.MdaEnv;
import com.brilliance.mda.runtime.mda.driver.MdaScanner;
import com.brilliance.mda.runtime.mda.driver.compile.component.AbstractAutoCompileScopeEmitter;
import com.brilliance.mda.runtime.mda.driver.compile.component.ModuleRuleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ModuleListAnnotationProcessor extends TransAnnotationProcessor {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public ModuleListAnnotationProcessor(){
        super(false);
    }
    public void buildModuleListEmitter(Class moduleClass, ModuleRuleContext moduleRuleContext){
        try {
            //初始化上下文
            init(moduleClass);
            this.setBeanDefinitionRegistry(MdaEnv.getBean(MdaScanner.class).getBeanDefinitionRegistry());
            IModule rootIns = (IModule) moduleClass.newInstance();
            this.makeInstance(rootIns,true);
            //设置为支持使用emitter了
            moduleRuleContext.setUseEmitter(true);
            //设置RuleContext
            this.setModuleRuleContext(moduleRuleContext);
            //注册Emitter
            registerEmitterToBeanFactory();
            //合并上下文
            this.mergeTransRules();
            //rule 排序
            this.getModuleRuleContext().sortRule();
            this.pushToProcessQueue();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void mergeTransRules(){
        //scopeEmitter需要设置上下文
        AbstractAutoCompileScopeEmitter abstractAutoCompileScopeEmitter = (AbstractAutoCompileScopeEmitter) MdaEnv.getBean(this.getEmitterKey());
        abstractAutoCompileScopeEmitter.acceptContext(this.getInstatnce());
        super.mergeTransRules();
        abstractAutoCompileScopeEmitter.removeContext();
    }
}
