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

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

import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.npc.movement.controllers.MotionController;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import java.util.concurrent.ThreadLocalRandom;
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 com.hypixel.hytale.server.npc.corecomponents.utility.builders.BuilderActionRandom;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.WeightedAction;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionRandom extends ActionBase
{
    @Nonnull
    protected final WeightedAction[] actions;
    @Nonnull
    protected final WeightedAction[] availableActions;
    protected int availableActionsCount;
    protected double totalWeight;
    @Nullable
    protected WeightedAction current;
    
    public ActionRandom(@Nonnull final BuilderActionRandom builder, @Nonnull final BuilderSupport support) {
        super(builder);
        this.actions = builder.getActions(support).toArray(WeightedAction[]::new);
        for (final WeightedAction action : this.actions) {
            if (action == null) {
                throw new IllegalArgumentException("WeightedAction in Random actions list can't be null");
            }
        }
        this.availableActions = new WeightedAction[this.actions.length];
        this.availableActionsCount = 0;
    }
    
    @Override
    public boolean canExecute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        final int length = this.actions.length;
        if (!super.canExecute(ref, role, sensorInfo, dt, store) || length == 0) {
            return false;
        }
        if (this.current != null) {
            return this.current.canExecute(ref, role, sensorInfo, dt, store);
        }
        this.availableActionsCount = 0;
        this.totalWeight = 0.0;
        for (final WeightedAction action : this.actions) {
            if (action.canExecute(ref, role, sensorInfo, dt, store)) {
                this.availableActions[this.availableActionsCount++] = action;
                this.totalWeight += action.getWeight();
            }
        }
        return this.availableActionsCount > 0;
    }
    
    @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.availableActionsCount == 0) {
            return true;
        }
        if (this.current == null) {
            this.current = this.availableActions[0];
            this.totalWeight *= ThreadLocalRandom.current().nextDouble();
            this.totalWeight -= this.current.getWeight();
            for (int i = 1; i < this.availableActionsCount && this.totalWeight >= 0.0; ++i) {
                this.current = this.availableActions[i];
                this.totalWeight -= this.current.getWeight();
            }
            this.current.activate(role, sensorInfo);
        }
        final boolean finished = this.current.execute(ref, role, sensorInfo, dt, store);
        if (finished) {
            this.current.clearOnce();
            this.current.deactivate(role, sensorInfo);
            this.current = null;
        }
        return finished;
    }
    
    @Override
    public void registerWithSupport(final Role role) {
        for (final WeightedAction action : this.actions) {
            action.registerWithSupport(role);
        }
    }
    
    @Override
    public void motionControllerChanged(@Nullable final Ref<EntityStore> ref, @Nonnull final NPCEntity npcComponent, final MotionController motionController, @Nullable final ComponentAccessor<EntityStore> componentAccessor) {
        for (final WeightedAction action : this.actions) {
            action.motionControllerChanged(ref, npcComponent, motionController, componentAccessor);
        }
    }
    
    @Override
    public void loaded(final Role role) {
        for (final WeightedAction action : this.actions) {
            action.loaded(role);
        }
    }
    
    @Override
    public void spawned(final Role role) {
        for (final WeightedAction action : this.actions) {
            action.spawned(role);
        }
    }
    
    @Override
    public void unloaded(final Role role) {
        for (final WeightedAction action : this.actions) {
            action.unloaded(role);
        }
    }
    
    @Override
    public void removed(final Role role) {
        for (final WeightedAction action : this.actions) {
            action.removed(role);
        }
    }
    
    @Override
    public void teleported(final Role role, final World from, final World to) {
        for (final WeightedAction action : this.actions) {
            action.teleported(role, from, to);
        }
    }
}
