package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import java.util.Collection;
import net.minecraft.world.level.saveddata.maps.MapDecoration;
import net.minecraft.network.protocol.Packet;

public class ClientboundMapItemDataPacket implements Packet<ClientGamePacketListener> {
    private int mapId;
    private byte scale;
    private boolean trackingPosition;
    private boolean locked;
    private MapDecoration[] decorations;
    private int startX;
    private int startY;
    private int width;
    private int height;
    private byte[] mapColors;
    
    public ClientboundMapItemDataPacket() {
    }
    
    public ClientboundMapItemDataPacket(final int integer1, final byte byte2, final boolean boolean3, final boolean boolean4, final Collection<MapDecoration> collection, final byte[] arr, final int integer7, final int integer8, final int integer9, final int integer10) {
        this.mapId = integer1;
        this.scale = byte2;
        this.trackingPosition = boolean3;
        this.locked = boolean4;
        this.decorations = collection.<MapDecoration>toArray(new MapDecoration[collection.size()]);
        this.startX = integer7;
        this.startY = integer8;
        this.width = integer9;
        this.height = integer10;
        this.mapColors = new byte[integer9 * integer10];
        for (int integer11 = 0; integer11 < integer9; ++integer11) {
            for (int integer12 = 0; integer12 < integer10; ++integer12) {
                this.mapColors[integer11 + integer12 * integer9] = arr[integer7 + integer11 + (integer8 + integer12) * 128];
            }
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.mapId = kv.readVarInt();
        this.scale = kv.readByte();
        this.trackingPosition = kv.readBoolean();
        this.locked = kv.readBoolean();
        this.decorations = new MapDecoration[kv.readVarInt()];
        for (int integer3 = 0; integer3 < this.decorations.length; ++integer3) {
            final MapDecoration.Type a4 = kv.<MapDecoration.Type>readEnum(MapDecoration.Type.class);
            this.decorations[integer3] = new MapDecoration(a4, kv.readByte(), kv.readByte(), (byte)(kv.readByte() & 0xF), kv.readBoolean() ? kv.readComponent() : null);
        }
        this.width = kv.readUnsignedByte();
        if (this.width > 0) {
            this.height = kv.readUnsignedByte();
            this.startX = kv.readUnsignedByte();
            this.startY = kv.readUnsignedByte();
            this.mapColors = kv.readByteArray();
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.mapId);
        kv.writeByte(this.scale);
        kv.writeBoolean(this.trackingPosition);
        kv.writeBoolean(this.locked);
        kv.writeVarInt(this.decorations.length);
        for (final MapDecoration crd6 : this.decorations) {
            kv.writeEnum(crd6.getType());
            kv.writeByte(crd6.getX());
            kv.writeByte(crd6.getY());
            kv.writeByte(crd6.getRot() & 0xF);
            if (crd6.getName() != null) {
                kv.writeBoolean(true);
                kv.writeComponent(crd6.getName());
            }
            else {
                kv.writeBoolean(false);
            }
        }
        kv.writeByte(this.width);
        if (this.width > 0) {
            kv.writeByte(this.height);
            kv.writeByte(this.startX);
            kv.writeByte(this.startY);
            kv.writeByteArray(this.mapColors);
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleMapItemData(this);
    }
    
    public int getMapId() {
        return this.mapId;
    }
    
    public void applyToMap(final MapItemSavedData crg) {
        crg.scale = this.scale;
        crg.trackingPosition = this.trackingPosition;
        crg.locked = this.locked;
        crg.decorations.clear();
        for (int integer3 = 0; integer3 < this.decorations.length; ++integer3) {
            final MapDecoration crd4 = this.decorations[integer3];
            crg.decorations.put("icon-" + integer3, crd4);
        }
        for (int integer3 = 0; integer3 < this.width; ++integer3) {
            for (int integer4 = 0; integer4 < this.height; ++integer4) {
                crg.colors[this.startX + integer3 + (this.startY + integer4) * 128] = this.mapColors[integer3 + integer4 * this.width];
            }
        }
    }
}
