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

package com.hypixel.hytale.builtin.adventure.objectives.markers.reachlocation;

import it.unimi.dsi.fastutil.objects.ObjectList;
import java.util.List;
import java.util.Collection;
import java.util.logging.Level;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.component.dependency.OrderPriority;
import com.hypixel.hytale.server.core.modules.entity.system.PlayerSpatialSystem;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.dependency.Dependency;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.component.spatial.SpatialResource;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.server.core.modules.entity.tracker.NetworkId;
import com.hypixel.hytale.component.system.HolderSystem;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.builtin.adventure.objectives.Objective;
import java.util.Iterator;
import com.hypixel.hytale.builtin.adventure.objectives.ObjectiveDataStore;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.builtin.adventure.objectives.task.ObjectiveTaskRef;
import com.hypixel.hytale.builtin.adventure.objectives.task.ReachLocationTask;
import com.hypixel.hytale.builtin.adventure.objectives.ObjectivePlugin;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.RefSystem;
import java.util.function.Supplier;
import java.util.HashSet;
import java.util.UUID;
import java.util.Set;
import com.hypixel.hytale.logger.HytaleLogger;

public class ReachLocationMarkerSystems
{
    private static final HytaleLogger LOGGER;
    private static final ThreadLocal<Set<UUID>> THREAD_LOCAL_TEMP_UUIDS;
    
    static {
        LOGGER = HytaleLogger.forEnclosingClass();
        THREAD_LOCAL_TEMP_UUIDS = ThreadLocal.withInitial((Supplier<? extends Set<UUID>>)HashSet::new);
    }
    
    public static class EntityAdded extends RefSystem<EntityStore>
    {
        private final ComponentType<EntityStore, ReachLocationMarker> reachLocationMarkerComponent;
        private final ComponentType<EntityStore, TransformComponent> transformComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        
        public EntityAdded(final ComponentType<EntityStore, ReachLocationMarker> reachLocationMarkerComponent) {
            this.reachLocationMarkerComponent = reachLocationMarkerComponent;
            this.transformComponentType = TransformComponent.getComponentType();
            this.query = (Query<EntityStore>)Query.and(reachLocationMarkerComponent, this.transformComponentType);
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Override
        public void onEntityAdded(@Nonnull final Ref<EntityStore> ref, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
            final ReachLocationMarker reachLocationMarkerComponent = commandBuffer.getComponent(ref, this.reachLocationMarkerComponent);
            assert reachLocationMarkerComponent != null;
            final TransformComponent transformComponent = commandBuffer.getComponent(ref, this.transformComponentType);
            assert transformComponent != null;
            final Vector3d pos = transformComponent.getPosition();
            final ObjectiveDataStore objectiveDataStore = ObjectivePlugin.get().getObjectiveDataStore();
            final Set<ObjectiveTaskRef<ReachLocationTask>> taskRefs = objectiveDataStore.getTaskRefsForType(ReachLocationTask.class);
            for (final ObjectiveTaskRef<ReachLocationTask> taskRef : taskRefs) {
                final Objective objective = objectiveDataStore.getObjective(taskRef.getObjectiveUUID());
                if (objective == null) {
                    continue;
                }
                taskRef.getObjectiveTask().setupMarker(objective, reachLocationMarkerComponent, pos, commandBuffer);
            }
        }
        
        @Override
        public void onEntityRemove(@Nonnull final Ref<EntityStore> ref, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        }
    }
    
    public static class EnsureNetworkSendable extends HolderSystem<EntityStore>
    {
        private final Query<EntityStore> query;
        
        public EnsureNetworkSendable() {
            this.query = (Query<EntityStore>)Query.and(ReachLocationMarker.getComponentType(), Query.not((Query<Object>)NetworkId.getComponentType()));
        }
        
        @Override
        public void onEntityAdd(@Nonnull final Holder<EntityStore> holder, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store) {
            holder.addComponent(NetworkId.getComponentType(), new NetworkId(store.getExternalData().takeNextNetworkId()));
        }
        
