// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.npc.asset.builder;

import java.util.Iterator;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import java.util.EnumSet;
import java.util.function.BiConsumer;
import java.util.List;

public class InstructionContextHelper
{
    private final InstructionType context;
    private ComponentContext componentContext;
    private List<BiConsumer<InstructionType, ComponentContext>> componentContextEvaluators;
    
    public InstructionContextHelper(final InstructionType context) {
        this.context = context;
    }
    
    public boolean isComponent() {
        return this.context == InstructionType.Component;
    }
    
    public void setComponentContext(final ComponentContext context) {
        this.componentContext = context;
    }
    
    public boolean isInCorrectInstruction(@Nonnull final EnumSet<InstructionType> validTypes) {
        return validTypes.contains(this.context);
    }
    
    public static boolean isInCorrectInstruction(@Nonnull final EnumSet<InstructionType> validTypes, final InstructionType instructionContext) {
        return validTypes.contains(instructionContext);
    }
    
    public boolean extraContextMatches(@Nullable final EnumSet<ComponentContext> contexts) {
        return contexts == null || contexts.contains(this.componentContext);
    }
    
    public static boolean extraContextMatches(@Nullable final EnumSet<ComponentContext> validContexts, final ComponentContext context) {
        return validContexts == null || validContexts.contains(context);
    }
    
    public void addComponentContextEvaluator(final BiConsumer<InstructionType, ComponentContext> evaluator) {
        if (this.componentContextEvaluators == null) {
            this.componentContextEvaluators = new ObjectArrayList<BiConsumer<InstructionType, ComponentContext>>();
        }
        this.componentContextEvaluators.add(evaluator);
    }
    
    public void validateComponentContext(final InstructionType instructionContext, final ComponentContext componentContext) {
        if (!this.isComponent()) {
            throw new IllegalStateException("Calling validateComponentContext on a InstructionContextHelper that is not part of a component!");
        }
        if (this.componentContextEvaluators == null) {
            return;
        }
        for (final BiConsumer<InstructionType, ComponentContext> evaluator : this.componentContextEvaluators) {
            evaluator.accept(instructionContext, componentContext);
        }
    }
    
    public InstructionType getInstructionContext() {
        return this.context;
    }
    
    public ComponentContext getComponentContext() {
        return this.componentContext;
    }
}
