package editor.serialization;

import java.awt.Color;
import java.lang.reflect.Type;
import java.util.stream.Collectors;

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.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().stream().map((c) -> c.multiverseid().get(0)).collect(Collectors.toSet())));
        category.add("blacklist", context.serialize(src.getBlacklist().stream().map((c) -> c.multiverseid().get(0)).collect(Collectors.toSet())));
        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(MainFrame.inventory().get(element.getAsLong()));
        for (JsonElement element : obj.get("blacklist").getAsJsonArray())
            category.exclude(MainFrame.inventory().get(element.getAsLong()));
        category.setColor(context.deserialize(obj.get("color"), Color.class));
        category.setFilter(context.deserialize(obj.get("filter"), Filter.class));

        return category;
    }
}
@@ -81,44 +81,8 @@
 ype;
-%0Aimport java.util.stream.Collectors;
 %0A%0Aim
@@ -418,16 +418,50 @@
 rySpec;%0A
+import editor.database.card.Card;%0A
 import e
@@ -1009,227 +1009,81 @@
 st()
-.stream().map((c) -%3E c.multiverseid().get(0)).collect(Collectors.toSet())));%0A        category.add(%22blacklist%22, context.serialize(src.getBlacklist().stream().map((c) -%3E c.multiverseid().get(0)).collect(Collectors.toSet()
+));%0A        category.add(%22blacklist%22, context.serialize(src.getBlacklist(
 )));
@@ -1666,53 +1666,47 @@
 ude(
-MainFrame.inventory().get(element.getAsLong()
+context.deserialize(element, Card.class
 ));%0A
@@ -1812,53 +1812,47 @@
 ude(
-MainFrame.inventory().get(element.getAsLong()
+context.deserialize(element, Card.class
 ));%0A

Fix CategoryAdapter to use serial context