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

package com.hypixel.hytale.server.spawning.corecomponents;

import com.hypixel.hytale.server.spawning.wrappers.BeaconSpawnWrapper;
import java.util.Iterator;
import java.util.List;
import com.hypixel.hytale.server.spawning.util.FloodFillPositionSelector;
import com.hypixel.hytale.server.spawning.beacons.SpawnBeacon;
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.spawning.corecomponents.builders.BuilderActionTriggerSpawnBeacon;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionTriggerSpawnBeacon extends ActionBase
{
    protected final int beaconId;
    protected final int range;
    protected final int targetSlot;
    
    public ActionTriggerSpawnBeacon(@Nonnull final BuilderActionTriggerSpawnBeacon builder, @Nonnull final BuilderSupport support) {
        super(builder);
        this.beaconId = builder.getBeaconId(support);
        this.range = builder.getRange(support);
        this.targetSlot = builder.getTargetSlot(support);
    }
    
    @Override
    public boolean canExecute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        return super.canExecute(ref, role, sensorInfo, dt, store) && (this.targetSlot == Integer.MIN_VALUE || role.getMarkedEntitySupport().hasMarkedEntityInSlot(this.targetSlot));
    }
    
    @Override
    public void registerWithSupport(@Nonnull final Role role) {
        role.getPositionCache().requireSpawnBeaconDistance(this.range);
    }
    
    @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);
        final List<Ref<EntityStore>> spawnBeacons = role.getPositionCache().getSpawnBeaconList();
        for (final Ref<EntityStore> spawnBeaconRef : spawnBeacons) {
            final SpawnBeacon spawnBeaconComponent = store.getComponent(spawnBeaconRef, SpawnBeacon.getComponentType());
            assert spawnBeaconComponent != null;
            final BeaconSpawnWrapper spawnWrapper = spawnBeaconComponent.getSpawnWrapper();
            if (spawnWrapper.getSpawnIndex() != this.beaconId) {
                continue;
            }
            Ref<EntityStore> targetRef;
            if (this.targetSlot != Integer.MIN_VALUE) {
                targetRef = role.getMarkedEntitySupport().getMarkedEntityRef(this.targetSlot);
            }
            else {
                targetRef = ref;
            }
            final FloodFillPositionSelector floodFillPositionSelectorComponent = store.getComponent(spawnBeaconRef, FloodFillPositionSelector.getComponentType());
            assert floodFillPositionSelectorComponent != null;
            spawnBeaconComponent.manualTrigger(spawnBeaconRef, floodFillPositionSelectorComponent, targetRef, store);
            return true;
        }
        return true;
    }
}
