package leigh.eo.mongo;

import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;

/**
 * Implementation of a BSON {@link Codec} for the EO class.
 * <p>
 * NOTE THIS IS A PARTIAL IMPLEMENTATION, DO NOT USE.
 *
 * @author Alex Leigh
 */
@Deprecated
public class EOCodec implements Codec<EO> {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(EOCodec.class);

    @Override
    public EO decode(BsonReader reader, DecoderContext decoderContext) {
        EO eo = new EO();

        reader.readStartDocument();

        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            String fieldName = reader.readName();
            switch (fieldName) {
                case "_id":
                    reader.readObjectId();
                    break;
                case "type":
                    eo.setApiType(reader.readString());
                    break;
            }
        }

        reader.readEndDocument();

        return eo;
    }

    @Override
    public void encode(BsonWriter writer, EO value, EncoderContext encoderContext) {
        writer.writeStartDocument();

        String apiType = value.getApiType();
        if (apiType != null) {
            writer.writeName("type");
            writer.writeString(value.getApiType());
        }

        writer.writeEndDocument();
    }

    @Override
    public Class<EO> getEncoderClass() {
        return EO.class;
    }
}
