package de.heatup.highscore;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
 * 
 * @author daniel
 *
 */
public class MapHighscores implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Map<Integer, ArrayList<ScoreEntry>> highscoreOfAllMaps = new HashMap<Integer, ArrayList<ScoreEntry>>();
	
	protected ArrayList<ScoreEntry> getHighscores(int mapHash) {
		if (highscoreOfAllMaps.get(mapHash) == null) {
			ArrayList<ScoreEntry> mapHighscores = new ArrayList<ScoreEntry>();
			for (int n = 0; n<10; n++) {
				mapHighscores.add(new ScoreEntry());
			}
			highscoreOfAllMaps.put(mapHash, mapHighscores);
		}
		return highscoreOfAllMaps.get(mapHash);
	}
	
	protected void setHighscore(int mapHash, String name, int score) {
		
		ArrayList<ScoreEntry> mapHighscores = highscoreOfAllMaps.get(mapHash);
		
		ArrayList<ScoreEntry> newMapHighscores = new ArrayList<ScoreEntry>();
		
		int i = 0;
		
		for (i = 0; i<10; i++) {
			
			if (mapHighscores.get(i).getScore() > score) {
				newMapHighscores.add(mapHighscores.get(i));
			} else {
				newMapHighscores.add(new ScoreEntry(name, score));
				break;
			}
			
		}
		
		for (int j = i; j<10; j++) {
			
			newMapHighscores.add(mapHighscores.get(j));
			
		}
		highscoreOfAllMaps.put(mapHash, newMapHighscores);
			
	}
}