        @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 Ticking extends EntityTickingSystem<EntityStore>
    {
        private final ComponentType<EntityStore, ReachLocationMarker> reachLocationMarkerComponent;
        private final ComponentType<EntityStore, TransformComponent> transformComponentType;
        private final ResourceType<EntityStore, SpatialResource<Ref<EntityStore>, EntityStore>> playerSpatialComponent;
        private final ComponentType<EntityStore, UUIDComponent> uuidComponentType;
        @Nonnull
        private final Query<EntityStore> query;
        @Nonnull
        private final Set<Dependency<EntityStore>> dependencies;
        
        public Ticking(final ComponentType<EntityStore, ReachLocationMarker> reachLocationMarkerComponent, final ResourceType<EntityStore, SpatialResource<Ref<EntityStore>, EntityStore>> playerSpatialComponent) {
            this.uuidComponentType = UUIDComponent.getComponentType();
            this.reachLocationMarkerComponent = reachLocationMarkerComponent;
            this.transformComponentType = TransformComponent.getComponentType();
            this.playerSpatialComponent = playerSpatialComponent;
            this.query = (Query<EntityStore>)Query.and(reachLocationMarkerComponent, this.transformComponentType);
            this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.AFTER, PlayerSpatialSystem.class, OrderPriority.CLOSEST));
        }
        
        @Nonnull
        @Override
        public Set<Dependency<EntityStore>> getDependencies() {
            return this.dependencies;
        }
        
        @Nonnull
        @Override
        public Query<EntityStore> getQuery() {
            return this.query;
        }
        
        @Override
        public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
            return false;
        }
        
        @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 ReachLocationMarker reachLocationMarkerComponent = archetypeChunk.getComponent(index, this.reachLocationMarkerComponent);
            assert reachLocationMarkerComponent != null;
            final String markerId = reachLocationMarkerComponent.getMarkerId();
            final ReachLocationMarkerAsset asset = ReachLocationMarkerAsset.getAssetMap().getAsset(markerId);
            if (asset == null) {
                ReachLocationMarkerSystems.LOGGER.at(Level.WARNING).log("No ReachLocationMarkerAsset found for ID '%s', entity removed! %s", markerId, reachLocationMarkerComponent);
                commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
                return;
            }
            final Set<UUID> previousPlayers = ReachLocationMarkerSystems.THREAD_LOCAL_TEMP_UUIDS.get();
            previousPlayers.clear();
            previousPlayers.addAll(reachLocationMarkerComponent.getPlayers());
            final Set<UUID> players = reachLocationMarkerComponent.getPlayers();
            players.clear();
            final SpatialResource<Ref<EntityStore>, EntityStore> spatialResource = store.getResource(this.playerSpatialComponent);
            final ObjectList<Ref<EntityStore>> results = SpatialResource.getThreadLocalReferenceList();
            final TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
            assert transformComponent != null;
            final Vector3d position = transformComponent.getPosition();
            spatialResource.getSpatialStructure().ordered(position, asset.getRadius(), results);
            final ObjectiveDataStore objectiveDataStore = ObjectivePlugin.get().getObjectiveDataStore();
            for (int i = 0; i < results.size(); ++i) {
                final Ref<EntityStore> otherEntityReference = results.get(i);
                final UUIDComponent otherUuidComponent = commandBuffer.getComponent(otherEntityReference, this.uuidComponentType);
                assert otherUuidComponent != null;
                final UUID otherUuid = otherUuidComponent.getUuid();
                players.add(otherUuid);
                if (!previousPlayers.contains(otherUuid)) {
                    final Set<ObjectiveTaskRef<ReachLocationTask>> taskRefs = objectiveDataStore.getTaskRefsForType(ReachLocationTask.class);
                    for (final ObjectiveTaskRef<ReachLocationTask> taskRef : taskRefs) {
                        final Objective objective = objectiveDataStore.getObjective(taskRef.getObjectiveUUID());
                        if (objective == null) {
                            continue;
                        }
                        taskRef.getObjectiveTask().onPlayerReachLocationMarker(store, otherEntityReference, markerId, objective);
                    }
                }
            }
        }
    }
}
