// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.protocol.packets.worldmap;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.PacketIO;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class BiomeData
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 17;
    public static final int MAX_SIZE = 32768027;
    public int zoneId;
    @Nullable
    public String zoneName;
    @Nullable
    public String biomeName;
    public int biomeColor;
    
    public BiomeData() {
    }
    
    public BiomeData(final int zoneId, @Nullable final String zoneName, @Nullable final String biomeName, final int biomeColor) {
        this.zoneId = zoneId;
        this.zoneName = zoneName;
        this.biomeName = biomeName;
        this.biomeColor = biomeColor;
    }
    
    public BiomeData(@Nonnull final BiomeData other) {
        this.zoneId = other.zoneId;
        this.zoneName = other.zoneName;
        this.biomeName = other.biomeName;
        this.biomeColor = other.biomeColor;
    }
    
    @Nonnull
    public static BiomeData deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BiomeData obj = new BiomeData();
        final byte nullBits = buf.getByte(offset);
        obj.zoneId = buf.getIntLE(offset + 1);
        obj.biomeColor = buf.getIntLE(offset + 5);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 17 + buf.getIntLE(offset + 9);
            final int zoneNameLen = VarInt.peek(buf, varPos0);
            if (zoneNameLen < 0) {
                throw ProtocolException.negativeLength("ZoneName", zoneNameLen);
            }
            if (zoneNameLen > 4096000) {
                throw ProtocolException.stringTooLong("ZoneName", zoneNameLen, 4096000);
            }
            obj.zoneName = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 17 + buf.getIntLE(offset + 13);
            final int biomeNameLen = VarInt.peek(buf, varPos2);
            if (biomeNameLen < 0) {
                throw ProtocolException.negativeLength("BiomeName", biomeNameLen);
            }
            if (biomeNameLen > 4096000) {
                throw ProtocolException.stringTooLong("BiomeName", biomeNameLen, 4096000);
            }
            obj.biomeName = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 17;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 9);
            int pos0 = offset + 17 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 13);
            int pos2 = offset + 17 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.zoneName != null) {
            nullBits |= 0x1;
        }
        if (this.biomeName != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.zoneId);
        buf.writeIntLE(this.biomeColor);
        final int zoneNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int biomeNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.zoneName != null) {
            buf.setIntLE(zoneNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.zoneName, 4096000);
        }
        else {
            buf.setIntLE(zoneNameOffsetSlot, -1);
        }
        if (this.biomeName != null) {
            buf.setIntLE(biomeNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.biomeName, 4096000);
        }
        else {
            buf.setIntLE(biomeNameOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 17;
        if (this.zoneName != null) {
            size += PacketIO.stringSize(this.zoneName);
        }
        if (this.biomeName != null) {
            size += PacketIO.stringSize(this.biomeName);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 17) {
            return ValidationResult.error("Buffer too small: expected at least 17 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int zoneNameOffset = buffer.getIntLE(offset + 9);
            if (zoneNameOffset < 0) {
                return ValidationResult.error("Invalid offset for ZoneName");
            }
            int pos = offset + 17 + zoneNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ZoneName");
            }
            final int zoneNameLen = VarInt.peek(buffer, pos);
            if (zoneNameLen < 0) {
                return ValidationResult.error("Invalid string length for ZoneName");
            }
            if (zoneNameLen > 4096000) {
                return ValidationResult.error("ZoneName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += zoneNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading ZoneName");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int biomeNameOffset = buffer.getIntLE(offset + 13);
            if (biomeNameOffset < 0) {
                return ValidationResult.error("Invalid offset for BiomeName");
            }
            int pos = offset + 17 + biomeNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for BiomeName");
            }
            final int biomeNameLen = VarInt.peek(buffer, pos);
            if (biomeNameLen < 0) {
                return ValidationResult.error("Invalid string length for BiomeName");
            }
            if (biomeNameLen > 4096000) {
                return ValidationResult.error("BiomeName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += biomeNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading BiomeName");
            }
        }
        return ValidationResult.OK;
    }
    
    public BiomeData clone() {
        final BiomeData copy = new BiomeData();
        copy.zoneId = this.zoneId;
        copy.zoneName = this.zoneName;
        copy.biomeName = this.biomeName;
        copy.biomeColor = this.biomeColor;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BiomeData other) {
            return this.zoneId == other.zoneId && Objects.equals(this.zoneName, other.zoneName) && Objects.equals(this.biomeName, other.biomeName) && this.biomeColor == other.biomeColor;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.zoneId, this.zoneName, this.biomeName, this.biomeColor);
    }
}
