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

package com.hypixel.hytale.protocol;

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

public class Edge
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 9;
    @Nullable
    public ColorAlpha color;
    public float width;
    
    public Edge() {
    }
    
    public Edge(@Nullable final ColorAlpha color, final float width) {
        this.color = color;
        this.width = width;
    }
    
    public Edge(@Nonnull final Edge other) {
        this.color = other.color;
        this.width = other.width;
    }
    
    @Nonnull
    public static Edge deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Edge obj = new Edge();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.color = ColorAlpha.deserialize(buf, offset + 1);
        }
        obj.width = buf.getFloatLE(offset + 5);
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 9;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.color != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        if (this.color != null) {
            this.color.serialize(buf);
        }
        else {
            buf.writeZero(4);
        }
        buf.writeFloatLE(this.width);
    }
    
    public int computeSize() {
        return 9;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        return ValidationResult.OK;
    }
    
    public Edge clone() {
        final Edge copy = new Edge();
        copy.color = ((this.color != null) ? this.color.clone() : null);
        copy.width = this.width;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Edge other) {
            return Objects.equals(this.color, other.color) && this.width == other.width;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.color, this.width);
    }
}
