package se.michaelthelin.spotify;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import se.michaelthelin.spotify.SpotifyProtos.Artist;
import se.michaelthelin.spotify.SpotifyProtos.Image;

import java.util.List;

public class JsonUtil {

  public static List<Artist> newArtistList(String json) {
    throw new RuntimeException("Not implemented");
  }

  public static Artist newArtist(String json) {
    return newArtist(JSONObject.fromObject(json));
  }

  public static Artist newArtist(JSONObject jsonObject) {
    if (jsonObject == null || jsonObject.isNullObject()) {
      return null;
    }
    Artist.Builder artistBuilder = Artist.newBuilder();

    artistBuilder.setApiLink(jsonObject.getString("api_link"));

    JSONArray apiLinkArray = jsonObject.getJSONArray("genres");
    for (int i = 0; i < apiLinkArray.size(); i++) {
      artistBuilder.addGenres(apiLinkArray.getString(i));
    }

    artistBuilder.setId(jsonObject.getString("id"));

    Artist.Images.Builder artistImagesBuilder = Artist.Images.newBuilder();
    JSONObject imagesJsonObject = jsonObject.getJSONObject("images");

    Image.Builder smallImageBuilder = Image.newBuilder();
    JSONObject smallImageJsonObject = imagesJsonObject.getJSONObject("SMALL");
    smallImageBuilder.setHeight(smallImageJsonObject.getInt("height"));
    smallImageBuilder.setImageUrl(smallImageJsonObject.getString("image_url"));
    smallImageBuilder.setWidth(smallImageJsonObject.getInt("width"));
    artistImagesBuilder.setSMALL(smallImageBuilder.build());

    Image.Builder mediumImageBuilder = Image.newBuilder();
    JSONObject mediumImageJsonObject = imagesJsonObject.getJSONObject("MEDIUM");
    mediumImageBuilder.setHeight(mediumImageJsonObject.getInt("height"));
    mediumImageBuilder.setImageUrl(mediumImageJsonObject.getString("image_url"));
    mediumImageBuilder.setWidth(mediumImageJsonObject.getInt("width"));
    artistImagesBuilder.setMEDIUM(mediumImageBuilder.build());

    Image.Builder largeImageBuilder = Image.newBuilder();
    JSONObject largeImageJsonObject = imagesJsonObject.getJSONObject("LARGE");
    largeImageBuilder.setHeight(largeImageJsonObject.getInt("height"));
    largeImageBuilder.setImageUrl(largeImageJsonObject.getString("image_url"));
    largeImageBuilder.setWidth(largeImageJsonObject.getInt("width"));
    artistImagesBuilder.setLARGE(largeImageBuilder.build());

    artistBuilder.setImages(artistImagesBuilder.build());

    artistBuilder.setLink(jsonObject.getString("link"));
    artistBuilder.setName(jsonObject.getString("name"));
    artistBuilder.setPopularity(jsonObject.getInt("popularity"));
    artistBuilder.setSpotifyUri(jsonObject.getString("spotify_uri"));
    artistBuilder.setType(jsonObject.getString("type"));

    return artistBuilder.build();
  }
}

@@ -246,16 +246,416 @@
 Util %7B%0A%0A
+  public static Image newImage(String json) %7B%0A    return newImage(JSONObject.fromObject(json));%0A  %7D%0A%0A  public static Image newImage(JSONObject jsonObject) %7B%0A    Image.Builder builder = Image.newBuilder();%0A    builder.setHeight(jsonObject.getInt(%22height%22));%0A    builder.setImageUrl(jsonObject.getString(%22image_url%22));%0A    builder.setWidth(jsonObject.getInt(%22width%22));%0A    return builder.build();%0A  %7D%0A%0A
   public
@@ -1514,66 +1514,8 @@
 );%0A%0A
-    Image.Builder smallImageBuilder = Image.newBuilder();%0A
     
@@ -1597,186 +1597,35 @@
     
-smallImageBuilder.setHeight(smallImageJsonObject.getInt(%22height%22));%0A    smallImageBuilder.setImageUrl(smallImageJsonObject.getString(%22image_url%22));%0A    smallImageBuilder.setWidth
+Image smallImage = newImage
 (sma
@@ -1641,32 +1641,16 @@
 onObject
-.getInt(%22width%22)
 );%0A    a
@@ -1691,517 +1691,197 @@
 mage
-Builder.build());%0A%0A    Image.Builder mediumImageBuilder = Image.newBuilder();%0A    JSONObject mediumImageJsonObject = imagesJsonObject.getJSONObject(%22MEDIUM%22);%0A    mediumImageBuilder.setHeight(mediumImageJsonObject.getInt(%22height%22));%0A    mediumImageBuilder.setImageUrl(mediumImageJsonObject.getString(%22image_url%22));%0A    mediumImageBuilder.setWidth(mediumImageJsonObject.getInt(%22width%22));%0A    artistImagesBuilder.setMEDIUM(mediumImageBuilder.build());%0A%0A    Image.Builder largeImageBuilder = Image.newBuilder(
+);%0A%0A    JSONObject mediumImageJsonObject = imagesJsonObject.getJSONObject(%22MEDIUM%22);%0A    Image mediumImage = newImage(smallImageJsonObject);%0A    artistImagesBuilder.setMEDIUM(smallImage
 );%0A
+%0A
     
@@ -1963,186 +1963,35 @@
     
-largeImageBuilder.setHeight(largeImageJsonObject.getInt(%22height%22));%0A    largeImageBuilder.setImageUrl(largeImageJsonObject.getString(%22image_url%22));%0A    largeImageBuilder.setWidth
+Image largeImage = newImage
 (lar
@@ -2007,32 +2007,16 @@
 onObject
-.getInt(%22width%22)
 );%0A    a
@@ -2053,31 +2053,16 @@
 rgeImage
-Builder.build()
 );%0A%0A    

Refactor JsonUtil to create images easier