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

package com.hypixel.hytale.server.npc.decisionmaker.core.conditions.base;

import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.codec.validation.Validators;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import javax.annotation.Nonnull;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.npc.decisionmaker.core.EvaluationContext;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.asset.type.responsecurve.config.ResponseCurve;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public abstract class CurveCondition extends Condition
{
    public static final BuilderCodec<CurveCondition> ABSTRACT_CODEC;
    protected String responseCurve;
    protected ResponseCurve.Reference responseCurveReference;
    
    protected CurveCondition() {
    }
    
    public String getResponseCurve() {
        return this.responseCurve;
    }
    
    @Override
    public double calculateUtility(final int selfIndex, final ArchetypeChunk<EntityStore> archetypeChunk, final Ref<EntityStore> target, final CommandBuffer<EntityStore> commandBuffer, final EvaluationContext context) {
        final ResponseCurve curve = this.responseCurveReference.get();
        if (curve == null) {
            throw new IllegalStateException("No such response curve asset: " + this.responseCurve);
        }
        final double input = this.getNormalisedInput(selfIndex, archetypeChunk, target, commandBuffer, context);
        if (input == Double.MAX_VALUE) {
            return 0.0;
        }
        return MathUtil.clamp(curve.computeY(input), 0.0, 1.0);
    }
    
    @Override
    public int getSimplicity() {
        return 20;
    }
    
    protected abstract double getNormalisedInput(final int p0, final ArchetypeChunk<EntityStore> p1, final Ref<EntityStore> p2, final CommandBuffer<EntityStore> p3, final EvaluationContext p4);
    
    @Nonnull
    @Override
    public String toString() {
        return "CurveCondition{responseCurve=" + this.responseCurve + "} " + super.toString();
    }
    
    static {
        ABSTRACT_CODEC = BuilderCodec.abstractBuilder(CurveCondition.class, CurveCondition.BASE_CODEC).appendInherited(new KeyedCodec<String>("Curve", Codec.STRING), (condition, s) -> condition.responseCurve = s, condition -> condition.responseCurve, (condition, parent) -> condition.responseCurve = parent.responseCurve).documentation("The response curve used to evaluate the condition.").addValidator(Validators.nonNull()).addValidator(ResponseCurve.VALIDATOR_CACHE.getValidator()).add().afterDecode(condition -> {
            if (condition.responseCurve != null) {
                final int index = ResponseCurve.getAssetMap().getIndex(condition.responseCurve);
                condition.responseCurveReference = new ResponseCurve.Reference(index, ResponseCurve.getAssetMap().getAsset(index));
            }
        }).build();
    }
}
