package common.packet;

import java.io.IOException;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;

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

    public SPacketMapChunkBulk()
    {
    }

    public SPacketMapChunkBulk(int[] x, int[] z, SPacketChunkData.Extracted[] chunks, boolean sky)
    {
    	this.xPositions = x;
    	this.zPositions = z;
    	this.chunksData = chunks;
    	this.sky = sky;
    }
    
    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.sky = buf.readBoolean();
        int i = buf.readVarInt();
        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].extend = new int[buf.readVarInt()];
            for(int z = 0; z < this.chunksData[j].extend.length; z++) {
            	this.chunksData[j].extend[z] = buf.readVarInt();
            }
            this.chunksData[j].data = new byte[SPacketChunkData.getSize(this.chunksData[j].extend.length, this.sky, 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.sky);
        buf.writeVarInt(this.chunksData.length);

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

        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[] getChunkExtend(int index)
    {
        return this.chunksData[index].extend;
    }
}
