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

package com.hypixel.hytale.server.core.entity.entities.player.windows;

import com.hypixel.hytale.server.core.asset.type.item.config.Item;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.protocol.packets.window.WindowType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;

public abstract class BlockWindow extends Window implements ValidatedWindow
{
    private static final float MAX_DISTANCE = 7.0f;
    protected final int x;
    protected final int y;
    protected final int z;
    @Nonnull
    protected BlockType blockType;
    protected final int rotationIndex;
    private double maxDistance;
    private double maxDistanceSqr;
    
    public BlockWindow(@Nonnull final WindowType windowType, final int x, final int y, final int z, final int rotationIndex, @Nonnull final BlockType blockType) {
        super(windowType);
        this.maxDistance = 7.0;
        this.maxDistanceSqr = this.maxDistance * this.maxDistance;
        this.x = x;
        this.y = y;
        this.z = z;
        this.rotationIndex = rotationIndex;
        this.blockType = blockType;
    }
    
    public int getX() {
        return this.x;
    }
    
    public int getY() {
        return this.y;
    }
    
    public int getZ() {
        return this.z;
    }
    
    public int getRotationIndex() {
        return this.rotationIndex;
    }
    
    @Nonnull
    public BlockType getBlockType() {
        return this.blockType;
    }
    
    public void setMaxDistance(final double maxDistance) {
        this.maxDistance = maxDistance;
        this.maxDistanceSqr = maxDistance * maxDistance;
    }
    
    public double getMaxDistance() {
        return this.maxDistance;
    }
    
    @Override
    public boolean validate(@Nonnull final Ref<EntityStore> ref, @Nonnull final ComponentAccessor<EntityStore> store) {
        final World world = store.getExternalData().getWorld();
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        if (transformComponent == null || transformComponent.getPosition().distanceSquaredTo(this.x, this.y, this.z) > this.maxDistanceSqr) {
            return false;
        }
        final ChunkStore chunkStore = world.getChunkStore();
        final long chunkIndex = ChunkUtil.indexChunkFromBlock(this.x, this.z);
        final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
        if (chunkRef == null || !chunkRef.isValid()) {
            return false;
        }
        final Store<ChunkStore> chunkComponentStore = chunkStore.getStore();
        final WorldChunk worldChunkComponent = chunkComponentStore.getComponent(chunkRef, WorldChunk.getComponentType());
        if (worldChunkComponent == null) {
            return false;
        }
        final BlockType currentBlockType = worldChunkComponent.getBlockType(this.x, this.y, this.z);
        if (currentBlockType == null) {
            return false;
        }
        final Item currentItem = currentBlockType.getItem();
        return currentItem != null && currentItem.equals(this.blockType.getItem());
    }
}
