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

package com.hypixel.hytale.server.spawning.suppression.system;

import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.server.spawning.suppression.SpawnSuppressorEntry;
import java.util.UUID;
import java.util.Map;
import java.util.logging.Level;
import com.hypixel.hytale.server.spawning.SpawningPlugin;
import com.hypixel.hytale.server.spawning.assets.spawnsuppression.SpawnSuppression;
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.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.spawning.suppression.component.SpawnSuppressionController;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.server.spawning.spawnmarkers.SpawnMarkerEntity;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.RefSystem;

public class SpawnMarkerSuppressionSystem extends RefSystem<EntityStore>
{
    private final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType;
    private final ResourceType<EntityStore, SpawnSuppressionController> spawnSuppressionControllerResourceType;
    private final ComponentType<EntityStore, UUIDComponent> uuidComponentType;
    private final ComponentType<EntityStore, TransformComponent> transformComponentType;
    @Nonnull
    private final Query<EntityStore> query;
    
    public SpawnMarkerSuppressionSystem(final ComponentType<EntityStore, SpawnMarkerEntity> spawnMarkerEntityComponentType, final ResourceType<EntityStore, SpawnSuppressionController> spawnSuppressionControllerResourceType) {
        this.uuidComponentType = UUIDComponent.getComponentType();
        this.transformComponentType = TransformComponent.getComponentType();
        this.spawnMarkerEntityComponentType = spawnMarkerEntityComponentType;
        this.spawnSuppressionControllerResourceType = spawnSuppressionControllerResourceType;
        this.query = (Query<EntityStore>)Query.and(spawnMarkerEntityComponentType, this.uuidComponentType, this.transformComponentType);
    }
    
    @Override
    public void onEntityAdded(@Nonnull final Ref<EntityStore> reference, @Nonnull final AddReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final SpawnSuppressionController suppressionController = store.getResource(this.spawnSuppressionControllerResourceType);
        final SpawnMarkerEntity marker = store.getComponent(reference, this.spawnMarkerEntityComponentType);
        final TransformComponent transform = commandBuffer.getComponent(reference, this.transformComponentType);
        final UUIDComponent uuid = commandBuffer.getComponent(reference, this.uuidComponentType);
        final Map<UUID, SpawnSuppressorEntry> spawnSuppressorMap = suppressionController.getSpawnSuppressorMap();
        spawnSuppressorMap.forEach((id, entry) -> {
            final SpawnSuppression suppression = SpawnSuppression.getAssetMap().getAsset(entry.getSuppressionId());
            if (suppression == null) {
                new NullPointerException(String.format("No such suppression with ID %s", entry.getSuppressionId()));
                throw;
            }
            else if (!(!suppression.isSuppressSpawnMarkers())) {
                final double radius = suppression.getRadius();
                final double radiusSquared = radius * radius;
                if (transform.getPosition().distanceSquaredTo(entry.getPosition()) <= radiusSquared) {
                    marker.suppress(id);
                    SpawningPlugin.get().getLogger().at(Level.FINEST).log("Suppressing spawn marker %s on add/load", uuid.getUuid());
                }
            }
        });
    }
    
    @Override
    public void onEntityRemove(@Nonnull final Ref<EntityStore> reference, @Nonnull final RemoveReason reason, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
}
