package jadx.core.dex.visitors.finaly.traverser.state;

import java.util.List;

import org.jetbrains.annotations.Nullable;

import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.visitors.finaly.CentralityState;
import jadx.core.dex.visitors.finaly.traverser.factory.TraverserStateFactory;
import jadx.core.dex.visitors.finaly.traverser.handlers.AbstractBlockTraverserHandler;
import jadx.core.dex.visitors.finaly.traverser.handlers.MergePathActivePathTraverserHandler;

public final class IdentifiedScopeWithTerminatorTraverserState extends TraverserState {

	public static TraverserStateFactory<IdentifiedScopeWithTerminatorTraverserState> getFactory(CentralityState centralityState,
			List<BlockNode> roots, BlockNode scopeTerminator) {
		return new IdentifiedScopeWithTerminatorStateFactory(centralityState, roots, scopeTerminator);
	}

	private static final class IdentifiedScopeWithTerminatorStateFactory
			extends TraverserStateFactory<IdentifiedScopeWithTerminatorTraverserState> {

		private final CentralityState centralityState;
		private final List<BlockNode> roots;
		private final BlockNode scopeTerminator;

		public IdentifiedScopeWithTerminatorStateFactory(CentralityState centralityState, List<BlockNode> roots,
				BlockNode scopeTerminator) {
			this.centralityState = centralityState;
			this.roots = roots;
			this.scopeTerminator = scopeTerminator;
		}

		@Override
		public IdentifiedScopeWithTerminatorTraverserState generateInternalState(TraverserActivePathState state) {
			return new IdentifiedScopeWithTerminatorTraverserState(state, centralityState, roots, scopeTerminator);
		}
	}

	private final CentralityState centralityState;
	private final List<BlockNode> roots;
	private final BlockNode scopeTerminator;

	public IdentifiedScopeWithTerminatorTraverserState(TraverserActivePathState state, CentralityState centralityState,
			List<BlockNode> roots, BlockNode scopeTerminator) {
		super(state);
		this.roots = roots;
		this.scopeTerminator = scopeTerminator;
		this.centralityState = centralityState;
	}

	@Override
	public @Nullable AbstractBlockTraverserHandler getNextHandler() {
		return new MergePathActivePathTraverserHandler(getComparatorState());
	}

	@Override
	public ComparisonState getCompareState() {
		return ComparisonState.READY_TO_COMPARE;
	}

	@Override
	public boolean isTerminal() {
		return false;
	}

	@Override
	protected @Nullable CentralityState getUnderlyingCentralityState() {
		return centralityState;
	}

	@Override
	protected @Nullable TraverserBlockInfo getUnderlyingBlockInsnInfo() {
		return null;
	}

	@Override
	protected TraverserState duplicateInternalState(TraverserActivePathState comparatorState) {
		return new IdentifiedScopeWithTerminatorTraverserState(comparatorState, centralityState, roots, scopeTerminator);
	}

	public BlockNode getTerminus() {
		return scopeTerminator;
	}

	public List<BlockNode> getRoots() {
		return roots;
	}
}
