using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class HighScoreManager : MonoBehaviour
{
		static int nHighScoresSaved = 10;
		
		public static SortedDictionary<int,string> LoadHighscores ()
		{
				SortedDictionary<int,string> highscores = new SortedDictionary<int, string> (new ReverseComparer<int> (Comparer<int>.Default));
				int pos = 1;
				
				while (PlayerPrefs.HasKey("HighScore_Pos_" + (pos).ToString())) {
						int score = PlayerPrefs.GetInt ("HighScore_Pos_" + pos.ToString ());
						string type = PlayerPrefs.GetString ("HighScore_TypeAtPos_" + pos.ToString ());
						highscores.Add (score, type);
						pos++;
				}
				return highscores;
		}
		
		public static void AddHighscore (int score, string gameType)
		{
				if (score == 0) {
						return;
				}
				SortedDictionary<int,string> highscores = LoadHighscores ();
				
				highscores.Add (score, gameType);
				
				int j = 0;
				foreach (KeyValuePair<int, string> highscore in highscores) {
						j++;
						if (j <= nHighScoresSaved) {
								PlayerPrefs.SetInt ("HighScore_Pos_" + j.ToString (), highscore.Key);
								PlayerPrefs.SetString ("HighScore_TypeAtPos_" + j.ToString (), highscore.Value);
						}
				}
				
				PlayerPrefs.Save ();
				
		}
}