using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem
{
   public static void SaveGame(GameManager gameManager)
   {
      BinaryFormatter formatter = new BinaryFormatter();
      string path = Application.persistentDataPath + "/game.death";
      FileStream stream = new FileStream(path, FileMode.Create);
      
      GameData data = new GameData(gameManager);
      
      formatter.Serialize(stream, data);
      stream.Close();
   }

   public static GameData LoadGame()
   {
      string path = Application.persistentDataPath + "/game.death";
      if (File.Exists(path))
      {
         BinaryFormatter formatter = new BinaryFormatter();
         FileStream stream = new FileStream(path, FileMode.Open);

         GameData data = formatter.Deserialize(stream) as GameData;
         stream.Close();
         
         return data;
      }
      else
      {
         Debug.LogError("Save file doesn't exist: " + path);
         return null;
      }
   }
}
