using Framework.Entities;
using Framework.Utility;
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
{
    /// <summary>
    /// This component should be attached to an empty GameObject in every scene that can be 
    /// saved and loaded from. It provides a component view for saving and loading level data.
    /// </summary>
    [AddComponentMenu("Scripts/Framework/Guardian Save System/Level Saver")]
    public class LevelSaver : CoreBehaviour
    {
        public int index; 
        private string path;
        //private Moment moment;
        
        protected override void InnerAwake()
        {
            //SaveLevel();
        }

        protected override void InnerUpdate()
        {
            if (Input.GetKeyDown(KeyCode.F))
            {
                Log("Loading...");
                LoadLevel();
            }
            if (Input.GetKeyDown(KeyCode.R))
            {
                Log("Saving...");
                SaveLevel();
            }
        }

        private void SaveLevel()
        {
            //Moment moment = new Moment("Moment_" + DateTime.Now.ToString("ddmmyy-hhMMss"));
            Moment moment = new Moment("Save");
            moment.SaveScene();
        }

        private void LoadLevel()
        {
            DirectoryInfo newestMomentDirectory = new DirectoryInfo(UnityFile.GetPath()).GetDirectories()
                       .OrderByDescending(d => d.LastWriteTimeUtc).First();
            Moment moment = new Moment(newestMomentDirectory.Name);
            moment.LoadScene();
        }

        private new void Log(string message)
        {
            Debug.Log(message);
        }
    }
}
