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

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

import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.NPCPlugin;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.component.ComponentAccessor;
import java.util.Objects;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.server.core.entity.movement.MovementStatesComponent;
import com.hypixel.hytale.server.core.modules.physics.component.Velocity;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;

public class MovementStatesSystem extends SteppableTickingSystem
{
    @Nonnull
    private final ComponentType<EntityStore, NPCEntity> npcComponentType;
    @Nonnull
    private final ComponentType<EntityStore, Velocity> velocityComponentType;
    @Nonnull
    private final ComponentType<EntityStore, MovementStatesComponent> movementStatesComponentType;
    @Nonnull
    private final Set<Dependency<EntityStore>> dependencies;
    @Nonnull
    private final Query<EntityStore> query;
    
    public MovementStatesSystem(@Nonnull final ComponentType<EntityStore, NPCEntity> npcComponentType, @Nonnull final ComponentType<EntityStore, Velocity> velocityComponentType, @Nonnull final ComponentType<EntityStore, MovementStatesComponent> movementStatesComponentType) {
        this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.AFTER, ComputeVelocitySystem.class));
        this.npcComponentType = npcComponentType;
        this.velocityComponentType = velocityComponentType;
        this.movementStatesComponentType = movementStatesComponentType;
        this.query = (Query<EntityStore>)Query.and(npcComponentType, velocityComponentType, movementStatesComponentType);
    }
    
    @Nonnull
    @Override
    public Set<Dependency<EntityStore>> getDependencies() {
        return this.dependencies;
    }
    
    @Override
    public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
        return false;
    }
    
    @Override
    public void steppedTick(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, this.npcComponentType);
        assert npcComponent != null;
        final Velocity velocityComponent = archetypeChunk.getComponent(index, this.velocityComponentType);
        assert velocityComponent != null;
        final MovementStatesComponent movementStatesComponent = archetypeChunk.getComponent(index, this.movementStatesComponentType);
        assert movementStatesComponent != null;
        try {
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            if (Objects.equals(npcComponent.getRoleName(), "Empty_Role")) {
                return;
            }
            npcComponent.getRole().updateMovementState(ref, movementStatesComponent.getMovementStates(), velocityComponent.getVelocity(), commandBuffer);
        }
        catch (final Exception e) {
            NPCPlugin.get().getLogger().atSevere().withCause(e).log("Failed to update movement states for " + npcComponent.getRoleName() + ", Archetype: " + String.valueOf(archetypeChunk.getArchetype()) + ", Spawn config index: " + npcComponent.getSpawnConfiguration() + ": ");
        }
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
}
