// 
// 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 java.util.Iterator;
import java.util.HashMap;
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;
import java.util.Map;
import com.hypixel.hytale.protocol.Packet;

public class UpdateWorldMapSettings implements Packet
{
    public static final int PACKET_ID = 240;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 16;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 16;
    public static final int MAX_SIZE = 1677721600;
    public boolean enabled;
    @Nullable
    public Map<Short, BiomeData> biomeDataMap;
    public boolean allowTeleportToCoordinates;
    public boolean allowTeleportToMarkers;
    public float defaultScale;
    public float minScale;
    public float maxScale;
    
    @Override
    public int getId() {
        return 240;
    }
    
    public UpdateWorldMapSettings() {
        this.enabled = true;
        this.defaultScale = 32.0f;
        this.minScale = 2.0f;
        this.maxScale = 256.0f;
    }
    
    public UpdateWorldMapSettings(final boolean enabled, @Nullable final Map<Short, BiomeData> biomeDataMap, final boolean allowTeleportToCoordinates, final boolean allowTeleportToMarkers, final float defaultScale, final float minScale, final float maxScale) {
        this.enabled = true;
        this.defaultScale = 32.0f;
        this.minScale = 2.0f;
        this.maxScale = 256.0f;
        this.enabled = enabled;
        this.biomeDataMap = biomeDataMap;
        this.allowTeleportToCoordinates = allowTeleportToCoordinates;
        this.allowTeleportToMarkers = allowTeleportToMarkers;
        this.defaultScale = defaultScale;
        this.minScale = minScale;
        this.maxScale = maxScale;
    }
    
    public UpdateWorldMapSettings(@Nonnull final UpdateWorldMapSettings other) {
        this.enabled = true;
        this.defaultScale = 32.0f;
        this.minScale = 2.0f;
        this.maxScale = 256.0f;
        this.enabled = other.enabled;
        this.biomeDataMap = other.biomeDataMap;
        this.allowTeleportToCoordinates = other.allowTeleportToCoordinates;
        this.allowTeleportToMarkers = other.allowTeleportToMarkers;
        this.defaultScale = other.defaultScale;
        this.minScale = other.minScale;
        this.maxScale = other.maxScale;
    }
    
    @Nonnull
    public static UpdateWorldMapSettings deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateWorldMapSettings obj = new UpdateWorldMapSettings();
        final byte nullBits = buf.getByte(offset);
        obj.enabled = (buf.getByte(offset + 1) != 0);
        obj.allowTeleportToCoordinates = (buf.getByte(offset + 2) != 0);
        obj.allowTeleportToMarkers = (buf.getByte(offset + 3) != 0);
        obj.defaultScale = buf.getFloatLE(offset + 4);
        obj.minScale = buf.getFloatLE(offset + 8);
        obj.maxScale = buf.getFloatLE(offset + 12);
        int pos = offset + 16;
        if ((nullBits & 0x1) != 0x0) {
            final int biomeDataMapCount = VarInt.peek(buf, pos);
            if (biomeDataMapCount < 0) {
                throw ProtocolException.negativeLength("BiomeDataMap", biomeDataMapCount);
            }
            if (biomeDataMapCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("BiomeDataMap", biomeDataMapCount, 4096000);
            }
            pos += VarInt.size(biomeDataMapCount);
            obj.biomeDataMap = new HashMap<Short, BiomeData>(biomeDataMapCount);
            for (int i = 0; i < biomeDataMapCount; ++i) {
                final short key = buf.getShortLE(pos);
                pos += 2;
                final BiomeData val = BiomeData.deserialize(buf, pos);
                pos += BiomeData.computeBytesConsumed(buf, pos);
                if (obj.biomeDataMap.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("biomeDataMap", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 16;
        if ((nullBits & 0x1) != 0x0) {
            final int dictLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos);
            for (int i = 0; i < dictLen; ++i) {
                pos += 2;
                pos += BiomeData.computeBytesConsumed(buf, pos);
            }
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.biomeDataMap != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.enabled ? 1 : 0);
        buf.writeByte(this.allowTeleportToCoordinates ? 1 : 0);
        buf.writeByte(this.allowTeleportToMarkers ? 1 : 0);
        buf.writeFloatLE(this.defaultScale);
        buf.writeFloatLE(this.minScale);
        buf.writeFloatLE(this.maxScale);
        if (this.biomeDataMap != null) {
            if (this.biomeDataMap.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("BiomeDataMap", this.biomeDataMap.size(), 4096000);
            }
            VarInt.write(buf, this.biomeDataMap.size());
            for (final Map.Entry<Short, BiomeData> e : this.biomeDataMap.entrySet()) {
                buf.writeShortLE(e.getKey());
                e.getValue().serialize(buf);
            }
        }
    }
    
    @Override
    public int computeSize() {
        int size = 16;
        if (this.biomeDataMap != null) {
            int biomeDataMapSize = 0;
            for (final Map.Entry<Short, BiomeData> kvp : this.biomeDataMap.entrySet()) {
                biomeDataMapSize += 2 + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.biomeDataMap.size()) + biomeDataMapSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 16) {
            return ValidationResult.error("Buffer too small: expected at least 16 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 16;
        if ((nullBits & 0x1) != 0x0) {
            final int biomeDataMapCount = VarInt.peek(buffer, pos);
            if (biomeDataMapCount < 0) {
                return ValidationResult.error("Invalid dictionary count for BiomeDataMap");
            }
            if (biomeDataMapCount > 4096000) {
                return ValidationResult.error("BiomeDataMap exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < biomeDataMapCount; ++i) {
                pos += 2;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += BiomeData.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public UpdateWorldMapSettings clone() {
        final UpdateWorldMapSettings copy = new UpdateWorldMapSettings();
        copy.enabled = this.enabled;
        if (this.biomeDataMap != null) {
            final Map<Short, BiomeData> m = new HashMap<Short, BiomeData>();
            for (final Map.Entry<Short, BiomeData> e : this.biomeDataMap.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.biomeDataMap = m;
        }
        copy.allowTeleportToCoordinates = this.allowTeleportToCoordinates;
        copy.allowTeleportToMarkers = this.allowTeleportToMarkers;
        copy.defaultScale = this.defaultScale;
        copy.minScale = this.minScale;
        copy.maxScale = this.maxScale;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateWorldMapSettings other) {
            return this.enabled == other.enabled && Objects.equals(this.biomeDataMap, other.biomeDataMap) && this.allowTeleportToCoordinates == other.allowTeleportToCoordinates && this.allowTeleportToMarkers == other.allowTeleportToMarkers && this.defaultScale == other.defaultScale && this.minScale == other.minScale && this.maxScale == other.maxScale;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.enabled, this.biomeDataMap, this.allowTeleportToCoordinates, this.allowTeleportToMarkers, this.defaultScale, this.minScale, this.maxScale);
    }
}
