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

public class GameData : MonoBehaviour {

    public static GameData instance;

    // Prefs of time
    private float timeSpent = 0;
    private float elapsedTime = 0;
    private bool chronoActive = false;

    // Prefs of score
    [HideInInspector]
    public int currentScore;

    public void Start()
    {
        if (instance == null)
            instance = this;

        GetPrefs();
    }

    public void Update()
    {
        // manage the time
        if(chronoActive)
        {
            elapsedTime += Time.deltaTime;
        }
    }

    private void GetPrefs()
    {
        if (PlayerPrefs.HasKey("timeSpent"))
        {
            timeSpent = PlayerPrefs.GetFloat("timeSpent");
        }
    }

    private void SavePrefs()
    {
        PlayerPrefs.SetFloat("timeSpent", timeSpent+elapsedTime);
    }

    private void StartTime()
    {
        chronoActive = true;
    }

    private void StopTime()
    {
        chronoActive = false;
    }

    public int GetLevelScore(string levelName)
    {
        int score = 0;

        if (PlayerPrefs.HasKey(levelName))
        {
            score = PlayerPrefs.GetInt(levelName);
        }

        return score;
    }

    public void SetLevelScore(string levelName)
    {
        int score = 0;

        if (PlayerPrefs.HasKey(levelName))
        {
            score = PlayerPrefs.GetInt(levelName);
        }

        if(score <= currentScore)
        {
            PlayerPrefs.SetInt(levelName, currentScore);
        }
    }

}
