using System.IO;
using UnityEngine;

namespace Managers
{
    [System.Serializable]
    public class SaveData
    {
        public bool doorIntroAlreadyPlayed = false;
        public int lastCompletedLevel = 0;
    }
    
    public static class GlobalData
    {
        private static SaveData _data = new SaveData();
        
        public static bool doorIntroAlreadyPlayed
        {
            get { return _data.doorIntroAlreadyPlayed; }
            set 
            { 
                _data.doorIntroAlreadyPlayed = value;
                SaveData();
            }
        }
        
        public static int lastCompletedLevel
        {
            get { return _data.lastCompletedLevel; }
            set 
            { 
                _data.lastCompletedLevel = value;
                SaveData();
            }
        }
        
        private static string SavePath => Path.Combine(Application.persistentDataPath, "globaldata.json");
        
        public static void SaveData()
        {
            string json = JsonUtility.ToJson(_data, true);
            File.WriteAllText(SavePath, json);
        }
        
        public static void LoadData()
        {
            if (File.Exists(SavePath))
            {
                string json = File.ReadAllText(SavePath);
                _data = JsonUtility.FromJson<SaveData>(json);
            }
        }
    }
}