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

package com.hypixel.hytale.server.npc.corecomponents.world.builders;

import com.hypixel.hytale.server.core.asset.type.blockset.config.BlockSet;
import com.hypixel.hytale.server.npc.asset.builder.Feature;
import com.hypixel.hytale.server.npc.asset.builder.validators.AssetValidator;
import com.hypixel.hytale.server.npc.asset.builder.validators.asset.BlockSetExistsValidator;
import com.hypixel.hytale.server.npc.asset.builder.validators.DoubleValidator;
import com.hypixel.hytale.server.npc.asset.builder.validators.DoubleRangeValidator;
import com.hypixel.hytale.server.npc.asset.builder.Builder;
import com.google.gson.JsonElement;
import com.hypixel.hytale.server.npc.asset.builder.BuilderDescriptorState;
import com.hypixel.hytale.server.npc.corecomponents.world.SensorBlock;
import com.hypixel.hytale.server.npc.instructions.Sensor;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.asset.builder.holder.BooleanHolder;
import com.hypixel.hytale.server.npc.asset.builder.holder.AssetHolder;
import com.hypixel.hytale.server.npc.asset.builder.holder.DoubleHolder;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderSensorBase;

public class BuilderSensorBlock extends BuilderSensorBase
{
    protected final DoubleHolder range;
    protected final DoubleHolder yRange;
    protected final AssetHolder blockSet;
    protected final BooleanHolder pickRandom;
    protected final BooleanHolder reserveBlock;
    
    public BuilderSensorBlock() {
        this.range = new DoubleHolder();
        this.yRange = new DoubleHolder();
        this.blockSet = new AssetHolder();
        this.pickRandom = new BooleanHolder();
        this.reserveBlock = new BooleanHolder();
    }
    
    @Nonnull
    @Override
    public String getShortDescription() {
        return "Checks for one of a set of blocks in the nearby area";
    }
    
    @Nonnull
    @Override
    public String getLongDescription() {
        return "Checks for one of a set of blocks in the nearby area and caches the result until explicitly reset or the targeted block changes/is removed. All block sensors with the same sought blockset share the same targeted block once found";
    }
    
    @Nonnull
    @Override
    public Sensor build(@Nonnull final BuilderSupport builderSupport) {
        return new SensorBlock(this, builderSupport);
    }
    
    @Nonnull
    @Override
    public BuilderDescriptorState getBuilderDescriptorState() {
        return BuilderDescriptorState.Experimental;
    }
    
    @Nonnull
    @Override
    public Builder<Sensor> readConfig(@Nonnull final JsonElement data) {
        this.requireDouble(data, "Range", this.range, DoubleRangeValidator.fromExclToIncl(0.0, Double.MAX_VALUE), BuilderDescriptorState.Stable, "The range to search for the blocks in", null);
        this.getDouble(data, "MaxHeight", this.yRange, 4.0, DoubleRangeValidator.fromExclToIncl(0.0, Double.MAX_VALUE), BuilderDescriptorState.Stable, "The vertical range to search for the blocks in", null);
        this.requireAsset(data, "Blocks", this.blockSet, BlockSetExistsValidator.required(), BuilderDescriptorState.Stable, "The set of blocks to search for", null);
        this.getBoolean(data, "Random", this.pickRandom, false, BuilderDescriptorState.Stable, "Whether to pick at random from within the matched blocks or pick the closest", null);
        this.getBoolean(data, "Reserve", this.reserveBlock, false, BuilderDescriptorState.Stable, "Whether to reserve the found block to prevent other NPCs selecting it", null);
        this.provideFeature(Feature.Position);
        return this;
    }
    
    public double getRange(@Nonnull final BuilderSupport support) {
        return this.range.get(support.getExecutionContext());
    }
    
    public double getYRange(@Nonnull final BuilderSupport support) {
        return this.yRange.get(support.getExecutionContext());
    }
    
    public int getBlockSet(@Nonnull final BuilderSupport support) {
        final String key = this.blockSet.get(support.getExecutionContext());
        final int index = BlockSet.getAssetMap().getIndex(key);
        if (index == Integer.MIN_VALUE) {
            throw new IllegalArgumentException("Unknown key! " + key);
        }
        return index;
    }
    
    public boolean isPickRandom(@Nonnull final BuilderSupport support) {
        return this.pickRandom.get(support.getExecutionContext());
    }
    
    public boolean isReserveBlock(@Nonnull final BuilderSupport support) {
        return this.reserveBlock.get(support.getExecutionContext());
    }
}
