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

package com.hypixel.hytale.server.core.modules.entityui;

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.ArchetypeChunk;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.SystemGroup;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.server.core.modules.entity.AllLegacyLivingEntityTypesQuery;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.AddReason;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Holder;
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 UIComponentSystems
{
    public static class Setup extends HolderSystem<EntityStore>
    {
        private final ComponentType<EntityStore, UIComponentList> uiComponentListComponentType;
        
        public Setup(final ComponentType<EntityStore, UIComponentList> uiComponentListType) {
            this.uiComponentListComponentType = uiComponentListType;
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            UIComponentList components = holder.getComponent(this.uiComponentListComponentType);
            if (components == null) {
                components = holder.ensureAndGetComponent(this.uiComponentListComponentType);
                components.update();
            }
        }
        
        @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 AllLegacyLivingEntityTypesQuery.INSTANCE;
        }
    }
    
    public static class Update extends EntityTickingSystem<EntityStore>
    {
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        private final ComponentType<EntityStore, UIComponentList> uiComponentListComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public Update(final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType, final ComponentType<EntityStore, UIComponentList> uiComponentListComponentType) {
            this.visibleComponentType = visibleComponentType;
            this.uiComponentListComponentType = uiComponentListComponentType;
            this.query = (Query<EntityStore>)Query.and(visibleComponentType, uiComponentListComponentType);
        }
        
        @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 visible = archetypeChunk.getComponent(index, this.visibleComponentType);
            final UIComponentList uiComponentList = archetypeChunk.getComponent(index, this.uiComponentListComponentType);
            if (!visible.newlyVisibleTo.isEmpty()) {
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), uiComponentList, visible.newlyVisibleTo);
            }
        }
        
        private static void queueUpdatesFor(final Ref<EntityStore> ref, @Nonnull final UIComponentList uiComponentList, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo) {
            final ComponentUpdate update = new ComponentUpdate();
            update.type = ComponentUpdateType.UIComponents;
            update.entityUIComponents = uiComponentList.getComponentIds();
            for (final EntityTrackerSystems.EntityViewer viewer : visibleTo.values()) {
                viewer.queueUpdate(ref, update);
            }
        }
    }
    
    public static class Remove extends RefChangeSystem<EntityStore, UIComponentList>
    {
        private final ComponentType<EntityStore, UIComponentList> uiComponentListComponentType;
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public Remove(final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType, final ComponentType<EntityStore, UIComponentList> componentType) {
            this.visibleComponentType = visibleComponentType;
            this.uiComponentListComponentType = componentType;
            this.query = (Query<EntityStore>)Query.and(visibleComponentType, componentType);
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Nonnull
        @Override
        public ComponentType<EntityStore, UIComponentList> componentType() {
            return this.uiComponentListComponentType;
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final UIComponentList component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<EntityStore> ref, final UIComponentList oldComponent, @Nonnull final UIComponentList newComponent, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final UIComponentList 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.UIComponents);
            }
        }
    }
}
