package com.osacky.hipsterviz.api;

import com.google.gson.Gson;
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.osacky.hipsterviz.models.track.RealBaseTrack;
import com.osacky.hipsterviz.models.track.RealTrackNoTags;
import com.osacky.hipsterviz.models.track.RealTrackWithTags;

import java.lang.reflect.Type;

public class TrackDeserializer<T extends RealBaseTrack> implements JsonDeserializer<T> {
    @SuppressWarnings("unused")
    private static final String TAG = "TrackDeserializer";

    private final String field = "track";

    public TrackDeserializer() {
    }

    @Override
    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject jsonObject = json.getAsJsonObject();
        if (jsonObject.toString().equals("{}")) {
            throw new JsonParseException("Object was {}");
        } else if (jsonObject.has("error")) {
            String message = jsonObject.get("message").getAsString();
            throw new JsonParseException(message);
        } else if (jsonObject.has(field)) {
            final JsonObject jsonElement = jsonObject.getAsJsonObject(field);
            if (jsonElement.get("toptags").isJsonPrimitive()) {
                return (T) new Gson().fromJson(jsonElement, RealTrackNoTags.class);
            } else {
                return (T) new Gson().fromJson(jsonElement, RealTrackWithTags.class);
            }
        } else {
            final JsonObject jsonElement = jsonObject.getAsJsonObject(field);
            if (jsonElement.get("toptags").isJsonPrimitive()) {
                return (T) new Gson().fromJson(json, RealTrackNoTags.class);
            } else {
                return (T) new Gson().fromJson(json, RealTrackWithTags.class);
            }
        }
    }
}

@@ -474,16 +474,44 @@
 .Type;%0A%0A
+@SuppressWarnings(%22unused%22)%0A
 public c
@@ -590,44 +590,24 @@
 zer%3C
-T%3E %7B%0A    @SuppressWarnings(%22unused%22)
+RealBaseTrack%3E %7B
 %0A   
@@ -667,51 +667,8 @@
 %22;%0A%0A
-    private final String field = %22track%22;%0A%0A
     
@@ -728,17 +728,29 @@
  public 
-T
+RealBaseTrack
  deseria
@@ -902,24 +902,104 @@
 onObject();%0A
+        String trackString = %22track%22;%0A        String topTagsString = %22toptags%22;%0A
         if (
@@ -1296,21 +1296,27 @@
 ect.has(
-field
+trackString
 )) %7B%0A   
@@ -1374,37 +1374,43 @@
 getAsJsonObject(
-field
+trackString
 );%0A            i
@@ -1424,33 +1424,37 @@
 Element.get(
-%22
 top
-tags%22
+TagsString
 ).isJsonPrim
@@ -1478,36 +1478,32 @@
           return
- (T)
  new Gson().from
@@ -1579,36 +1579,32 @@
           return
- (T)
  new Gson().from
@@ -1752,13 +1752,19 @@
 ect(
-field
+trackString
 );%0A 
@@ -1794,25 +1794,29 @@
 ent.get(
-%22
 top
-tags%22
+TagsString
 ).isJson
@@ -1844,36 +1844,32 @@
           return
- (T)
  new Gson().from
@@ -1950,12 +1950,8 @@
 turn
- (T)
  new

fix those generics!!