package common.packet;

import java.io.IOException;
import java.util.List;

import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.world.Chunk;

public class SPacketMapChunkBulk implements Packet<IClientPlayer>
{
    private int[] xPositions;
    private int[] zPositions;
    private SPacketChunkData.Extracted[] chunksData;
    private boolean isOverworld;

    public SPacketMapChunkBulk()
    {
    }

    public SPacketMapChunkBulk(List<Chunk> chunks)
    {
        int i = chunks.size();
        this.xPositions = new int[i];
        this.zPositions = new int[i];
        this.chunksData = new SPacketChunkData.Extracted[i];
        this.isOverworld = !((Chunk)chunks.get(0)).getWorld().dimension.hasNoLight();

        for (int j = 0; j < i; ++j)
        {
            Chunk chunk = (Chunk)chunks.get(j);
            SPacketChunkData.Extracted s21packetchunkdata$extracted = SPacketChunkData.getExtractedData(chunk, true, this.isOverworld, 0xffffffff);
            this.xPositions[j] = chunk.xPos;
            this.zPositions[j] = chunk.zPos;
            this.chunksData[j] = s21packetchunkdata$extracted;
        }
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.isOverworld = buf.readBoolean();
        int i = buf.readVarIntFromBuffer();
        this.xPositions = new int[i];
        this.zPositions = new int[i];
        this.chunksData = new SPacketChunkData.Extracted[i];

        for (int j = 0; j < i; ++j)
        {
            this.xPositions[j] = buf.readInt();
            this.zPositions[j] = buf.readInt();
            this.chunksData[j] = new SPacketChunkData.Extracted();
            this.chunksData[j].dataSize = buf.readInt();
            this.chunksData[j].data = new byte[SPacketChunkData.getSize(Integer.bitCount(this.chunksData[j].dataSize), this.isOverworld, true)];
        }

        for (int k = 0; k < i; ++k)
        {
            buf.readBytes(this.chunksData[k].data);
        }
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeBoolean(this.isOverworld);
        buf.writeVarIntToBuffer(this.chunksData.length);

        for (int i = 0; i < this.xPositions.length; ++i)
        {
            buf.writeInt(this.xPositions[i]);
            buf.writeInt(this.zPositions[i]);
            buf.writeInt(this.chunksData[i].dataSize);
        }

        for (int j = 0; j < this.xPositions.length; ++j)
        {
            buf.writeBytes(this.chunksData[j].data);
        }
    }

    /**
     * Passes this Packet on to the NetHandler for processing.
     */
    public void processPacket(IClientPlayer handler)
    {
        handler.handleMapChunkBulk(this);
    }

    public int getChunkX(int p_149255_1_)
    {
        return this.xPositions[p_149255_1_];
    }

    public int getChunkZ(int p_149253_1_)
    {
        return this.zPositions[p_149253_1_];
    }

    public int getChunkCount()
    {
        return this.xPositions.length;
    }

    public byte[] getChunkBytes(int p_149256_1_)
    {
        return this.chunksData[p_149256_1_].data;
    }

    public int getChunkSize(int p_179754_1_)
    {
        return this.chunksData[p_179754_1_].dataSize;
    }
}
