using Framework.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;

namespace Framework.GuardianSaveSystem
{
    // TODO: Unfinished
    [AddComponentMenu("Scripts/Framework/Guardian Save System/Angel Level Saver")]
    public class AngelLevelSaver : CoreBehaviour
    {
        public new GameObject gameObject;

        private string path;
        private XmlSerializer serializer;

        protected override void InnerAwake()
        {
            path = Application.dataPath + "\\Data\\";
            if (!Directory.Exists(path))
            {
                Debug.Log("Create directory");
                Directory.CreateDirectory(path);
            }
            using (FileStream stream = new FileStream(path + gameObject.GetInstanceID() + ".txt", FileMode.Create))
            {
                //serializer = new XmlSerializer(gameObject.GetType());
                //serializer.Serialize(stream, gameObject);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, gameObject);
                stream.Flush();
            }
        }

        protected override void InnerUpdate()
        {
            if (Input.GetKeyDown(KeyCode.F))
            {
                using(FileStream stream = new FileStream(path + gameObject.GetInstanceID(), FileMode.Open))
                {
                    // TODO return
                    //BinaryFormatter formatter = new BinaryFormatter();
                    //GameObject data = (GameObject)formatter.Deserialize(stream);
                    stream.Flush();
                }
            }
        }
    }
}
