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

package com.hypixel.hytale.server.npc.systems;

import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.spawning.SpawningPlugin;
import com.hypixel.hytale.server.core.modules.time.WorldTimeResource;
import com.hypixel.hytale.server.npc.components.SpawnBeaconReference;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.protocol.AnimationSlot;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.component.system.tick.EntityTickingSystem;
import com.hypixel.hytale.component.Archetype;
import com.hypixel.hytale.component.dependency.SystemDependency;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathSystems;
import com.hypixel.hytale.component.dependency.Order;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.dependency.Dependency;
import java.util.Set;
import com.hypixel.hytale.server.core.modules.entity.component.ModelComponent;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;

public class NPCPreTickSystem extends SteppableTickingSystem
{
    private static final float DEFAULT_DESPAWN_CHECK_DELAY = 30.0f;
    @Nonnull
    private final ComponentType<EntityStore, NPCEntity> npcComponentType;
    @Nonnull
    private final ComponentType<EntityStore, TransformComponent> transformComponentType;
    @Nonnull
    private final ComponentType<EntityStore, ModelComponent> modelComponentType;
    @Nonnull
    private final Set<Dependency<EntityStore>> dependencies;
    @Nonnull
    private final Query<EntityStore> query;
    
    public NPCPreTickSystem(@Nonnull final ComponentType<EntityStore, NPCEntity> npcComponentType) {
        this.transformComponentType = TransformComponent.getComponentType();
        this.modelComponentType = ModelComponent.getComponentType();
        this.npcComponentType = npcComponentType;
        this.dependencies = (Set<Dependency<EntityStore>>)Set.of(new SystemDependency(Order.BEFORE, DeathSystems.CorpseRemoval.class));
        this.query = (Query<EntityStore>)Archetype.of(npcComponentType, this.transformComponentType);
    }
    
    @Nonnull
    @Override
    public Set<Dependency<EntityStore>> getDependencies() {
        return this.dependencies;
    }
    
    @Override
    public boolean isParallel(final int archetypeChunkSize, final int taskCount) {
        return EntityTickingSystem.maybeUseParallel(archetypeChunkSize, taskCount);
    }
    
    @Nonnull
    @Override
    public Query<EntityStore> getQuery() {
        return this.query;
    }
    
    @Override
    public void steppedTick(final float dt, final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer) {
        final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
        final NPCEntity npcComponent = archetypeChunk.getComponent(index, this.npcComponentType);
        assert npcComponent != null;
        final TransformComponent transformComponent = archetypeChunk.getComponent(index, this.transformComponentType);
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        npcComponent.storeTickStartPosition(position);
        if (npcComponent.isPlayingDespawnAnim()) {
            if (npcComponent.tickDespawnAnimationRemainingSeconds(dt)) {
                commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
            }
            return;
        }
        if (npcComponent.isDespawning()) {
            if (npcComponent.tickDespawnRemainingSeconds(dt)) {
                npcComponent.setDespawning(false);
                final ModelComponent modelComponent = archetypeChunk.getComponent(index, this.modelComponentType);
                if (modelComponent != null && modelComponent.getModel().getAnimationSetMap().containsKey("Despawn")) {
                    npcComponent.setPlayingDespawnAnim(true);
                    npcComponent.setDespawnAnimationRemainingSeconds(npcComponent.getRole().getDespawnAnimationTime());
                    commandBuffer.run(_store -> npcComponent.playAnimation(ref, AnimationSlot.Status, "Despawn", _store));
                    return;
                }
                commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
            }
            return;
        }
        if (npcComponent.tickDespawnCheckRemainingSeconds(dt)) {
            npcComponent.setDespawnCheckRemainingSeconds(30.0f);
            if (npcComponent.getRole().getStateSupport().isInBusyState()) {
                return;
            }
            final SpawnBeaconReference spawnBeaconReference = archetypeChunk.getComponent(index, SpawnBeaconReference.getComponentType());
            final WorldTimeResource timeManager = commandBuffer.getResource(WorldTimeResource.getResourceType());
            if (SpawningPlugin.get().shouldNPCDespawn(store, npcComponent, timeManager, npcComponent.getSpawnConfiguration(), spawnBeaconReference != null)) {
                npcComponent.setDespawning(true);
                npcComponent.setDespawnRemainingSeconds(0.0f);
            }
        }
    }
}
