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 PixabayImagesApi implements Api 
{
	public String getName()
	{
		return "PixabayImagesApi";
	}

	public String getURL(int page, String apikey, String rnd) {
		return "https://pixabay.com/api/?per_page=200&page=" + page + "&key=" + apikey;
	}

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

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

				String titre = repo.getString("name");
				String url = repo.getString("pageURL");
				String licence = "cc0";

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

		return resultats;
	}
}
