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

	public String getURL(int page) {
		return "https://api.github.com/search/repositories?q=license:apache-2.0&page=" + page;
	}

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

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

				String titre = repo.getString("full_name");
				String url = repo.getString("html_url");
				String licence = repo.getJSONObject("license").getString("spdx_id");

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

		return resultats;
	}
}
