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

package org.bson.codecs;

import java.math.BigDecimal;
import org.bson.BsonType;
import org.bson.BsonInvalidOperationException;
import org.bson.types.Decimal128;
import org.bson.BsonReader;

final class NumberCodecHelper
{
    static int decodeInt(final BsonReader reader) {
        final BsonType bsonType = reader.getCurrentBsonType();
        int intValue = 0;
        switch (bsonType) {
            case INT32: {
                intValue = reader.readInt32();
                break;
            }
            case INT64: {
                final long longValue = reader.readInt64();
                intValue = (int)longValue;
                if (longValue != intValue) {
                    throw invalidConversion(Integer.class, longValue);
                }
                break;
            }
            case DOUBLE: {
                final double doubleValue = reader.readDouble();
                intValue = (int)doubleValue;
                if (doubleValue != intValue) {
                    throw invalidConversion(Integer.class, doubleValue);
                }
                break;
            }
            case DECIMAL128: {
                final Decimal128 decimal128 = reader.readDecimal128();
                intValue = decimal128.intValue();
                if (!decimal128.equals(new Decimal128(intValue))) {
                    throw invalidConversion(Integer.class, decimal128);
                }
                break;
            }
            default: {
                throw new BsonInvalidOperationException(String.format("Invalid numeric type, found: %s", bsonType));
            }
        }
        return intValue;
    }
    
    static long decodeLong(final BsonReader reader) {
        final BsonType bsonType = reader.getCurrentBsonType();
        long longValue = 0L;
        switch (bsonType) {
            case INT32: {
                longValue = reader.readInt32();
                break;
            }
            case INT64: {
                longValue = reader.readInt64();
                break;
            }
            case DOUBLE: {
                final double doubleValue = reader.readDouble();
                longValue = (long)doubleValue;
                if (doubleValue != longValue) {
                    throw invalidConversion(Long.class, doubleValue);
                }
                break;
            }
            case DECIMAL128: {
                final Decimal128 decimal128 = reader.readDecimal128();
                longValue = decimal128.longValue();
                if (!decimal128.equals(new Decimal128(longValue))) {
                    throw invalidConversion(Long.class, decimal128);
                }
                break;
            }
            default: {
                throw new BsonInvalidOperationException(String.format("Invalid numeric type, found: %s", bsonType));
            }
        }
        return longValue;
    }
    
    static double decodeDouble(final BsonReader reader) {
        final BsonType bsonType = reader.getCurrentBsonType();
        switch (bsonType) {
            case INT32: {
                final double doubleValue = reader.readInt32();
                return doubleValue;
            }
            case INT64: {
                final long longValue = reader.readInt64();
                final double doubleValue = (double)longValue;
                if (longValue != (long)doubleValue) {
                    throw invalidConversion(Double.class, longValue);
                }
                return doubleValue;
            }
            case DOUBLE: {
                final double doubleValue = reader.readDouble();
                return doubleValue;
            }
            case DECIMAL128: {
                final Decimal128 decimal128 = reader.readDecimal128();
                try {
                    final double doubleValue = decimal128.doubleValue();
                    if (!decimal128.equals(new Decimal128(new BigDecimal(doubleValue)))) {
                        throw invalidConversion(Double.class, decimal128);
                    }
                    return doubleValue;
                }
                catch (final NumberFormatException e) {
                    throw invalidConversion(Double.class, decimal128);
                }
                break;
            }
        }
        throw new BsonInvalidOperationException(String.format("Invalid numeric type, found: %s", bsonType));
    }
    
    private static <T extends Number> BsonInvalidOperationException invalidConversion(final Class<T> clazz, final Number value) {
        return new BsonInvalidOperationException(String.format("Could not convert `%s` to a %s without losing precision", value, clazz));
    }
    
    private NumberCodecHelper() {
    }
}
