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

package com.hypixel.hytale.server.npc.blackboard.view.resource;

import com.hypixel.hytale.server.npc.blackboard.view.IBlackboardView;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
import java.util.HashMap;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import java.util.Map;
import it.unimi.dsi.fastutil.ints.IntSet;
import com.hypixel.hytale.server.npc.blackboard.view.BlockRegionView;

public class ResourceView extends BlockRegionView<ResourceView>
{
    private final long index;
    private final IntSet[] reservationsBySection;
    private final Map<Ref<EntityStore>, BlockReservation> reservationsByEntity;
    
    public ResourceView(final long index) {
        this.reservationsBySection = new IntSet[10];
        this.reservationsByEntity = new HashMap<Ref<EntityStore>, BlockReservation>();
        this.index = index;
    }
    
    @Override
    public boolean isOutdated(@Nonnull final Ref<EntityStore> ref, @Nonnull final Store<EntityStore> store) {
        return false;
    }
    
    @Nonnull
    @Override
    public ResourceView getUpdatedView(@Nonnull final Ref<EntityStore> ref, @Nonnull final ComponentAccessor<EntityStore> componentAccessor) {
        return this;
    }
    
    @Override
    public void initialiseEntity(@Nonnull final Ref<EntityStore> ref, @Nonnull final NPCEntity npcComponent) {
    }
    
    @Override
    public void cleanup() {
    }
    
    @Override
    public void onWorldRemoved() {
    }
    
    public boolean isBlockReserved(final int x, final int y, final int z) {
        final IntSet section = this.reservationsBySection[BlockRegionView.indexSection(y)];
        return section != null && section.contains(BlockRegionView.indexBlock(x, y, z));
    }
    
    public void reserveBlock(@Nonnull final NPCEntity entity, final int x, final int y, final int z) {
        final int sectionIndex = BlockRegionView.indexSection(y);
        IntSet section = this.reservationsBySection[sectionIndex];
        if (section == null) {
            section = new IntOpenHashSet();
            this.reservationsBySection[sectionIndex] = section;
        }
        final int blockIndex = BlockRegionView.indexBlock(x, y, z);
        section.add(blockIndex);
        this.reservationsByEntity.put(entity.getReference(), new BlockReservation(sectionIndex, blockIndex));
    }
    
    public void clearReservation(@Nonnull final Ref<EntityStore> ref) {
        final BlockReservation reservation = this.reservationsByEntity.remove(ref);
        if (reservation == null) {
            return;
        }
        final IntSet section = this.reservationsBySection[reservation.getSectionIndex()];
        section.remove(reservation.getBlockIndex());
    }
    
    public long getIndex() {
        return this.index;
    }
    
    @Nonnull
    public Map<Ref<EntityStore>, BlockReservation> getReservationsByEntity() {
        return this.reservationsByEntity;
    }
    
    public static class BlockReservation
    {
        private final int sectionIndex;
        private final int blockIndex;
        
        public BlockReservation(final int sectionIndex, final int blockIndex) {
            this.sectionIndex = sectionIndex;
            this.blockIndex = blockIndex;
        }
        
        public int getSectionIndex() {
            return this.sectionIndex;
        }
        
        public int getBlockIndex() {
            return this.blockIndex;
        }
    }
}
