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

package com.hypixel.hytale.codec.codecs.simple;

import javax.annotation.Nullable;
import com.hypixel.hytale.codec.schema.config.IntegerSchema;
import com.hypixel.hytale.codec.schema.config.Schema;
import com.hypixel.hytale.codec.schema.SchemaContext;
import java.io.IOException;
import com.hypixel.hytale.codec.util.RawJsonReader;
import org.bson.BsonInt64;
import com.hypixel.hytale.codec.ExtraInfo;
import javax.annotation.Nonnull;
import org.bson.BsonValue;
import com.hypixel.hytale.codec.PrimitiveCodec;
import com.hypixel.hytale.codec.RawJsonCodec;
import com.hypixel.hytale.codec.Codec;

public class LongCodec implements Codec<Long>, RawJsonCodec<Long>, PrimitiveCodec
{
    @Nonnull
    @Override
    public Long decode(@Nonnull final BsonValue bsonValue, final ExtraInfo extraInfo) {
        final long longValue = bsonValue.asNumber().longValue();
        if (longValue != bsonValue.asNumber().doubleValue()) {
            throw new IllegalArgumentException("Expected an long but got a decimal!");
        }
        return longValue;
    }
    
    @Nonnull
    @Override
    public BsonValue encode(final Long t, final ExtraInfo extraInfo) {
        return new BsonInt64(t);
    }
    
    @Nonnull
    @Override
    public Long decodeJson(@Nonnull final RawJsonReader reader, final ExtraInfo extraInfo) throws IOException {
        return reader.readLongValue();
    }
    
    @Nonnull
    @Override
    public Schema toSchema(@Nonnull final SchemaContext context) {
        return new IntegerSchema();
    }
    
    @Nonnull
    @Override
    public Schema toSchema(@Nonnull final SchemaContext context, @Nullable final Long def) {
        final IntegerSchema s = new IntegerSchema();
        if (def != null) {
            s.setDefault(def.intValue());
        }
        return s;
    }
}
