package org.ladanse.dto.databind;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.ladanse.utils.SignUpType;

import java.io.IOException;

public class SignUpTypeDeserializer extends JsonDeserializer<SignUpType>
{
    @Override
    public SignUpType deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException
    {
        JsonNode node = jp.getCodec()
                          .readTree(jp);

        String strSignUpType = node.asText();

        if (SignUpType.WILL_COME.getType()
                                .equals(strSignUpType))
        {
            return SignUpType.WILL_COME;
        }
        else if (SignUpType.MIGHT_COME.getType()
                                      .equals(strSignUpType))
        {
            return SignUpType.MIGHT_COME;
        }
        else if (SignUpType.ABSENCE.getType()
                                   .equals(strSignUpType))
        {
            return SignUpType.ABSENCE;
        }
        else
        {
            throw new IOException("Unable to deserialize SignUpType");
        }
    }
}
