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

package com.hypixel.hytale.protocol;

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 java.util.Map;
import javax.annotation.Nullable;

public class ProjectileConfig
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 163;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 171;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public PhysicsConfig physicsConfig;
    @Nullable
    public Model model;
    public double launchForce;
    @Nullable
    public Vector3f spawnOffset;
    @Nullable
    public Direction rotationOffset;
    @Nullable
    public Map<InteractionType, Integer> interactions;
    public int launchLocalSoundEventIndex;
    public int projectileSoundEventIndex;
    
    public ProjectileConfig() {
    }
    
    public ProjectileConfig(@Nullable final PhysicsConfig physicsConfig, @Nullable final Model model, final double launchForce, @Nullable final Vector3f spawnOffset, @Nullable final Direction rotationOffset, @Nullable final Map<InteractionType, Integer> interactions, final int launchLocalSoundEventIndex, final int projectileSoundEventIndex) {
        this.physicsConfig = physicsConfig;
        this.model = model;
        this.launchForce = launchForce;
        this.spawnOffset = spawnOffset;
        this.rotationOffset = rotationOffset;
        this.interactions = interactions;
        this.launchLocalSoundEventIndex = launchLocalSoundEventIndex;
        this.projectileSoundEventIndex = projectileSoundEventIndex;
    }
    
    public ProjectileConfig(@Nonnull final ProjectileConfig other) {
        this.physicsConfig = other.physicsConfig;
        this.model = other.model;
        this.launchForce = other.launchForce;
        this.spawnOffset = other.spawnOffset;
        this.rotationOffset = other.rotationOffset;
        this.interactions = other.interactions;
        this.launchLocalSoundEventIndex = other.launchLocalSoundEventIndex;
        this.projectileSoundEventIndex = other.projectileSoundEventIndex;
    }
    
    @Nonnull
    public static ProjectileConfig deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ProjectileConfig obj = new ProjectileConfig();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.physicsConfig = PhysicsConfig.deserialize(buf, offset + 1);
        }
        obj.launchForce = buf.getDoubleLE(offset + 123);
        if ((nullBits & 0x2) != 0x0) {
            obj.spawnOffset = Vector3f.deserialize(buf, offset + 131);
        }
        if ((nullBits & 0x4) != 0x0) {
            obj.rotationOffset = Direction.deserialize(buf, offset + 143);
        }
        obj.launchLocalSoundEventIndex = buf.getIntLE(offset + 155);
        obj.projectileSoundEventIndex = buf.getIntLE(offset + 159);
        if ((nullBits & 0x8) != 0x0) {
            final int varPos0 = offset + 171 + buf.getIntLE(offset + 163);
            obj.model = Model.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos2 = offset + 171 + buf.getIntLE(offset + 167);
            final int interactionsCount = VarInt.peek(buf, varPos2);
            if (interactionsCount < 0) {
                throw ProtocolException.negativeLength("Interactions", interactionsCount);
            }
            if (interactionsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Interactions", interactionsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.interactions = new HashMap<InteractionType, Integer>(interactionsCount);
            int dictPos = varPos2 + varIntLen;
            for (int i = 0; i < interactionsCount; ++i) {
                final InteractionType key = InteractionType.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final int val = buf.getIntLE(dictPos);
                dictPos += 4;
                if (obj.interactions.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("interactions", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 171;
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 163);
            int pos0 = offset + 171 + fieldOffset0;
            pos0 += Model.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 167);
            int pos2 = offset + 171 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                ++pos2;
                pos2 += 4;
            }
            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.physicsConfig != null) {
            nullBits |= 0x1;
        }
        if (this.spawnOffset != null) {
            nullBits |= 0x2;
        }
        if (this.rotationOffset != null) {
            nullBits |= 0x4;
        }
        if (this.model != null) {
            nullBits |= 0x8;
        }
        if (this.interactions != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        if (this.physicsConfig != null) {
            this.physicsConfig.serialize(buf);
        }
        else {
            buf.writeZero(122);
        }
        buf.writeDoubleLE(this.launchForce);
        if (this.spawnOffset != null) {
            this.spawnOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.rotationOffset != null) {
            this.rotationOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        buf.writeIntLE(this.launchLocalSoundEventIndex);
        buf.writeIntLE(this.projectileSoundEventIndex);
        final int modelOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int interactionsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.model != null) {
            buf.setIntLE(modelOffsetSlot, buf.writerIndex() - varBlockStart);
            this.model.serialize(buf);
        }
        else {
            buf.setIntLE(modelOffsetSlot, -1);
        }
        if (this.interactions != null) {
            buf.setIntLE(interactionsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.interactions.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Interactions", this.interactions.size(), 4096000);
            }
            VarInt.write(buf, this.interactions.size());
            for (final Map.Entry<InteractionType, Integer> e : this.interactions.entrySet()) {
                buf.writeByte(e.getKey().getValue());
                buf.writeIntLE(e.getValue());
            }
        }
        else {
            buf.setIntLE(interactionsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 171;
        if (this.model != null) {
            size += this.model.computeSize();
        }
        if (this.interactions != null) {
            size += VarInt.size(this.interactions.size()) + this.interactions.size() * 5;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 171) {
            return ValidationResult.error("Buffer too small: expected at least 171 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x8) != 0x0) {
            final int modelOffset = buffer.getIntLE(offset + 163);
            if (modelOffset < 0) {
                return ValidationResult.error("Invalid offset for Model");
            }
            int pos = offset + 171 + modelOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Model");
            }
            final ValidationResult modelResult = Model.validateStructure(buffer, pos);
            if (!modelResult.isValid()) {
                return ValidationResult.error("Invalid Model: " + modelResult.error());
            }
            pos += Model.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int interactionsOffset = buffer.getIntLE(offset + 167);
            if (interactionsOffset < 0) {
                return ValidationResult.error("Invalid offset for Interactions");
            }
            int pos = offset + 171 + interactionsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Interactions");
            }
            final int interactionsCount = VarInt.peek(buffer, pos);
            if (interactionsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Interactions");
            }
            if (interactionsCount > 4096000) {
                return ValidationResult.error("Interactions exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < interactionsCount; ++i) {
                ++pos;
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading value");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public ProjectileConfig clone() {
        final ProjectileConfig copy = new ProjectileConfig();
        copy.physicsConfig = ((this.physicsConfig != null) ? this.physicsConfig.clone() : null);
        copy.model = ((this.model != null) ? this.model.clone() : null);
        copy.launchForce = this.launchForce;
        copy.spawnOffset = ((this.spawnOffset != null) ? this.spawnOffset.clone() : null);
        copy.rotationOffset = ((this.rotationOffset != null) ? this.rotationOffset.clone() : null);
        copy.interactions = ((this.interactions != null) ? new HashMap<InteractionType, Integer>(this.interactions) : null);
        copy.launchLocalSoundEventIndex = this.launchLocalSoundEventIndex;
        copy.projectileSoundEventIndex = this.projectileSoundEventIndex;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ProjectileConfig other) {
            return Objects.equals(this.physicsConfig, other.physicsConfig) && Objects.equals(this.model, other.model) && this.launchForce == other.launchForce && Objects.equals(this.spawnOffset, other.spawnOffset) && Objects.equals(this.rotationOffset, other.rotationOffset) && Objects.equals(this.interactions, other.interactions) && this.launchLocalSoundEventIndex == other.launchLocalSoundEventIndex && this.projectileSoundEventIndex == other.projectileSoundEventIndex;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.physicsConfig, this.model, this.launchForce, this.spawnOffset, this.rotationOffset, this.interactions, this.launchLocalSoundEventIndex, this.projectileSoundEventIndex);
    }
}
