package editor.serialization;

import java.awt.Color;
import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import editor.collection.deck.CategorySpec;
import editor.database.card.Card;
import editor.filter.Filter;
import editor.gui.MainFrame;

/**
 * A JSON serializer and deserializer for {@link CategorySpec}.
 * 
 * @author Alec Roelke
 */
public class CategoryAdapter implements JsonSerializer<CategorySpec>, JsonDeserializer<CategorySpec>
{
    @Override
    public JsonElement serialize(CategorySpec src, Type typeOfSrc, JsonSerializationContext context)
    {
        JsonObject category = new JsonObject();
        category.addProperty("name", src.getName());
        category.add("whitelist", context.serialize(src.getWhitelist()));
        category.add("blacklist", context.serialize(src.getBlacklist()));
        category.add("color", context.serialize(src.getColor()));
        category.add("filter", context.serialize(src.getFilter()));

        return category;
    }

    @Override
    public CategorySpec deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        JsonObject obj = json.getAsJsonObject();

        CategorySpec category = new CategorySpec();
        category.setName(obj.get("name").getAsString());
        for (JsonElement element : obj.get("whitelist").getAsJsonArray())
            category.include(context.deserialize(element, Card.class));
        for (JsonElement element : obj.get("blacklist").getAsJsonArray())
            category.exclude(context.deserialize(element, Card.class));
        category.setColor(context.deserialize(obj.get("color"), Color.class));
        category.setFilter(context.deserialize(obj.get("filter"), Filter.class));

        return category;
    }
}
@@ -77,16 +77,38 @@
 ct.Type;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -394,16 +394,58 @@
 ializer;
+%0Aimport com.google.gson.reflect.TypeToken;
 %0A%0Aimport
@@ -548,37 +548,8 @@
 ter;
-%0Aimport editor.gui.MainFrame;
 %0A%0A/*
@@ -966,24 +966,92 @@
 getName());%0A
+        category.add(%22filter%22, context.serialize(src.getFilter()));%0A
         cate
@@ -1255,76 +1255,8 @@
 )));
-%0A        category.add(%22filter%22, context.serialize(src.getFilter()));
 %0A%0A  
@@ -1590,24 +1590,106 @@
 sString());%0A
+        category.setFilter(context.deserialize(obj.get(%22filter%22), Filter.class));%0A
         for 
@@ -1960,32 +1960,32 @@
 , Card.class));%0A
+
         category
@@ -2050,90 +2050,8 @@
 s));
-%0A        category.setFilter(context.deserialize(obj.get(%22filter%22), Filter.class));
 %0A%0A  

Fix order of setting category fields