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

package com.hypixel.hytale.server.core.entity.nameplate;

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.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 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 NameplateSystems
{
    public static class EntityTrackerUpdate extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final ComponentType<EntityStore, Nameplate> componentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityTrackerUpdate(@Nonnull final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType, @Nonnull final ComponentType<EntityStore, Nameplate> componentType) {
            this.visibleComponentType = visibleComponentType;
            this.componentType = componentType;
            this.query = (Query<EntityStore>)Query.and(visibleComponentType, componentType);
        }
        
        @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 Nameplate nameplateComponent = archetypeChunk.getComponent(index, this.componentType);
            assert nameplateComponent != null;
            if (nameplateComponent.consumeNetworkOutdated()) {
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), nameplateComponent, visibleComponent.visibleTo);
            }
            else if (!visibleComponent.newlyVisibleTo.isEmpty()) {
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), nameplateComponent, visibleComponent.newlyVisibleTo);
            }
        }
        
        private static void queueUpdatesFor(@Nonnull final Ref<EntityStore> ref, @Nonnull final Nameplate nameplateComponent, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo) {
            final ComponentUpdate update = new ComponentUpdate();
            update.type = ComponentUpdateType.Nameplate;
            update.nameplate = new com.hypixel.hytale.protocol.Nameplate();
            update.nameplate.text = nameplateComponent.getText();
            for (final EntityTrackerSystems.EntityViewer viewer : visibleTo.values()) {
                viewer.queueUpdate(ref, update);
            }
        }
    }
    
    public static class EntityTrackerRemove extends RefChangeSystem<EntityStore, Nameplate>
    {
        @Nonnull
        private final ComponentType<EntityStore, Nameplate> componentType;
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityTrackerRemove(@Nonnull final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType, @Nonnull final ComponentType<EntityStore, Nameplate> componentType) {
            this.visibleComponentType = visibleComponentType;
            this.componentType = componentType;
            this.query = (Query<EntityStore>)Query.and(visibleComponentType, componentType);
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public ComponentType<EntityStore, Nameplate> componentType() {
            return this.componentType;
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final Nameplate component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<EntityStore> ref, final Nameplate oldComponent, @Nonnull final Nameplate newComponent, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final Nameplate component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final EntityTrackerSystems.Visible visibleComponent = store.getComponent(ref, this.visibleComponentType);
            assert visibleComponent != null;
            for (final EntityTrackerSystems.EntityViewer viewer : visibleComponent.visibleTo.values()) {
                viewer.queueRemove(ref, ComponentUpdateType.Nameplate);
            }
        }
    }
}
