package data;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Data for a GitHub repository
 * 
 * @author Lisa Nguyen Quang Do
 */

public class Repository {

	public final int id;
	public int forks;
	public int open_issues;
	public int size;

	public final String name;
	public String description = "";
	public String creation_date;
	public String latest_update = "";
	public int score;

	public boolean hasDownloads;
	public boolean hasWiki;
	public boolean isPrivate;

	public String html_url;
	public String releases_url;
	public String issues_url;
	public String commits_url;
	public String contents_url;
	public String contributors_url;

	public String license = "";

	public String owner = "";
	public String ownerType = "";

	public int starred;
	public int watched;

	public Repository(JSONObject item) throws JSONException {
		this.id = item.getInt("id");
		this.forks = item.getInt("forks_count");
		this.open_issues = item.getInt("open_issues_count");
		this.size = item.getInt("size");

		this.name = item.getString("name"); // full_name
		if (!item.isNull("description"))
			this.description = item.getString("description");
		if (item.has("pushed_at") && (item.get("pushed_at") instanceof String))
			this.latest_update = item.getString("pushed_at");
		this.creation_date = item.getString("created_at");
		this.score = item.getInt("score");

		this.hasDownloads = item.getBoolean("has_downloads");
		this.hasWiki = item.getBoolean("has_wiki");
		this.isPrivate = item.getBoolean("private");

		this.html_url = item.getString("html_url");
		this.releases_url = item.getString("releases_url");
		this.commits_url = item.getString("commits_url");
		this.issues_url = item.getString("issues_url");
		this.contents_url = item.getString("contents_url");
		this.contributors_url = item.getString("contributors_url");

		if (item.has("licence")) {
			JSONObject lic = item.getJSONObject("license");
			this.license = lic.getString("name");
		}

		this.starred = item.getInt("stargazers_count");
		this.watched = item.getInt("watchers_count");

		JSONObject owner = item.getJSONObject("owner");
		this.owner = owner.getString("login");
		this.ownerType = owner.getString("type");
	}

	@Override
	public boolean equals(Object o) {
		if (o == null || !(o instanceof Repository))
			return false;
		return (((Repository) o).id == this.id);
	}

	@Override
	public int hashCode() {
		return this.id;
	}

	@Override
	public String toString() {
		return this.name + " (" + this.id + ")";
	}

}
