package it.larusba.integration.neo4jcouchbaseconnector.examples.spotify;

import java.io.IOException;

import org.junit.Test;
import org.springframework.web.client.RestTemplate;

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.CouchbaseCluster;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;

public class SpotifyRecursiveLoaderTest {

	private RestTemplate restTemplate = new RestTemplate();

	@Test
	public void test() throws IOException {
		System.out.println("Starting DB...");
		Cluster cluster = CouchbaseCluster.create();
		System.out.println("Created DB");
		System.out.println("Opening bucket...");
		Bucket bucket = cluster.openBucket("default");
		System.out.println("Opened bucket");

		int docs = 0;
		String artistId = "51Blml2LZPmy7TTiAg47vQ";
		String albumsUrl = "https://api.spotify.com/v1/artists/" + artistId + "/albums";
		System.out.println("Getting the artist's albums...");
		String albumsJson = this.restTemplate.getForObject(albumsUrl, String.class);
		System.out.println("Got the artist's albums...");
		docs++;
		JsonObject albumsObject = JsonObject.fromJson(albumsJson);
		String albumsId = albumsObject.getString("id");
		albumsId = albumsId == null ? "albums" + artistId : albumsId;
		JsonDocument albumsDocument = JsonDocument.create(albumsId, albumsObject);
		bucket.upsert(albumsDocument);
		JsonArray albums = albumsObject.getArray("items");
		Object albumObj = albums.get(0);
		// for (Object albumObj : albums) {
		JsonObject item = (JsonObject) albumObj;
		String itemHref = item.getString("href");
		System.out.println("Getting the album...");
		String albumJson = this.restTemplate.getForObject(itemHref, String.class);
		System.out.println("Got the album...");
		docs++;
		JsonObject albumObject = JsonObject.fromJson(albumJson);
		String albumId = albumObject.getString("id");
		JsonDocument albumDocument = JsonDocument.create(albumId, albumObject);
		bucket.upsert(albumDocument);
		JsonArray tracks = albumObject.getObject("tracks").getArray("items");
		Object trackObj = tracks.get(0);
		// for (Object trackObj : tracks) {
		JsonObject track = (JsonObject) trackObj;
		String trackHref = track.getString("href");
		System.out.println("Getting the track...");
		String trackJson = this.restTemplate.getForObject(trackHref, String.class);
		System.out.println("Got the track...");
		docs++;
		JsonObject trackObject = JsonObject.fromJson(trackJson);
		String trackId = trackObject.getString("id");
		JsonDocument trackDocument = JsonDocument.create(trackId, trackObject);
		bucket.upsert(trackDocument);
		// }
		// }
		System.out.println("Total documents " + docs);
	}
}

@@ -1570,46 +1570,8 @@
 ;%0A%09%09
-Object albumObj = albums.get(0);%0A%09%09// 
 for 
@@ -1595,24 +1595,25 @@
 : albums) %7B%0A
+%09
 %09%09JsonObject
@@ -1639,24 +1639,25 @@
 ) albumObj;%0A
+%09
 %09%09String ite
@@ -1682,32 +1682,33 @@
 ring(%22href%22);%0A%09%09
+%09
 System.out.print
@@ -1731,24 +1731,25 @@
 album...%22);%0A
+%09
 %09%09String alb
@@ -1805,32 +1805,33 @@
  String.class);%0A
+%09
 %09%09System.out.pri
@@ -1854,32 +1854,33 @@
 bum...%22);%0A%09%09
+%09
 docs++;%0A
 %09%09JsonObject
@@ -1859,32 +1859,33 @@
 .%22);%0A%09%09%09docs++;%0A
+%09
 %09%09JsonObject alb
@@ -1921,32 +1921,33 @@
 n(albumJson);%0A%09%09
+%09
 String albumId =
@@ -1968,32 +1968,33 @@
 etString(%22id%22);%0A
+%09
 %09%09JsonDocument a
@@ -2045,32 +2045,33 @@
 albumObject);%0A%09%09
+%09
 bucket.upsert(al
@@ -2076,32 +2076,33 @@
 albumDocument);%0A
+%09
 %09%09JsonArray trac
@@ -2163,46 +2163,9 @@
 ;%0A%09%09
-Object trackObj = tracks.get(0);%0A%09%09// 
+%09
 for 
@@ -2193,16 +2193,18 @@
 acks) %7B%0A
+%09%09
 %09%09JsonOb
@@ -2237,24 +2237,26 @@
 trackObj;%0A%09%09
+%09
+%09
 String track
@@ -2283,24 +2283,26 @@
 ng(%22href%22);%0A
+%09%09
 %09%09System.out
@@ -2331,24 +2331,26 @@
 track...%22);%0A
+%09%09
 %09%09String tra
@@ -2407,32 +2407,34 @@
  String.class);%0A
+%09%09
 %09%09System.out.pri
@@ -2455,24 +2455,26 @@
 track...%22);%0A
+%09%09
 %09%09docs++;%0A%09%09
@@ -2465,32 +2465,34 @@
 ;%0A%09%09%09%09docs++;%0A%09%09
+%09
+%09
 JsonObject track
@@ -2528,24 +2528,26 @@
 trackJson);%0A
+%09%09
 %09%09String tra
@@ -2580,24 +2580,26 @@
 ng(%22id%22);%0A%09%09
+%09
+%09
 JsonDocument
@@ -2650,32 +2650,34 @@
 , trackObject);%0A
+%09%09
 %09%09bucket.upsert(
@@ -2698,18 +2698,13 @@
 ;%0A%09%09
-// 
+%09
 %7D%0A%09%09
-// 
 %7D%0A%09%09

fix foreach object