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

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

import com.hypixel.hytale.component.system.DelayedSystem;
import com.hypixel.hytale.server.core.event.events.ecs.BreakBlockEvent;
import com.hypixel.hytale.component.system.EcsEvent;
import javax.annotation.Nullable;
import com.hypixel.hytale.component.Archetype;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.event.events.ecs.DamageBlockEvent;
import com.hypixel.hytale.component.system.EntityEventSystem;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.blackboard.Blackboard;
import com.hypixel.hytale.component.ResourceType;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.StoreSystem;

public class BlackboardSystems
{
    public static class InitSystem extends StoreSystem<EntityStore>
    {
        @Nonnull
        private final ResourceType<EntityStore, Blackboard> resourceType;
        
        public InitSystem(@Nonnull final ResourceType<EntityStore, Blackboard> resourceType) {
            this.resourceType = resourceType;
        }
        
        @Override
        public void onSystemAddedToStore(@Nonnull final Store<EntityStore> store) {
            store.getResource(this.resourceType).init(store.getExternalData().getWorld());
        }
        
        @Override
        public void onSystemRemovedFromStore(@Nonnull final Store<EntityStore> store) {
            store.getResource(this.resourceType).onWorldRemoved();
        }
    }
    
    public static class DamageBlockEventSystem extends EntityEventSystem<EntityStore, DamageBlockEvent>
    {
        public DamageBlockEventSystem() {
            super(DamageBlockEvent.class);
        }
        
        @Override
        public void handle(final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final DamageBlockEvent event) {
            final Blackboard blackBoardResource = store.getResource(Blackboard.getResourceType());
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            blackBoardResource.onEntityDamageBlock(ref, event);
        }
        
        @Nullable
        @Override
        public Query<EntityStore> getQuery() {
            return (Query<EntityStore>)Archetype.empty();
        }
    }
    
    public static class BreakBlockEventSystem extends EntityEventSystem<EntityStore, BreakBlockEvent>
    {
        public BreakBlockEventSystem() {
            super(BreakBlockEvent.class);
        }
        
        @Override
        public void handle(final int index, @Nonnull final ArchetypeChunk<EntityStore> archetypeChunk, @Nonnull final Store<EntityStore> store, @Nonnull final CommandBuffer<EntityStore> commandBuffer, @Nonnull final BreakBlockEvent event) {
            final Blackboard blackBoardResource = store.getResource(Blackboard.getResourceType());
            final Ref<EntityStore> ref = archetypeChunk.getReferenceTo(index);
            blackBoardResource.onEntityBreakBlock(ref, event);
        }
        
        @Nullable
        @Override
        public Query<EntityStore> getQuery() {
            return (Query<EntityStore>)Archetype.empty();
        }
    }
    
    public static class TickingSystem extends DelayedSystem<EntityStore>
    {
        private static final float SYSTEM_INTERVAL = 5.0f;
        @Nonnull
        private final ResourceType<EntityStore, Blackboard> resourceType;
        
        public TickingSystem(@Nonnull final ResourceType<EntityStore, Blackboard> resourceType) {
            super(5.0f);
            this.resourceType = resourceType;
        }
        
        @Override
        public void delayedTick(final float dt, final int systemIndex, @Nonnull final Store<EntityStore> store) {
            store.getResource(this.resourceType).cleanupViews();
        }
    }
}
