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

package com.hypixel.hytale.server.core.modules.blockhealth;

import javax.annotation.Nonnull;
import io.netty.buffer.ByteBuf;
import com.hypixel.hytale.math.util.MathUtil;
import java.time.Instant;

public class BlockHealth implements Cloneable
{
    public static final BlockHealth NO_DAMAGE_INSTANCE;
    private float health;
    private Instant lastDamageGameTime;
    
    public BlockHealth() {
        this(1.0f, Instant.MIN);
    }
    
    public BlockHealth(final float health, final Instant lastDamageGameTime) {
        this.health = health;
        this.lastDamageGameTime = lastDamageGameTime;
    }
    
    public float getHealth() {
        return this.health;
    }
    
    public void setHealth(final float health) {
        this.health = health;
    }
    
    public Instant getLastDamageGameTime() {
        return this.lastDamageGameTime;
    }
    
    public void setLastDamageGameTime(final Instant lastDamageGameTime) {
        this.lastDamageGameTime = lastDamageGameTime;
    }
    
    public boolean isDestroyed() {
        return MathUtil.closeToZero(this.health) || this.health < 0.0f;
    }
    
    public boolean isFullHealth() {
        return this.health >= 1.0;
    }
    
    public void deserialize(@Nonnull final ByteBuf buf, final byte version) {
        this.health = buf.readFloat();
        this.lastDamageGameTime = Instant.ofEpochMilli(buf.readLong());
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        buf.writeFloat(this.health);
        buf.writeLong(this.lastDamageGameTime.toEpochMilli());
    }
    
    @Nonnull
    @Override
    protected BlockHealth clone() {
        return new BlockHealth(this.health, this.lastDamageGameTime);
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "BlockHealth{health=" + this.health + ", lastDamageGameTime=" + String.valueOf(this.lastDamageGameTime);
    }
    
    static {
        NO_DAMAGE_INSTANCE = new BlockHealth(Instant.MIN) {
            @Override
            public void setHealth(final float health) {
                throw new UnsupportedOperationException("NO_DAMAGE_INSTANCE is immutable!");
            }
            
            @Override
            public void setLastDamageGameTime(final Instant lastDamageGameTime) {
                throw new UnsupportedOperationException("NO_DAMAGE_INSTANCE is immutable!");
            }
        };
    }
}
