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

public static class SaveQuestSystem
{
    public static void SaveQuestItem(Task quest, string ElementFolderName)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        // todo: add in scene name folder
        string path = SaveSystem.GetSavePath(ElementFolderName); 
Debug.Log(path);
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        
        path += "/" + quest.title + ".fun";

        FileStream stream = new FileStream(path, FileMode.Create);


        formatter.Serialize(stream, quest);
        stream.Close();   
    }

    public static void SaveQuestsList(List<Task> questsList, string ElementName)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        // todo: add in scene name folder
        string path = SaveSystem.GetSavePath(); 
Debug.Log("Saved Quest at " + path);
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        
        path += "/" + ElementName + ".fun";

        FileStream stream = new FileStream(path, FileMode.Create);

        formatter.Serialize(stream, questsList);
        stream.Close();  
    }

    private static void Save() 
    {

    }

    public static Task LoadQuestItem(string questTitle, string ElementFolderName)
    {
        string path = SaveSystem.GetSavePath(ElementFolderName) + "/" + questTitle;// + ".fun"; 

        if(File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            Task quest = formatter.Deserialize(stream) as Task;
            stream.Close();

            return quest;
        } else {
            Debug.Log("Save file not found in " + path);
            return null;
        }
    }

    public static List<Task> LoadQuestsList(string ElementName)
    {
        string path = SaveSystem.GetSavePath() + "/" + ElementName  + ".fun"; 

        if(File.Exists(path))
        {
            
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            List<Task> questsList = formatter.Deserialize(stream) as List<Task>;
            stream.Close();

            return questsList;
        } else {
            Debug.Log("Save file not found in " + path);
            return null;
        }
    }

    private static void Load() 
    {

    }
}