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

public static class SaveEquippableItemSystem
{

    private static string map;

    public static void SaveEquitableItem(EquippableItemPrefabAsset equitabbleItem, 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 += "/" + equitabbleItem.Name + ".fun";

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

        EquippableItemPrefabAssetData data = new EquippableItemPrefabAssetData(equitabbleItem);

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

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

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

        List<EquippableItemPrefabAssetData> data = new List<EquippableItemPrefabAssetData>();
        
        foreach(EquippableItemPrefabAsset equitabbleItem in equitabbleItemList)
        {
            data.Add(new EquippableItemPrefabAssetData(equitabbleItem));
        }

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

    private static void Save() 
    {

    }

    public static EquippableItemPrefabAssetData LoadEquitableItem(string equitabbleItemName, string ElementFolderName)
    {
        string path = SaveSystem.GetSavePath(ElementFolderName) + "/" + equitabbleItemName;// + ".fun"; 

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

            EquippableItemPrefabAssetData equitabbleItem = formatter.Deserialize(stream) as EquippableItemPrefabAssetData;
            stream.Close();

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

    public static List<EquippableItemPrefabAssetData> LoadEquitableItemList(string _path, string ElementName)
    {
        string path = SaveSystem.GetSavePath(_path) + "/" + ElementName  + ".fun"; 

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

            List<EquippableItemPrefabAssetData> equitabbleItemList = formatter.Deserialize(stream) as List<EquippableItemPrefabAssetData>;
            stream.Close();

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

    private static void Load() 
    {

    }

}