package org.sillyjirafe.uoapi.binds;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;
import org.sillyblahaj.uoapi.Api;
import org.sillyblahaj.uoapi.element;

public class OpenVerseAudiosApi implements Api 
{
	public String getName()
	{
		return "OpenVerseAudiosApi";
	}

	public String getURL(int page) {
		return "https://api.openverse.org/v1/audio/?format=json&page=" + page;
	}

	public List<element> Parse(String jsonRaw) {
		List<element> resultats = new ArrayList<>();
		
		
		try { 
		JSONObject json = new JSONObject(jsonRaw);
		JSONArray items = json.getJSONArray("results");

		for (int i = 0; i < items.length(); i++) {
			JSONObject image = items.getJSONObject(i);

			String titre = image.getString("title");
			String url = image.getString("url");
			String licence = image.getString("license");

			resultats.add(new element(titre, url, licence, "openverse", "audio"));
		}
		} catch (org.json.JSONException e) {
	        System.out.println("error but continue : " + e.getMessage());
	    }
		return resultats;
	}
}
