using System;
using System.Linq;

namespace InABox.Core
{
    
    public interface IEnclosedEntity
    {
    }
    
    public abstract class EnclosedEntity : BaseObject, IEnclosedEntity
    {
        
        private Func<BaseObject>? _linkedentity;
        
        [DoNotSerialize]
        protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();

        //[Obsolete("Please supply linked Entity")]
        public EnclosedEntity()
        {
        }

        public EnclosedEntity(Func<BaseObject>? entity) 
        {
            _linkedentity = entity;
        }
        
        protected override void DoPropertyChanged(string name, object before, object after)
        {
            var le = LinkedEntity();
            if (IsObserving() && le != null)
            {
                var link = LinkedProperties.Find(le, this).FirstOrDefault(x => x.Source.Equals(name));
                if (link != null)
                    link.Update(this, le);
            }
        }
    }
}