using UnityEngine;
using System;
using System.Collections;

public class PersistenceManager
{
    public static PersistenceManager Instance { get; private set; }
    static PersistenceManager() { Instance = new PersistenceManager(); }

    public PersistenceManager()
    {
		Initialize();
    }

    private void Initialize()
    {
        foreach (Enums.eTownsfolkGoodFacts goodFact in Enum.GetValues(typeof(Enums.eTownsfolkGoodFacts)))
        {
            SafelySetIntKey("FactUnlocked_" + goodFact.ToString(), 0);
        }
        foreach (Enums.eTownsfolkBadFacts badFact in Enum.GetValues(typeof(Enums.eTownsfolkBadFacts)))
        {
            SafelySetIntKey("FactUnlocked_" + badFact.ToString(), 0);
        }
    }

    private void SafelySetIntKey(string key, int value)
    {
        if (!PlayerPrefs.HasKey(key))
        {
            SetInt(key, value);
        }
    }

    public int GetInt(string key)
    {
        Util.AssertTrue(PlayerPrefs.HasKey(key), "The key does not exist in the PlayerPrefs dictionary: " + key);
        return PlayerPrefs.GetInt(key);
    }

    public void SetInt(string key, int value)
    {
        PlayerPrefs.SetInt(key, value);
    }

    public void IncInt(string key, int increment = 1)
    {
        Util.AssertTrue(increment > 0, "IncInt's increment value must be positive");
        PlayerPrefs.SetInt(key, PlayerPrefs.GetInt(key) + increment);
    }

    public void DecInt(string key, int decrement = 1)
    {
        Util.AssertTrue(decrement > 0, "DecInt's decrement value must be positive");
        PlayerPrefs.SetInt(key, PlayerPrefs.GetInt(key) - decrement);
    }
	
    public string GetString(string key)
    {
        Util.AssertTrue(PlayerPrefs.HasKey(key), "The key does not exist in the PlayerPrefs dictionary: " + key);
        return PlayerPrefs.GetString(key);
    }

    public void SetString(string key, string value)
    {
        PlayerPrefs.SetString(key, value);
    }

    public float GetFloat(string key)
    {
        Util.AssertTrue(PlayerPrefs.HasKey(key), "The key does not exist in the PlayerPrefs dictionary: " + key);
        return PlayerPrefs.GetFloat(key);
    }

    public void SetFloat(string key, float value)
    {
        PlayerPrefs.SetFloat(key, value);
    }
}