package com.android.commands.monkey.ape.jev;

import com.android.commands.monkey.ape.model.ModelAction;
import com.android.commands.monkey.ape.naming.NameManager;
import com.android.commands.monkey.ape.tree.GUITree;
import com.android.commands.monkey.ape.tree.GUITreeAction;
import com.android.commands.monkey.ape.tree.GUITreeNode;

/** Read-only classification shared by the production gate, KEEP cache and shadow. */
final class JevRegionMatcher {
    static final class Match {
        final boolean source,action;
        final GUITreeNode concrete;
        final String reason;
        Match(boolean source,boolean action,GUITreeNode concrete,String reason) {
            this.source=source;this.action=action;this.concrete=concrete;this.reason=reason;
        }
        boolean exact(GUITreeNode node){return concrete!=null && concrete==node;}
    }
    static Match match(GUITreeAction anchor,GUITree tree,ModelAction action) {
        if(anchor==null || tree==null || anchor.getGUITree().getCurrentState()!=tree.getCurrentState())
            return new Match(false,false,null,"source_state_mismatch");
        if(action==null || anchor.getModelAction()!=action)
            return new Match(true,false,null,"model_action_mismatch");
        GUITreeNode node=NameManager.withoutInterning(new java.util.function.Supplier<GUITreeNode>() {
            public GUITreeNode get(){return JevRegistry.correspondingNode(anchor,tree,action);}
        });
        return new Match(true,true,node,node==null?"concrete_absent_or_ambiguous":"exact");
    }
    private JevRegionMatcher() { }
}
