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

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

import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.component.system.HolderSystem;
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.protocol.Direction;
import com.hypixel.hytale.protocol.Position;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.protocol.ModelTransform;
import com.hypixel.hytale.server.core.util.PositionUtil;
import com.hypixel.hytale.math.vector.Vector3f;
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 com.hypixel.hytale.server.core.modules.entity.component.HeadRotation;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
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 TransformSystems
{
    public static class EntityTrackerUpdate extends EntityTickingSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, EntityTrackerSystems.Visible> visibleComponentType;
        @Nonnull
        private final ComponentType<EntityStore, TransformComponent> transformComponentType;
        @Nonnull
        private final ComponentType<EntityStore, HeadRotation> headRotationComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityTrackerUpdate() {
            this.visibleComponentType = EntityTrackerSystems.Visible.getComponentType();
            this.transformComponentType = TransformComponent.getComponentType();
            this.headRotationComponentType = HeadRotation.getComponentType();
            this.query = (Query<EntityStore>)Query.and(this.visibleComponentType, this.transformComponentType);
        }
        
        @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 TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
            assert transformComponent != null;
            final HeadRotation headRotationComponent = archetypeChunk.getComponent(index, this.headRotationComponentType);
            final ModelTransform sentTransform = transformComponent.getSentTransform();
            final Vector3d position = transformComponent.getPosition();
            final Vector3f headRotation = (headRotationComponent != null) ? headRotationComponent.getRotation() : Vector3f.ZERO;
            final Vector3f bodyRotation = transformComponent.getRotation();
            final Position sentPosition = sentTransform.position;
            final Direction sentLookOrientation = sentTransform.lookOrientation;
            final Direction sentBodyOrientation = sentTransform.bodyOrientation;
            if (!PositionUtil.equals(position, sentPosition) || !PositionUtil.equals(headRotation, sentLookOrientation) || !PositionUtil.equals(bodyRotation, sentBodyOrientation)) {
                PositionUtil.assign(sentPosition, position);
                PositionUtil.assign(sentLookOrientation, headRotation);
                PositionUtil.assign(sentBodyOrientation, bodyRotation);
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), sentTransform, visibleComponent.visibleTo, false);
            }
            else if (!visibleComponent.newlyVisibleTo.isEmpty()) {
                queueUpdatesFor(archetypeChunk.getReferenceTo(index), sentTransform, visibleComponent.newlyVisibleTo, true);
            }
        }
        
        private static void queueUpdatesFor(@Nonnull final Ref<EntityStore> ref, @Nonnull final ModelTransform sentTransform, @Nonnull final Map<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> visibleTo, final boolean newlyVisible) {
            final ComponentUpdate update = new ComponentUpdate();
            update.type = ComponentUpdateType.Transform;
            update.transform = sentTransform;
            for (final Map.Entry<Ref<EntityStore>, EntityTrackerSystems.EntityViewer> entry : visibleTo.entrySet()) {
                if (!newlyVisible && ref.equals(entry.getKey())) {
                    continue;
                }
                entry.getValue().queueUpdate(ref, update);
            }
        }
    }
    
    public static class OnRemove extends HolderSystem<EntityStore>
    {
        @Nonnull
        private final ComponentType<EntityStore, TransformComponent> transformComponentType;
        
        public OnRemove() {
            this.transformComponentType = TransformComponent.getComponentType();
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
        }
        
        @Override
        public void onEntityRemoved(@Nonnull final Holder<EntityStore> holder, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store) {
            holder.getComponent(this.transformComponentType).setChunkLocation(null, null);
        }
        
        @Override
        public Query<EntityStore> getQuery() {
            return this.transformComponentType;
        }
    }
}
