package be.jeffcheasey88.peeratcode.routes;

import java.io.IOException;
import java.util.SortedSet;
import java.util.regex.Matcher;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
import be.jeffcheasey88.peeratcode.framework.HttpReader;
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.model.Player;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;

public class Leaderboard implements Response {

	private final DatabaseRepository databaseRepo;

	public Leaderboard(DatabaseRepository databaseRepo) {
		this.databaseRepo = databaseRepo;
	}
	
	@RouteDoc(path = "/leaderboard/{id}", responseCode = 200, responseDescription = "JSON contenant le leaderboard")

	@Route(path = "^\\/leaderboard\\/?(\\d+)?$")
	public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
		writer.response(200, "Access-Control-Allow-Origin: *");
		if (matcher.group(1) != null) {
			groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer);
		} else {
			playersLeaderboard(writer);
		}
	}

	public final void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
		Chapter chInfo = databaseRepo.getChapter(chapterId);

		SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
		JSONObject leaderboardJSON = new JSONObject();
		if (chInfo.getStartDate() != null)
			leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
		if (chInfo.getEndDate() != null)
			leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
		JSONArray groupsJSON = new JSONArray();
		if (allGroupsForChapter != null) {
			int rank = 1;
			int sameRankCount = 1;
			Group previousGroup = null;
			for (Group g : allGroupsForChapter) {
				if (previousGroup != null) {
					if (g.compareTo(previousGroup) == 0) {
						sameRankCount++;
					} else {
						rank = rank + sameRankCount;
						sameRankCount = 1;
					}
				}
				groupsJSON.add(g.toJson(rank));
				previousGroup = g;
			}
		}
		leaderboardJSON.put("groups", groupsJSON);
		writer.write(leaderboardJSON.toJSONString().replace("\\", ""));
	}

	public final void playersLeaderboard(HttpWriter writer) throws IOException {
		SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
		JSONArray playersJSON = new JSONArray();
		if (allPlayers != null) {
			for (Player player : allPlayers) {
				JSONObject playerJSON = new JSONObject();
				playerJSON.put("pseudo", player.getPseudo());
				if (player.getGroups() != null)
					playerJSON.put("groups", player.getJsonGroups());
//				if (player.getAvatar() != null)
//					playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
				playerJSON.put("rank", player.getRank());
				playerJSON.put("score", player.getTotalScore());
				playerJSON.put("completions", player.getTotalCompletion());
				playerJSON.put("tries", player.getTotalTries());
				playersJSON.add(playerJSON);
			}
		}
		writer.write(playersJSON.toJSONString().replace("\\", ""));
	}
}
