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 mg) throws IOException {
        this.mapId = mg.readVarInt();
        this.scale = mg.readByte();
        this.trackingPosition = mg.readBoolean();
        this.locked = mg.readBoolean();
        this.decorations = new MapDecoration[mg.readVarInt()];
        for (int integer3 = 0; integer3 < this.decorations.length; ++integer3) {
            final MapDecoration.Type a4 = mg.<MapDecoration.Type>readEnum(MapDecoration.Type.class);
            this.decorations[integer3] = new MapDecoration(a4, mg.readByte(), mg.readByte(), (byte)(mg.readByte() & 0xF), mg.readBoolean() ? mg.readComponent() : null);
        }
        this.width = mg.readUnsignedByte();
        if (this.width > 0) {
            this.height = mg.readUnsignedByte();
            this.startX = mg.readUnsignedByte();
            this.startY = mg.readUnsignedByte();
            this.mapColors = mg.readByteArray();
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.mapId);
        mg.writeByte(this.scale);
        mg.writeBoolean(this.trackingPosition);
        mg.writeBoolean(this.locked);
        mg.writeVarInt(this.decorations.length);
        for (final MapDecoration czs6 : this.decorations) {
            mg.writeEnum(czs6.getType());
            mg.writeByte(czs6.getX());
            mg.writeByte(czs6.getY());
            mg.writeByte(czs6.getRot() & 0xF);
            if (czs6.getName() != null) {
                mg.writeBoolean(true);
                mg.writeComponent(czs6.getName());
            }
            else {
                mg.writeBoolean(false);
            }
        }
        mg.writeByte(this.width);
        if (this.width > 0) {
            mg.writeByte(this.height);
            mg.writeByte(this.startX);
            mg.writeByte(this.startY);
            mg.writeByteArray(this.mapColors);
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.handleMapItemData(this);
    }
    
    public int getMapId() {
        return this.mapId;
    }
    
    public void applyToMap(final MapItemSavedData czv) {
        czv.scale = this.scale;
        czv.trackingPosition = this.trackingPosition;
        czv.locked = this.locked;
        czv.decorations.clear();
        for (int integer3 = 0; integer3 < this.decorations.length; ++integer3) {
            final MapDecoration czs4 = this.decorations[integer3];
            czv.decorations.put("icon-" + integer3, czs4);
        }
        for (int integer3 = 0; integer3 < this.width; ++integer3) {
            for (int integer4 = 0; integer4 < this.height; ++integer4) {
                czv.colors[this.startX + integer3 + (this.startY + integer4) * 128] = this.mapColors[integer3 + integer4 * this.width];
            }
        }
    }
}
