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

public class dataSelect : MonoBehaviour {

	bool[]							levels = {true, false, false, false, true, false, false, false, true, false, false, false};
	[HideInInspector]public int		lifesLost = 0;
	[HideInInspector]public int		ringsEarned = 0;
//	[HideInInspector]public int[]	scores = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};// new int  [12];//
	int[]	scores = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};// new int  [12];//
	[HideInInspector]public int		activeLevel = 0;

	static public dataSelect		data;

	void Awake() {
		if (data == null) {
			data = this;
		}
	}

	void Start() {
		initLevels ();
		initLifesLost ();
		initRingsEarned ();
	}

	void initLevels() {
		int level = 0;
		foreach (bool isActive in levels) {
			if (PlayerPrefs.HasKey ("level" + level)) {
				if (PlayerPrefs.GetInt ("level" + level) > 0) {
					levels [level] = true;
				} else {
					levels [level] = false;
				}
			}
			if (PlayerPrefs.HasKey ("score" + level)) {
				scores[level] = PlayerPrefs.GetInt ("score" + level);
			} else {
				scores[level] = 0;
			}
			if (levels [level]) {
				activateLevel (level);
			} else {
				desactivateLevel (level);
			}
			++level;
		}
	}

	void activateLevel(int level) {
		CanvasGroup canvasLevel = GetComponentsInChildren<CanvasGroup> () [level];
		PlayerPrefs.SetInt("Level" + level, 1);
//		canvasLevel.transform.GetChild (0).GetComponent<Image> ().color = Color.white;//new Color (1, 1, 1, 0);
		canvasLevel.interactable = true;
	}
	
	void desactivateLevel(int level) {
		CanvasGroup canvasLevel = GetComponentsInChildren<CanvasGroup> () [level];
		PlayerPrefs.SetInt("Level" + level, 0);
		canvasLevel.interactable = false;
	}
	
	void initLifesLost() {
		if (!PlayerPrefs.HasKey ("lifesLost")) {
			PlayerPrefs.SetInt ("lifesLost", 0);
		} else {
			lifesLost = PlayerPrefs.GetInt("lifesLost");
		}
	}
	
	void initRingsEarned() {
		if (!PlayerPrefs.HasKey ("ringsEarned")) {
			PlayerPrefs.SetInt ("ringsEarned", 0);
		} else {
			ringsEarned = PlayerPrefs.GetInt("ringsEarned");
		}
	}
	
	public void reset() {
		PlayerPrefs.DeleteAll ();
	}
	
	public void save () {
		PlayerPrefs.Save ();
	}
	
	public void unlockLevel(int level) {
		PlayerPrefs.SetInt ("Level" + level, 1);
	}
}
