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

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

import com.hypixel.hytale.server.core.modules.entity.component.NewSpawnComponent;
import java.util.Iterator;
import com.hypixel.hytale.protocol.ComponentUpdateType;
import com.hypixel.hytale.protocol.ComponentUpdate;
import java.util.Map;
import com.hypixel.hytale.protocol.ColorLight;
import com.hypixel.hytale.component.ArchetypeChunk;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.modules.entity.component.DynamicLight;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.chunk.EntityChunk;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.server.core.modules.entity.EntityModule;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.component.system.HolderSystem;
import com.hypixel.hytale.server.core.modules.entity.component.FromWorldGen;
import com.hypixel.hytale.server.core.modules.entity.component.FromPrefab;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.dependency.SystemGroupDependency;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.component.system.RefSystem;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Component;

public class EntitySystems
{
    public abstract static class ClearMarker<T extends Component<EntityStore>> extends RefSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, T> componentType;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        
        public ClearMarker(@Nonnull final ComponentType<EntityStore, T> componentType, @Nonnull final SystemGroup<EntityStore> preGroup) {
            this.componentType = componentType;
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemGroupDependency(Order.AFTER, preGroup));
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            commandBuffer.removeComponent(ref, this.componentType);
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public Query<EntityStore> getQuery() {
            return this.componentType;
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
    }
    
    public static class ClearFromPrefabMarker extends ClearMarker<FromPrefab>
    {
        public ClearFromPrefabMarker(@Nonnull final ComponentType<EntityStore, FromPrefab> componentType, @Nonnull final SystemGroup<EntityStore> preGroup) {
            super(componentType, preGroup);
        }
    }
    
    public static class ClearFromWorldGenMarker extends ClearMarker<FromWorldGen>
    {
        public ClearFromWorldGenMarker(@Nonnull final ComponentType<EntityStore, FromWorldGen> componentType, @Nonnull final SystemGroup<EntityStore> preGroup) {
            super(componentType, preGroup);
        }
    }
    
    public static class OnLoadFromExternal extends HolderSystem<EntityStore>
    {
        @Nonnull
        private final Query<EntityStore> query;
        @Nonnull
        private final SystemGroup<EntityStore> group;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        
        public OnLoadFromExternal(@Nonnull final ComponentType<EntityStore, FromPrefab> fromPrefab, @Nonnull final ComponentType<EntityStore, FromWorldGen> fromWorldGen, @Nonnull final SystemGroup<EntityStore> group) {
            this.query = (Query<EntityStore>)Query.and(Query.or(fromPrefab, fromWorldGen), UUIDComponent.getComponentType());
            this.group = group;
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.BEFORE, EntityStore.UUIDSystem.class), new SystemDependency(Order.AFTER, EntityModule.LegacyUUIDSystem.class));
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            holder.putComponent(UUIDComponent.getComponentType(), UUIDComponent.generateVersion3UUID());
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public SystemGroup<EntityStore> getGroup() {
            return this.group;
        }
    }
    
    public static class UnloadEntityFromChunk extends RefSystem<EntityStore>
    {
        @Override
        public Query<EntityStore> getQuery() {
            return TransformComponent.getComponentType();
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
            assert transformComponent != null;
            final Ref<ChunkStore> chunkRef = transformComponent.getChunkRef();
            if (chunkRef == null || !chunkRef.isValid()) {
                return;
            }
            final World world = commandBuffer.getExternalData().getWorld();
            final ChunkStore chunkStore = world.getChunkStore();
            final Store<ChunkStore> chunkComponentStore = chunkStore.getStore();
            final EntityChunk entityChunkComponent = chunkComponentStore.getComponent(chunkRef, EntityChunk.getComponentType());
            assert entityChunkComponent != null;
            switch (reason) {
                case REMOVE: {
                    entityChunkComponent.removeEntityReference(ref);
                    break;
                }
                case UNLOAD: {
                    entityChunkComponent.unloadEntityReference(ref);
                    break;
                }
            }
        }
    }
    
    public static class DynamicLightTracker extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> componentType;
        @Nonnull
        private final ComponentType<EntityStore, DynamicLight> dynamicLightType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public DynamicLightTracker(@Nonnull final ComponentType<EntityStore, EntityTrackerSystems.Visible> componentType) {
            this.componentType = componentType;
            this.dynamicLightType = DynamicLight.getComponentType();
            this.query = (Query<EntityStore>)Query.and(componentType, this.dynamicLightType);
        }
        
        @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.componentType);
            assert visibleComponent != null;
            final DynamicLight dynamicLightComponent = archetypeChunk.getComponent(index, this.dynamicLightType);
            assert dynamicLightComponent != null;
            final ColorLight dynamicLight = dynamicLightComponent.getColorLight();
            if (dynamicLightComponent.consumeNetworkOutdated()) {
                if (dynamicLight != null) {
                    queueUpdatesFor(archetypeChunk.getReferenceTo(index), dynamicLight, visibleComponent.visibleTo);
                }
                else {
                    queueRemoveFor(archetypeChunk.getReferenceTo(index), visibleComponent.visibleTo);
                }
            }
            else if (!visibleComponent.newlyVisibleTo.isEmpty() && dynamicLight != null) {
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), dynamicLight, visibleComponent.newlyVisibleTo);
            }
        }
        
        private static void queueUpdatesFor(@Nonnull final Ref<EntityStore> ref, @Nonnull final ColorLight dynamicLight, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo) {
            final ComponentUpdate update = new ComponentUpdate();
            update.type = ComponentUpdateType.DynamicLight;
            update.dynamicLight = dynamicLight;
            for (final EntityTrackerSystems.EntityViewer viewer : visibleTo.values()) {
                viewer.queueUpdate(ref, update);
            }
        }
        
        private static void queueRemoveFor(@Nonnull final Ref<EntityStore> ref, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo) {
            for (final EntityTrackerSystems.EntityViewer viewer : visibleTo.values()) {
                viewer.queueRemove(ref, ComponentUpdateType.DynamicLight);
            }
        }
    }
    
    public static class NewSpawnEntityTrackerUpdate extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final ComponentType<EntityStore, NewSpawnComponent> newSpawnComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public NewSpawnEntityTrackerUpdate() {
            this.visibleComponentType = EntityTrackerSystems.Visible.getComponentType();
            this.newSpawnComponentType = NewSpawnComponent.getComponentType();
            this.query = (Query<EntityStore>)Query.and(this.visibleComponentType, this.newSpawnComponentType);
        }
        
        @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 NewSpawnComponent newSpawnComponent = archetypeChunk.getComponent(index, this.newSpawnComponentType);
            assert newSpawnComponent != null;
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            if (!visibleComponent.newlyVisibleTo.isEmpty()) {
                final ComponentUpdate update = new ComponentUpdate();
                update.type = ComponentUpdateType.NewSpawn;
                for (final Map.Entry<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> entry : visibleComponent.newlyVisibleTo.entrySet()) {
                    entry.getValue().queueUpdate(ref, update);
                }
            }
        }
    }
    
    public static class NewSpawnTick extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, NewSpawnComponent> newSpawnComponentType;
        
        public NewSpawnTick() {
            this.newSpawnComponentType = NewSpawnComponent.getComponentType();
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.newSpawnComponentType;
        }
        
        @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 NewSpawnComponent newSpawnComponent = archetypeChunk.getComponent(index, this.newSpawnComponentType);
            assert newSpawnComponent != null;
            if (newSpawnComponent.newSpawnWindowPassed(dt)) {
                commandBuffer.removeComponent(archetypeChunk.getReferenceTo(index), this.newSpawnComponentType);
            }
        }
    }
}
