package ch.zhaw.gartenverwaltung.json;

import ch.zhaw.gartenverwaltung.io.PlantList;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import javafx.scene.image.Image;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Used by the Jackson Library to deserialize a String to a {@link Image}
 */
public class PlantImageDeserializer extends JsonDeserializer<Image> {

    @Override
    public Image deserialize(JsonParser parser, DeserializationContext context) throws IOException {
        Image result = null;
        InputStream is = PlantList.class.getResourceAsStream(String.format("images/%s", parser.getText()));
        if (is != null) {
            result = new Image(is);
        }
        return result;
    }
}
