package org.sillyjirafe.uoapi.binds;

import org.json.JSONArray;
import org.json.JSONObject;
import org.sillyjirafe.uoapi.Api;
import org.sillyjirafe.uoapi.element;
import java.util.ArrayList;
import java.util.List;

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

	public String getURL(int page) {
		return "https://api.github.com/search/repositories?q=license:mit&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;
	}
}
