package org.sillyjirafe.uoapi.binds;

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

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

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

	public String getURL(int page) {
		return "https://api.openverse.org/v1/images/?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", "image"));
		}
		} catch (org.json.JSONException e) {
	        System.out.println("error but continue : " + e.getMessage());
	    }
		return resultats;
	}
}
