/*
<copyright file="BGGCLoad.cs" company="BansheeGz">
    Copyright (c) 2018-2021 All Rights Reserved
</copyright>
*/

using System.IO;
using GameCreator.Core;
using GameCreator.Variables;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;

#endif

namespace BansheeGz.BGDatabase
{
    /// <summary>
    /// support for saveload addon https://www.bansheegz.com/BGDatabase/Addons/SaveLoad/ 
    /// </summary>
    public class BGGCLoad : BGGCFileBasedA
    {
        protected override string FileExt => "sav";

        public override bool InstantExecute(GameObject target, IAction[] actions, int index)
        {
            var path = GetPath(target);
            if (string.IsNullOrEmpty(path))
            {
                Debug.Log("WARNING! BGDatabase: can not load a game, cause filename is null!");
                return true;
            }

            if (!File.Exists(path))
            {
                Debug.Log("WARNING! BGDatabase: can not load a game, cause file does not exist!");
                return true;
            }

            var save = File.ReadAllBytes(path);
            BGRepo.I.Addons.Get<BGAddonSaveLoad>().Load(save);
            return true;
        }
        // +--------------------------------------------------------------------------------------+
        // | EDITOR                                                                               |
        // +--------------------------------------------------------------------------------------+

#if UNITY_EDITOR
        public static new string NAME = "BansheeGz/Load";
        protected override string Title => "BGDatabase: Load";
#endif
    }
}