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

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

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import com.hypixel.hytale.codec.schema.SchemaConvertable;
import com.hypixel.hytale.codec.schema.config.ObjectSchema;
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 java.util.Iterator;
import org.bson.BsonDocument;
import com.hypixel.hytale.codec.exception.CodecException;
import java.util.Collections;
import com.hypixel.hytale.codec.ExtraInfo;
import javax.annotation.Nonnull;
import org.bson.BsonValue;
import java.util.function.Supplier;
import com.hypixel.hytale.codec.WrappedCodec;
import com.hypixel.hytale.codec.Codec;
import java.util.Map;

public class MapCodec<V, M extends Map<String, V>> implements Codec<Map<String, V>>, WrappedCodec<V>
{
    public static final MapCodec<String, Map<String, String>> STRING_HASH_MAP_CODEC;
    private final Codec<V> codec;
    private final Supplier<M> supplier;
    private final boolean unmodifiable;
    
    public MapCodec(final Codec<V> codec, final Supplier<M> supplier) {
        this(codec, supplier, true);
    }
    
    public MapCodec(final Codec<V> codec, final Supplier<M> supplier, final boolean unmodifiable) {
        this.codec = codec;
        this.supplier = supplier;
        this.unmodifiable = unmodifiable;
    }
    
    @Override
    public Codec<V> getChildCodec() {
        return this.codec;
    }
    
    @Override
    public Map<String, V> decode(@Nonnull final BsonValue bsonValue, @Nonnull final ExtraInfo extraInfo) {
        final BsonDocument bsonDocument = bsonValue.asDocument();
        if (bsonDocument.isEmpty()) {
            return this.unmodifiable ? Collections.emptyMap() : ((M)this.supplier.get());
        }
        Map<String, V> map = this.supplier.get();
        for (final Map.Entry<String, BsonValue> entry : bsonDocument.entrySet()) {
            final String key = entry.getKey();
            final BsonValue value = entry.getValue();
            extraInfo.pushKey(key);
            try {
                map.put(key, this.codec.decode(value, extraInfo));
            }
            catch (final Exception e) {
                throw new CodecException("Failed to decode", value, extraInfo, e);
            }
            finally {
                extraInfo.popKey();
            }
        }
        if (this.unmodifiable) {
            map = Collections.unmodifiableMap((Map<? extends String, ? extends V>)map);
        }
        return map;
    }
    
    @Nonnull
    @Override
    public BsonValue encode(@Nonnull final Map<String, V> map, final ExtraInfo extraInfo) {
        final BsonDocument bsonDocument = new BsonDocument();
        for (final Map.Entry<String, V> entry : map.entrySet()) {
            final BsonValue value = this.codec.encode(entry.getValue(), extraInfo);
            if (value != null && !value.isNull() && (!value.isDocument() || !value.asDocument().isEmpty()) && (!value.isArray() || !value.asArray().isEmpty())) {
                bsonDocument.put(entry.getKey(), value);
            }
        }
        return bsonDocument;
    }
    
    @Override
    public Map<String, V> decodeJson(@Nonnull final RawJsonReader reader, @Nonnull final ExtraInfo extraInfo) throws IOException {
        reader.expect('{');
        reader.consumeWhiteSpace();
        if (reader.tryConsume('}')) {
            return this.unmodifiable ? Collections.emptyMap() : ((M)this.supplier.get());
        }
        Map<String, V> map = this.supplier.get();
        while (true) {
            final String key = reader.readString();
            reader.consumeWhiteSpace();
            reader.expect(':');
            reader.consumeWhiteSpace();
            extraInfo.pushKey(key, reader);
            try {
                map.put(key, this.codec.decodeJson(reader, extraInfo));
            }
            catch (final Exception e) {
                throw new CodecException("Failed to decode", reader, extraInfo, e);
            }
            finally {
                extraInfo.popKey();
            }
            reader.consumeWhiteSpace();
            if (reader.tryConsumeOrExpect('}', ',')) {
                break;
            }
            reader.consumeWhiteSpace();
        }
        if (this.unmodifiable) {
            map = Collections.unmodifiableMap((Map<? extends String, ? extends V>)map);
        }
        return map;
    }
    
    @Nonnull
    @Override
    public Schema toSchema(@Nonnull final SchemaContext context) {
        final ObjectSchema schema = new ObjectSchema();
        schema.setTitle("Map");
        final Schema childSchema = context.refDefinition(this.codec);
        schema.setAdditionalProperties(childSchema);
        return schema;
    }
    
    static {
        STRING_HASH_MAP_CODEC = new MapCodec<String, Map<String, String>>(Codec.STRING, (Supplier<Map<String, String>>)Object2ObjectOpenHashMap::new);
    }
}
