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

package com.hypixel.hytale.server.core.modules.projectile.system;

import java.util.Iterator;
import com.hypixel.hytale.protocol.ComponentUpdateType;
import com.hypixel.hytale.protocol.ComponentUpdate;
import java.util.Map;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.projectile.component.PredictedProjectile;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;

public class PredictedProjectileSystems
{
    public static class EntityTrackerUpdate extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final ComponentType<EntityStore, PredictedProjectile> predictedProjectileComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityTrackerUpdate() {
            this.visibleComponentType = EntityTrackerSystems.Visible.getComponentType();
            this.predictedProjectileComponentType = PredictedProjectile.getComponentType();
            this.query = (Query<EntityStore>)Query.and(this.visibleComponentType, this.predictedProjectileComponentType);
        }
        
        @Nullable
        @Override
        public SystemGroup<EntityStore> getGroup() {
            return EntityTrackerSystems.QUEUE_UPDATE_GROUP;
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Override
        public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
            return EntityTickingSystem.maybeUseParallel(archetypeChunkSize, taskCount);
        }
        
        @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 EntityTrackerSystems.Visible visibleComponent = archetypeChunk.getComponent(index, this.visibleComponentType);
            assert visibleComponent != null;
            final PredictedProjectile predictedProjectileComponent = archetypeChunk.getComponent(index, this.predictedProjectileComponentType);
            assert predictedProjectileComponent != null;
            if (!visibleComponent.newlyVisibleTo.isEmpty()) {
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), predictedProjectileComponent, visibleComponent.newlyVisibleTo);
            }
        }
        
        private static void queueUpdatesFor(@Nonnull final Ref<EntityStore> ref, @Nonnull final PredictedProjectile predictedProjectile, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo) {
            final ComponentUpdate update = new ComponentUpdate();
            update.type = ComponentUpdateType.Prediction;
            update.predictionId = predictedProjectile.getUuid();
            for (final Map.Entry<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> entry : visibleTo.entrySet()) {
                entry.getValue().queueUpdate(ref, update);
            }
        }
    }
}
