using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HighScoreManager : MonoBehaviour {

	struct RowData {
		public string name;
		public int score;
	}

	List<RowData> rows = new List<RowData>();

	public Transform highscoreTable;
	public GameObject yourNameLabel;
	public GameObject playerNameEntry;
	public Text playerNameText;

	private GameObject scoreManager;
	private int playerScore = 0;
	private int playerRank = -1;

	struct HighScoreKeys {
		static public string HIGHSCORE_01 = "MIND_CONTROL_HS_SCORE_01";
		static public string HIGHSCORE_02 = "MIND_CONTROL_HS_SCORE_02";
		static public string HIGHSCORE_03 = "MIND_CONTROL_HS_SCORE_03";
		static public string HIGHSCORE_04 = "MIND_CONTROL_HS_SCORE_04";
		static public string HIGHSCORE_05 = "MIND_CONTROL_HS_SCORE_05";
	}

	struct NameKeys {
		static public string NAME_01 = "MIND_CONTROL_HS_NAME_01";
		static public string NAME_02 = "MIND_CONTROL_HS_NAME_02";
		static public string NAME_03 = "MIND_CONTROL_HS_NAME_03";
		static public string NAME_04 = "MIND_CONTROL_HS_NAME_04";
		static public string NAME_05 = "MIND_CONTROL_HS_NAME_05";
	}

	string[][] keyTable = new string[5][] {
		new string[] {NameKeys.NAME_01, HighScoreKeys.HIGHSCORE_01},
		new string[] {NameKeys.NAME_02, HighScoreKeys.HIGHSCORE_02},
		new string[] {NameKeys.NAME_03, HighScoreKeys.HIGHSCORE_03},
		new string[] {NameKeys.NAME_04, HighScoreKeys.HIGHSCORE_04},
		new string[] {NameKeys.NAME_05, HighScoreKeys.HIGHSCORE_05},
	};

	void Awake() {
		scoreManager = GameObject.Find("ScoreManager");
		if(scoreManager != null) {
			playerScore = scoreManager.GetComponent<ScoreManager>().PlayerScore;
			GameObject.Find("YourScore").GetComponent<Text>().text = string.Format("Your Score: {0:D8}", playerScore);
		} else {
			Debug.LogError("Couldn't find score manager");
		}

		LoadSavedHighScoreData();
		DisplayHighScore();
	}

	// Use this for initialization
	void Start () {
	}
	
	void LoadSavedHighScoreData() {
		for(int i = 0; i < keyTable.Length; i++) {
			string name = PlayerPrefs.GetString(keyTable[i][0], "-----------");
			int score = PlayerPrefs.GetInt(keyTable[i][1], 0);

			rows.Add(new RowData{ name = name, score = score });
		}
	}

	void DisplayHighScore() {
		for(int i = 0; i < highscoreTable.childCount && i < rows.Count; i++) {
			HighScoreRow highScoreRow = highscoreTable.GetChild(i).GetComponent<HighScoreRow>();

			if(playerScore > rows[i].score && playerRank == -1) {
				playerRank = i;
				SetActiveNameInput(true);
				highScoreRow.SetColor(Color.yellow);
			}

			highScoreRow.SetValue(string.Format("{0}. {1}", i + 1, rows[i].name), rows[i].score);
		}
	}

	public void EndNameEditing() {
		if(playerRank != -1) {
			// Insert the player at his/her position
			rows.Insert(playerRank, new RowData{ name = playerNameText.text, score = playerScore });
			// Remove the last entry
			rows.RemoveAt(rows.Count - 1);

			SetActiveNameInput(false);
			DisplayHighScore();
			SaveHighScore();
		}
	}

	public void SetActiveNameInput(bool active) {
		if(yourNameLabel != null) {
			yourNameLabel.SetActive(active);
		} else {
			Debug.LogError("Couldn't find your name label");
		}

		if(playerNameEntry != null) {
			playerNameEntry.SetActive(active);
		} else {
			Debug.LogError("Couldn't find player name entry");
		}
	}

	void SaveHighScore() {
		for(int i = 0; i < keyTable.Length && i < rows.Count; i++) {
			string[] keyPair = keyTable[i];
			RowData row = rows[i];

			PlayerPrefs.SetString(keyPair[0], row.name);
			PlayerPrefs.SetInt(keyPair[1], row.score);
		}
	}

	public void GoBackToMainMenu() {
		SceneManager.LoadScene("Menu");
	}
}
