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

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

import com.hypixel.hytale.component.Component;
import java.util.Iterator;
import com.hypixel.hytale.protocol.ComponentUpdateType;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.modules.entity.tracker.EntityTrackerSystems;
import com.hypixel.hytale.component.system.RefChangeSystem;
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.query.Query;
import com.hypixel.hytale.server.core.modules.entity.component.PersistentDynamicLight;
import com.hypixel.hytale.server.core.modules.entity.component.DynamicLight;
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 DynamicLightSystems
{
    public static class Setup extends HolderSystem<EntityStore>
    {
        private final ComponentType<EntityStore, DynamicLight> dynamicLightComponentType;
        private final ComponentType<EntityStore, PersistentDynamicLight> persistentDynamicLightComponentType;
        private final Query<EntityStore> query;
        
        public Setup() {
            this.dynamicLightComponentType = DynamicLight.getComponentType();
            this.persistentDynamicLightComponentType = PersistentDynamicLight.getComponentType();
            this.query = (Query<EntityStore>)Query.and(this.persistentDynamicLightComponentType, Query.not((Query<Object>)this.dynamicLightComponentType));
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            final PersistentDynamicLight persistentLight = holder.getComponent(this.persistentDynamicLightComponentType);
            holder.putComponent(this.dynamicLightComponentType, new DynamicLight(persistentLight.getColorLight()));
        }
        
        @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 EntityTrackerRemove extends RefChangeSystem<EntityStore, DynamicLight>
    {
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        
        public EntityTrackerRemove(final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType) {
            this.visibleComponentType = visibleComponentType;
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.visibleComponentType;
        }
        
        @Nonnull
        @Override
        public ComponentType<EntityStore, DynamicLight> componentType() {
            return DynamicLight.getComponentType();
        }
        
        @Override
        public void onComponentAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final DynamicLight component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentSet(@Nonnull final Ref<EntityStore> ref, final DynamicLight oldComponent, @Nonnull final DynamicLight newComponent, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
        
        @Override
        public void onComponentRemoved(@Nonnull final Ref<EntityStore> ref, @Nonnull final DynamicLight component, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final EntityTrackerSystems.Visible visible = commandBuffer.getComponent(ref, this.visibleComponentType);
            if (visible != null) {
                for (final EntityTrackerSystems.EntityViewer viewer : visible.visibleTo.values()) {
                    viewer.queueRemove(ref, ComponentUpdateType.DynamicLight);
                }
            }
        }
    }
}
