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

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

import java.util.List;
import com.hypixel.hytale.server.npc.util.expression.Scope;
import com.hypixel.hytale.server.npc.util.expression.ExecutionContext;
import com.hypixel.hytale.server.npc.validators.NPCLoadTimeValidationHelper;
import java.util.Iterator;
import com.google.gson.JsonArray;
import javax.annotation.Nonnull;
import com.google.gson.JsonElement;
import javax.annotation.Nullable;

public abstract class BuilderObjectArrayHelper<T, U> extends BuilderObjectHelper<T>
{
    @Nullable
    protected BuilderObjectReferenceHelper[] builders;
    protected String label;
    
    public BuilderObjectArrayHelper(final Class<?> classType, final BuilderContext owner) {
        super(classType, owner);
    }
    
    @Override
    public void readConfig(@Nonnull final JsonElement data, @Nonnull final BuilderManager builderManager, @Nonnull final BuilderParameters builderParameters, @Nonnull final BuilderValidationHelper builderValidationHelper) {
        super.readConfig(data, builderManager, builderParameters, builderValidationHelper);
        if (data.isJsonNull()) {
            this.builders = null;
        }
        else {
            if (!data.isJsonArray()) {
                final String string = data.toString();
                throw new IllegalArgumentException(String.format("Expected a JSON array of '%s' at %s (JSON: %s)", this.classType.getSimpleName(), this.getBreadCrumbs(), (string.length() > 60) ? (string.substring(60) + "...") : string));
            }
            final JsonArray array = data.getAsJsonArray();
            final BuilderFactory<U> factory = builderManager.getFactory(this.classType);
            this.builders = new BuilderObjectReferenceHelper[array.size()];
            int index = 0;
            for (final JsonElement element : array) {
                final BuilderObjectReferenceHelper<U> builderObjectReferenceHelper = this.createReferenceHelper();
                builderObjectReferenceHelper.readConfig(element, factory, builderManager, builderParameters, builderValidationHelper);
                if (!builderObjectReferenceHelper.isPresent()) {
                    throw new IllegalStateException("Missing builder reference at " + this.getBreadCrumbs() + ": " + builderParameters.getFileName());
                }
                this.builders[index++] = builderObjectReferenceHelper;
            }
        }
    }
    
    @Override
    public boolean validate(final String configName, final NPCLoadTimeValidationHelper loadTimeValidationHelper, @Nonnull final BuilderManager manager, @Nonnull final ExecutionContext context, final Scope globalScope, @Nonnull final List<String> errors) {
        if (this.hasNoElements()) {
            return true;
        }
        boolean result = true;
        for (final BuilderObjectReferenceHelper builder : this.builders) {
            if (!builder.excludeFromRegularBuild()) {
                result &= builder.validate(configName, loadTimeValidationHelper, manager, context, globalScope, errors);
            }
        }
        return result;
    }
    
    @Override
    public boolean isPresent() {
        return this.builders != null;
    }
    
    public boolean isEmpty() {
        return this.isPresent() && this.builders.length == 0;
    }
    
    public boolean hasNoElements() {
        return this.builders == null || this.builders.length == 0;
    }
    
    @Override
    public String getLabel() {
        return this.label;
    }
    
    public void setLabel(final String label) {
        this.label = label;
    }
    
    @Nonnull
    protected BuilderObjectReferenceHelper<U> createReferenceHelper() {
        return new BuilderObjectReferenceHelper<U>(this.classType, this);
    }
}
