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

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

import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import com.hypixel.hytale.server.core.inventory.Inventory;
import com.hypixel.hytale.protocol.InteractionType;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.server.core.modules.interaction.IInteractionSimulationHandler;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.entity.LivingEntity;
import com.hypixel.hytale.server.core.entity.InteractionManager;
import com.hypixel.hytale.server.npc.interactions.NPCInteractionSimulationHandler;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.server.core.modules.interaction.InteractionModule;
import com.hypixel.hytale.component.query.Query;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.HolderSystem;

public class NPCInteractionSystems
{
    public static class AddSimulationManagerSystem extends HolderSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, NPCEntity> npcEntityComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public AddSimulationManagerSystem(@Nonnull final ComponentType<EntityStore, NPCEntity> npcEntityComponentType) {
            this.npcEntityComponentType = npcEntityComponentType;
            this.query = (Query<EntityStore>)Query.and(npcEntityComponentType, Query.not((Query<Object>)InteractionModule.get().getInteractionManagerComponent()));
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            final NPCEntity npcComponent = holder.getComponent(this.npcEntityComponentType);
            assert npcComponent != null;
            holder.addComponent(InteractionModule.get().getInteractionManagerComponent(), new InteractionManager(npcComponent, null, new NPCInteractionSimulationHandler()));
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
    }
    
    public static class TickHeldInteractionsSystem extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, NPCEntity> npcComponentType;
        @Nonnull
        private final ComponentType<EntityStore, InteractionManager> interactionManagerComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public TickHeldInteractionsSystem(@Nonnull final ComponentType<EntityStore, NPCEntity> npcEntityComponentType) {
            this.npcComponentType = npcEntityComponentType;
            this.interactionManagerComponentType = InteractionModule.get().getInteractionManagerComponent();
            this.query = (Query<EntityStore>)Query.and(npcEntityComponentType, this.interactionManagerComponentType);
        }
        
        @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, this.npcComponentType);
            assert npcComponent != null;
            final InteractionManager interactionManager = archetypeChunk.getComponent(index, this.interactionManagerComponentType);
            assert interactionManager != null;
            final Inventory inventory = npcComponent.getInventory();
            final ItemContainer armorInventory = inventory.getArmor();
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            interactionManager.tryRunHeldInteraction(ref, commandBuffer, InteractionType.Held);
            interactionManager.tryRunHeldInteraction(ref, commandBuffer, InteractionType.HeldOffhand);
            for (short i = 0; i < armorInventory.getCapacity(); ++i) {
                interactionManager.tryRunHeldInteraction(ref, commandBuffer, InteractionType.Equipped, i);
            }
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
    }
}
