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

namespace Framework.Utility
{
    public class UnityFile
    {
        private static readonly string path = @"C:/Unity/CGS/Data/"; //Application.dataPath + @"/Data/";

        public static string GetPath()
        {
            return path;
        } 

        private string filePath;
        private string fileType;

        public UnityFile(string fileName)
        {
            this.fileType = "txt";
            this.filePath = path + fileName + "." + this.fileType;
        }

        public string GetFilePath()
        {
            return this.filePath;
        }

        public void Write(object data)
        {
            //Debug.Log(this.filePath);
            using (FileStream stream = new FileStream(this.filePath, FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, data);
                stream.Flush();
            }
        }

        public DataType Read<DataType>()
        {
            //Debug.Log(this.filePath);
            DataType data = default(DataType);
            using (FileStream stream = new FileStream(this.filePath, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                data = (DataType)formatter.Deserialize(stream);
                stream.Flush();
            }
            return data;
        }

        public bool Exists()
        {
            return File.Exists(this.filePath);
        }
    }
}
