using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{ 
    public static void SaveLevels(Levels levels) 
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/levels.data";
        FileStream stream = new FileStream(path, FileMode.Create);

        LevelsData data = new LevelsData(levels);
        formatter.Serialize(stream, data);
        stream.Close();
    }

    public static LevelsData LoadLevels() 
    {
        string path = Application.persistentDataPath + "/levels.data";
        if (File.Exists(path)) 
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            LevelsData data = formatter.Deserialize(stream) as LevelsData;
            stream.Close();

            return data;
        } else 
        {
            return new LevelsData(new Levels());
        }
    }
}
