package vn.edu.usth.musicplayer.Model;

import android.content.Context;
import android.util.Log;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.jayway.jsonpath.JsonPath;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by MacbookPro on 4/12/16.
 */

public class SongAPI {
    private static com.android.volley.RequestQueue queue;
    private final static String[] songNames = {"Hello", "Grenade"};
    private static ArrayList<JSONObject> songs = null;

    private SongAPI() {
    }

    public static ArrayList<JSONObject> getSongs(Context context) {
        if(songs == null) {
            queue = RequestQueue.getQueue(context);
            getSongsFromZingAPI();
        }
        return songs;
    }

    private static void getSongsFromZingAPI() {
        for (String songName: songNames) {
            getMP3ZingSongIDAndGetInfo(songName);
        }
    }

    private static void getMP3ZingSongIDAndGetInfo(String songName) {
        Response.Listener<JSONObject> songIDRequestListener = new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String songID = JsonPath.read(response, "$[0]");
                Log.i("songAPI", "SongId: " + songID);
//                getMP3ZingSongInfo(songID);
            }
        };

        String url = "http://j.ginggong.com/jOut.ashx?h=mp3.zing.vn&code=77076dc6-6a6b-4f70-bbad-7888bb59cc7d&k=" + songName;
        JsonObjectRequest songIDRequest = new JsonObjectRequest(url, null, songIDRequestListener, null);
        queue.add(songIDRequest);
    }

    private static void getMP3ZingSongInfo(String songID) {
        Response.Listener<JSONObject> songInfoRequestListener = new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                songs.add(response);
            }
        };

        String url = "http://api.mp3.zing.vn/api/mobile/song/getsonginfo?requestdata={\"id\":\"" + songID + "\"}";
        JsonObjectRequest songInfoRequest = new JsonObjectRequest(url, null, songInfoRequestListener, null);
        queue.add(songInfoRequest);
    }
}

@@ -168,58 +168,210 @@
 Json
-ObjectRequest;%0Aimport com.jayway.jsonpath.JsonPath
+ArrayRequest;%0Aimport com.android.volley.toolbox.JsonObjectRequest;%0Aimport com.jayway.jsonpath.Configuration;%0Aimport com.jayway.jsonpath.JsonPath;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException
 ;%0Aim
@@ -1184,30 +1184,29 @@
 istener%3CJSON
-Object
+Array
 %3E songIDRequ
@@ -1237,38 +1237,37 @@
 se.Listener%3CJSON
-Object
+Array
 %3E() %7B%0A          
@@ -1317,34 +1317,387 @@
 nse(JSON
-Object response) %7B
+Array response) %7B%0A                JSONObject songIDObject = null;%0A                try %7B%0A                    songIDObject = response.getJSONObject(0);%0A                %7D catch (JSONException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A                Object document = Configuration.defaultConfiguration().jsonProvider().parse(songIDObject.toString());%0A
 %0A       
@@ -1739,23 +1739,27 @@
 ead(
-response, %22$%5B0%5D
+document, %22$.SiteId
 %22);%0A
@@ -1797,17 +1797,17 @@
 , %22SongI
-d
+D
 : %22 + so
@@ -1813,18 +1813,17 @@
 ongID);%0A
-//
+%0A
         
@@ -2014,38 +2014,37 @@
 me;%0A        Json
-Object
+Array
 Request songIDRe
@@ -2051,38 +2051,37 @@
 quest = new Json
-Object
+Array
 Request(url, nul
@@ -2072,30 +2072,24 @@
 Request(url,
- null,
  songIDReque

Fix error getting song ID