﻿using UnityEngine;
using System.Collections;

public class PlayerPrefsManager : MonoBehaviour {

	const string MASTER_VOLUME_KEY = "master_volume";
	const string DIFFICULTY_KEY = "difficulty";
	const string LEVEL_KEY = "level_unlocked_";

	public static void SetMasterVolume (float volume) {
		if (volume >= 0f && volume <= 1f) {
			PlayerPrefs.SetFloat (MASTER_VOLUME_KEY, volume);
		} else {
			Debug.LogError("Master Volume out of range");
		}
	}

	public static float GetMasterVolume () {
		return PlayerPrefs.GetFloat (MASTER_VOLUME_KEY);
	}

	public static void UnlockedLevel (int level) {
		if (level <= Application.levelCount - 1) {
			PlayerPrefs.SetInt (LEVEL_KEY + level.ToString (), 1);
		} else {
			Debug.LogError ("Trying to unlock level not in build order");
		}
	}

	public static bool IsLevelUnlocked (int level) {
		int levelValue = PlayerPrefs.GetInt (LEVEL_KEY + level.ToString ());
		bool isLevelUnlocked = (levelValue == 1);
		if (level <= Application.levelCount - 1) {
			return isLevelUnlocked;
		} else {
			Debug.LogError ("Trying to unlock level not in build order");
			return false;
		}
	}

	public static void SetDifficulty (float difficulty) {
		Debug.Log ("difficulty passed to PlayerPrefsManager as: " + difficulty);
		Debug.Log ("PlayerPrefs Difficulty is currently "+ PlayerPrefs.GetFloat(DIFFICULTY_KEY));

		if (difficulty >= 1f && difficulty <= 3f) {
			//set difficulty
			PlayerPrefs.SetFloat (DIFFICULTY_KEY, difficulty);
			Debug.Log("PlayerPrefs Difficulty Key has been changed to "+ PlayerPrefs.GetFloat(DIFFICULTY_KEY));
		} else {
			Debug.LogError ("Trying to set difficulty out of range");
		}
	}

	public static int GetDifficulty () {
		int difficulty = PlayerPrefs.GetInt (DIFFICULTY_KEY);
		return difficulty;
	}
}
