package org.sillyjrafe.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 GitHubLGPLApi implements Api 
{
	
	public String getName()
	{
		return "GitHubLGPLApi";
	}

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