using MainShell.Filewritable;
using MwFramework.ManagerService;
using MXJM.FileWritable;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace MainShell.Recipe.Models
{
    public abstract class RecipeBase :JsonFileWritableBase,IRecipe , INotifyPropertyChanged
    {
        private string _recipeName;
        public virtual string RecipeName
        {
            get => _recipeName;
            set
            {
                if (_recipeName != value)
                {
                    string oldName = _recipeName;
                    _recipeName = value;
                    OnRenamed?.Invoke(this, oldName, _recipeName);
                    OnPropertyChanged();
                }
            }
        }

        public event Action<IRecipe, string, string> OnRenamed;
        #region
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 通用的 SetProperty 辅助方法，派生类在属性 setter 中可使用以简化通知
        /// 用法： SetProperty(ref _field, value);
        /// </summary>
        protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value)) return false;
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public string CreateSnapshot()
        {
            var settings = new JsonSerializerSettings
            {
                Formatting = Formatting.None,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                ContractResolver = IgnorePropertiesResolver
            };

            return JsonConvert.SerializeObject(this, settings);
        }
        #endregion
        protected RecipeBase(string name)
        {
            _recipeName = name;
        }
        protected RecipeBase()
        {
        }
       
    }
}
