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

package com.hypixel.hytale.server.npc.systems;

import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.modules.splitvelocity.VelocityConfig;
import com.hypixel.hytale.math.vector.Vector3d;
import java.util.Iterator;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.debug.DebugUtils;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.modules.physics.component.Velocity;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.component.SystemType;
import com.hypixel.hytale.component.dependency.SystemTypeDependency;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import com.hypixel.hytale.component.system.ISystem;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.server.core.modules.physics.systems.GenericVelocityInstructionSystem;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.query.Query;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class NPCVelocityInstructionSystem extends EntityTickingSystem<EntityStore>
{
    @Nonnull
    private final Set<Dependency<EntityStore>> dependencies;
    @Nonnull
    private final Query<EntityStore> query;
    
    public NPCVelocityInstructionSystem() {
        this.dependencies = Set.of((Dependency<EntityStore>)new SystemDependency(Order.BEFORE, (Class<ISystem>)GenericVelocityInstructionSystem.class), (Dependency<EntityStore>)new SystemTypeDependency(Order.AFTER, (SystemType<Object, ISystem>)EntityModule.get().getVelocityModifyingSystemType()));
        this.query = (Query<EntityStore>)Query.and(NPCEntity.getComponentType(), Velocity.getComponentType());
    }
    
    @Override
    public void tick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final NPCEntity npcComponent = archetypeChunk.getComponent(index, NPCEntity.getComponentType());
        assert npcComponent != null;
        final Role role = npcComponent.getRole();
        if (role == null) {
            return;
        }
        final Velocity velocityComponent = archetypeChunk.getComponent(index, Velocity.getComponentType());
        assert velocityComponent != null;
        for (final Velocity.Instruction instruction : velocityComponent.getInstructions()) {
            switch (instruction.getType()) {
                case Set: {
                    final Vector3d velocity = instruction.getVelocity();
                    final VelocityConfig velocityConfig = instruction.getConfig();
                    role.processSetVelocityInstruction(velocity, velocityConfig);
                    if (!DebugUtils.DISPLAY_FORCES) {
                        continue;
                    }
                    final TransformComponent transformComponent = archetypeChunk.getComponent(index, TransformComponent.getComponentType());
                    if (transformComponent == null) {
                        continue;
                    }
                    final World world = commandBuffer.getExternalData().getWorld();
                    DebugUtils.addForce(world, transformComponent.getPosition(), velocity, velocityConfig);
                    continue;
                }
                case Add: {
                    final Vector3d velocity = instruction.getVelocity();
                    final VelocityConfig velocityConfig = instruction.getConfig();
                    role.processAddVelocityInstruction(velocity, velocityConfig);
                    if (!DebugUtils.DISPLAY_FORCES) {
                        continue;
                    }
                    final TransformComponent transformComponent = archetypeChunk.getComponent(index, TransformComponent.getComponentType());
                    if (transformComponent != null) {
                        final World world = commandBuffer.getExternalData().getWorld();
                        DebugUtils.addForce(world, transformComponent.getPosition(), velocity, velocityConfig);
                        continue;
                    }
                    continue;
                }
            }
        }
        velocityComponent.getInstructions().clear();
    }
    
    @Nonnull
    @Override
    public Set<Dependency<EntityStore>> getDependencies() {
        return this.dependencies;
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
}
