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

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

import com.hypixel.hytale.component.Component;
import com.hypixel.hytale.component.system.RefChangeSystem;
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.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.server.core.modules.entity.component.Invulnerable;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import com.hypixel.hytale.component.ResourceType;
import java.util.concurrent.ConcurrentHashMap;
import com.hypixel.hytale.component.Ref;
import java.util.Set;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Resource;

public class InvulnerableSystems
{
    public static class QueueResource implements Resource<EntityStore>
    {
        private final Set<Ref<EntityStore>> queue;
        
        public QueueResource() {
            this.queue = (Set<Ref<EntityStore>>)ConcurrentHashMap.newKeySet();
        }
        
        public static ResourceType<EntityStore, QueueResource> getResourceType() {
            return EntityModule.get().getInvulnerableQueueResourceType();
        }
        
        @Nonnull
        @Override
        public Resource<EntityStore> clone() {
            return new QueueResource();
        }
    }
    
    public static class EntityTrackerUpdate extends EntityTickingSystem<EntityStore>
    {
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> componentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityTrackerUpdate(final ComponentType<EntityStore, EntityTrackerSystems.Visible> componentType) {
            this.componentType = componentType;
            this.query = (Query<EntityStore>)Query.and(componentType, Invulnerable.getComponentType());
        }
        
        @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 systemIndex, @Nonnull final Store<EntityStore> store) {
            super.tick(dt, systemIndex, store);
            store.getResource(QueueResource.getResourceType()).queue.clear();
        }
        
        @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 visible = archetypeChunk.getComponent(index, this.componentType);
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            if (commandBuffer.getResource(QueueResource.getResourceType()).queue.remove(ref)) {
                queueUpdatesFor(ref, visible.visibleTo);
            }
            else if (!visible.newlyVisibleTo.isEmpty()) {
                queueUpdatesFor(ref, visible.newlyVisibleTo);
            }
        }
        
        private static void queueUpdatesFor(final Ref<EntityStore> ref, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo) {
            final ComponentUpdate update = new ComponentUpdate();
            update.type = ComponentUpdateType.Invulnerable;
            for (final EntityTrackerSystems.EntityViewer viewer : visibleTo.values()) {
                viewer.queueUpdate(ref, update);
            }
        }
    }
    
    public static class EntityTrackerAddAndRemove extends RefChangeSystem<EntityStore, Invulnerable>
    {
        private final ComponentType<EntityStore, Invulnerable> invulnerableComponentType;
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityTrackerAddAndRemove(final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType) {
            this.invulnerableComponentType = Invulnerable.getComponentType();
            this.visibleComponentType = visibleComponentType;
            this.query = (Query<EntityStore>)Query.and(visibleComponentType, this.invulnerableComponentType);
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public ComponentType<EntityStore, Invulnerable> componentType() {
            return this.invulnerableComponentType;
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final Invulnerable component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            commandBuffer.getResource(QueueResource.getResourceType()).queue.add(ref);
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<EntityStore> ref, final Invulnerable oldComponent, @Nonnull final Invulnerable newComponent, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final Invulnerable component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            for (final EntityTrackerSystems.EntityViewer viewer : store.getComponent(ref, this.visibleComponentType).visibleTo.values()) {
                viewer.queueRemove(ref, ComponentUpdateType.Invulnerable);
            }
        }
    }
}
