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

package com.hypixel.hytale.server.npc.corecomponents.statemachine;

import com.hypixel.hytale.server.npc.util.ComponentInfo;
import com.hypixel.hytale.server.npc.decisionmaker.stateevaluator.StateEvaluator;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderActionBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.statemachine.builders.BuilderActionState;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionState extends ActionBase
{
    protected final int state;
    protected final int subState;
    protected final boolean clearOnce;
    protected final boolean componentLocal;
    protected final int componentIndex;
    
    public ActionState(@Nonnull final BuilderActionState builderActionState, @Nonnull final BuilderSupport support) {
        super(builderActionState);
        this.state = builderActionState.getStateIndex();
        this.subState = builderActionState.getSubStateIndex();
        this.clearOnce = builderActionState.isClearState();
        this.componentLocal = builderActionState.isComponentLocal();
        this.componentIndex = support.getComponentIndex();
    }
    
    @Override
    public boolean execute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        super.execute(ref, role, sensorInfo, dt, store);
        if (this.componentLocal) {
            role.getStateSupport().setComponentState(this.componentIndex, this.state);
            return true;
        }
        final StateEvaluator stateEvaluatorComponent = store.getComponent(ref, StateEvaluator.getComponentType());
        if (stateEvaluatorComponent == null || !stateEvaluatorComponent.isActive()) {
            role.getStateSupport().setState(this.state, this.subState, this.clearOnce, false);
        }
        return true;
    }
    
    @Override
    public void getInfo(@Nonnull final Role role, @Nonnull final ComponentInfo holder) {
        if (this.componentLocal) {
            holder.addField("Component local state: " + this.state);
        }
        else {
            holder.addField("State: " + role.getStateSupport().getStateName(this.state, this.subState));
        }
    }
}
