package com.brilliance.mda.runtime.mda.impl;

import com.brilliance.mda.runtime.mda.IBaseObject;
import com.brilliance.mda.runtime.mda.IModule;
import com.brilliance.mda.runtime.mda.driver.MdaDriver;

import java.util.Stack;

public abstract class AbstractScopeEmitter extends AbstractRuleEmitter{
    private ThreadLocal<Stack<IModule>> scopeContext = new ThreadLocal<>();

    public void acceptContext(IModule module){
        Stack<IModule> stack = scopeContext.get();
        if(scopeContext.get() == null){
            stack = new Stack<>();
            scopeContext.set(stack);
        }
        stack.push(module);
    }
    public void removeContext(){
        Stack<IModule> stack = scopeContext.get();
        if(stack == null){
            return;
        }
        stack.pop();
        if(stack.empty()){
            scopeContext.remove();
        }

    }
    public IModule getContext(){
        Stack<IModule> stack = scopeContext.get();
        if(stack == null){
            return null;
        }
        return stack.peek();
    }

    @Override
    public String getRealPath(String path)
    {
        if(path==null) return "";
        String realPath = eqMap.get(path);
        if(realPath != null)
            return realPath;
        //计算path
        IBaseObject baseObject = MdaDriver.getBaseObject(getContext(),path);
        if(baseObject != null )
            realPath = getModuleListFieldAbsolutePath(getContext(),baseObject);
        else
        {
            int lastIndex = path.lastIndexOf('.');
            if(lastIndex > 0)
            {
                //取父级模型，因为按钮在实际中不存在
                baseObject = MdaDriver.getBaseObject(getContext(),path.substring(0,lastIndex));
                if(baseObject!=null && baseObject instanceof IModule)
                {
                    realPath = ((IModule)baseObject).getFieldPath(path.substring(lastIndex+1));
                    if(realPath == null || realPath.length() ==0)
                    {
                        realPath = baseObject.getPath()+"."+path.substring(lastIndex+1);
                    }
                }
            }else{
                realPath = path;
                eqMap.put(path,realPath);
                return realPath;
            }
        }
        if(realPath == null)
            return path;
        realPath = realPath.toLowerCase().replaceAll("\\\\",".");
        if(realPath.charAt(0) == '.')
            realPath = realPath.substring(1);
        String parentPath = getContext().getPath();
        realPath = realPath.substring(parentPath.length());
        eqMap.put(path,realPath);
        return realPath;
    }
    public String getModuleListFieldAbsolutePath(IModule itemModule, IBaseObject baseObject){
        IModule parent = baseObject.getParent();
        StringBuilder sb = new StringBuilder(baseObject.getName());
        while(parent != itemModule){
            sb.insert(0,parent.getName()+"\\");
            parent = parent.getParent();
        }

        if(parent != null)
            sb.insert(0,itemModule.getPath()+"\\");
        return sb.toString();
    }
}
