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

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

import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.npc.NPCPlugin;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.npc.role.RoleDebugFlags;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.npc.components.messaging.BeaconSupport;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderSensorBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.entity.builders.BuilderSensorBeacon;
import com.hypixel.hytale.server.npc.sensorinfo.EntityPositionProvider;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.npc.corecomponents.SensorBase;

public class SensorBeacon extends SensorBase
{
    protected static final ComponentType<EntityStore, TransformComponent> TRANSFORM_COMPONENT_TYPE;
    protected final int messageIndex;
    protected final double range;
    protected final int targetSlot;
    protected final boolean consume;
    private final EntityPositionProvider positionProvider;
    
    public SensorBeacon(@Nonnull final BuilderSensorBeacon builderSensorBeacon, @Nonnull final BuilderSupport builderSupport) {
        super(builderSensorBeacon);
        this.positionProvider = new EntityPositionProvider();
        this.messageIndex = builderSensorBeacon.getMessageSlot(builderSupport);
        this.range = builderSensorBeacon.getRange(builderSupport);
        this.targetSlot = builderSensorBeacon.getTargetSlot(builderSupport);
        this.consume = builderSensorBeacon.isConsume();
    }
    
    @Override
    public boolean matches(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final double dt, @Nonnull final Store<EntityStore> store) {
        if (!super.matches(ref, role, dt, store)) {
            return false;
        }
        final BeaconSupport beaconSupportComponent = store.getComponent(ref, BeaconSupport.getComponentType());
        if (beaconSupportComponent == null) {
            this.positionProvider.clear();
            return false;
        }
        if (!beaconSupportComponent.isMessageQueued(this.messageIndex)) {
            this.positionProvider.clear();
            return false;
        }
        final Ref<EntityStore> target = this.consume ? beaconSupportComponent.pollMessage(this.messageIndex) : beaconSupportComponent.peekMessage(this.messageIndex);
        if (target == null) {
            this.positionProvider.clear();
            return false;
        }
        final Ref<EntityStore> targetRef = this.positionProvider.setTarget(target, store);
        if (targetRef == null || !targetRef.isValid()) {
            this.positionProvider.clear();
            return false;
        }
        final TransformComponent targetTransformComponent = store.getComponent(targetRef, SensorBeacon.TRANSFORM_COMPONENT_TYPE);
        assert targetTransformComponent != null;
        final Vector3d targetPosition = targetTransformComponent.getPosition();
        final TransformComponent transformComponent = store.getComponent(ref, SensorBeacon.TRANSFORM_COMPONENT_TYPE);
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        if (targetPosition.distanceSquaredTo(position) > this.range * this.range) {
            this.positionProvider.clear();
            return false;
        }
        if (this.targetSlot >= 0) {
            role.getMarkedEntitySupport().setMarkedEntity(this.targetSlot, targetRef);
        }
        if (role.getDebugSupport().isDebugFlagSet(RoleDebugFlags.BeaconMessages)) {
            NPCPlugin.get().getLogger().atInfo().log("ID %d received message '%s' with target ID %d", ref.getIndex(), beaconSupportComponent.getMessageTextForIndex(this.messageIndex), target.getIndex());
        }
        return true;
    }
    
    @Override
    public InfoProvider getSensorInfo() {
        return this.positionProvider;
    }
    
    static {
        TRANSFORM_COMPONENT_TYPE = TransformComponent.getComponentType();
    }
}
