package common.packet;

import java.io.IOException;

import common.init.BlockRegistry;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.BlockPos;
import common.util.ChunkPos;
import common.world.Chunk;
import common.world.State;

public class SPacketMultiBlockChange implements Packet<IClientPlayer>
{
    private ChunkPos chunkPosCoord;
    private SPacketMultiBlockChange.BlockUpdateData[] changedBlocks;

    public SPacketMultiBlockChange()
    {
    }

    public SPacketMultiBlockChange(int p_i45181_1_, int[] crammedPositionsIn, Chunk chunkIn)
    {
        this.chunkPosCoord = new ChunkPos(chunkIn.xPos, chunkIn.zPos);
        this.changedBlocks = new SPacketMultiBlockChange.BlockUpdateData[p_i45181_1_];

        for (int i = 0; i < this.changedBlocks.length; ++i)
        {
            this.changedBlocks[i] = new SPacketMultiBlockChange.BlockUpdateData(crammedPositionsIn[i], chunkIn);
        }
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.chunkPosCoord = new ChunkPos(buf.readInt(), buf.readInt());
        this.changedBlocks = new SPacketMultiBlockChange.BlockUpdateData[buf.readVarIntFromBuffer()];

        for (int i = 0; i < this.changedBlocks.length; ++i)
        {
            this.changedBlocks[i] = new SPacketMultiBlockChange.BlockUpdateData(buf.readInt(), (State)BlockRegistry.STATEMAP.getByValue(buf.readVarIntFromBuffer()));
        }
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeInt(this.chunkPosCoord.x);
        buf.writeInt(this.chunkPosCoord.z);
        buf.writeVarIntToBuffer(this.changedBlocks.length);

        for (SPacketMultiBlockChange.BlockUpdateData s22packetmultiblockchange$blockupdatedata : this.changedBlocks)
        {
            buf.writeInt(s22packetmultiblockchange$blockupdatedata.getRawPos());
            buf.writeVarIntToBuffer(BlockRegistry.STATEMAP.get(s22packetmultiblockchange$blockupdatedata.getBlockState()));
        }
    }

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

    public SPacketMultiBlockChange.BlockUpdateData[] getChangedBlocks()
    {
        return this.changedBlocks;
    }

    public class BlockUpdateData
    {
        private final int position;
        private final State blockState;

        public BlockUpdateData(int raw, State state)
        {
            this.position = raw;
            this.blockState = state;
        }

        public BlockUpdateData(int raw, Chunk chunkIn)
        {
            this.position = raw;
            this.blockState = chunkIn.getState(this.getPos());
        }

        public BlockPos getPos()
        {
            ChunkPos r = SPacketMultiBlockChange.this.chunkPosCoord;
			return new BlockPos(new BlockPos((r.x << 4) + (this.position >> 13 & 15), this.position & 511, (r.z << 4) + (this.position >> 9 & 15)));
        }

        public int getRawPos()
        {
            return this.position;
        }

        public State getBlockState()
        {
            return this.blockState;
        }
    }
}
