using Expressive;
using System;
using System.Dynamic;
using System.Linq;

namespace InABox.Core
{
    public interface IEntityLink : ISubObject
    {
        Guid ID { get; set; }
        
        Guid Deleted { get; set; }

        bool Clear();
        bool IsValid();
    }

    public interface IEntityLink<T> : IEntityLink
    {
    }

    public abstract class EntityLink<T> : BaseObject, IEntityLink<T> where T : Entity, new()
    {
        /*
        private Func<BaseObject>? _linkedentity;
        
        [DoNotSerialize]
        protected BaseObject? LinkedEntity() => _linkedentity?.Invoke();*/

        private BaseObject? _linkedParent;

        private string? _linkedPath;

        public void SetLinkedParent(BaseObject parent)
        {
            _linkedParent = parent;
        }

        public void SetLinkedPath(string path)
        {
            _linkedPath = path;
        }

        public BaseObject? GetLinkedParent() => _linkedParent;

        public string? GetLinkedPath() => _linkedPath;

        protected override IOriginalValues CreateOriginalValues()
        {
            return new SubObjectOriginalValues(this);
        }

        protected override ILoadedColumns CreateLoadedColumns()
        {
            return new SubObjectLoadedColumns(this);
        }

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

        public EntityLink(Func<BaseObject>? entity, string name) 
        {
            _linkedentity = entity;
        }*/

        [NullEditor]
        public abstract Guid ID { get; set; }
        
        [NullEditor]
        [Obsolete]
        public Guid Deleted { get; set; }

        /// <summary>
        /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
        /// </summary>
        /// <param name="other">The link to copy data from.</param>
        public void CopyFrom(IEntityLink<T> other)
        {
            ID = other.ID;
            this.Synchronise(other);
        }

        /// <summary>
        /// Basically do the same as <see cref="Synchronise(object)"/>, but also copy the <see cref="ID"/>.
        /// </summary>
        /// <param name="other">The link to copy data from.</param>
        public void CopyFrom(T other)
        {
            ID = other.ID;
            this.Synchronise(other);
        }

        public bool Clear()
        {
            var result = false;
            var props = CoreUtils.PropertyList(GetType(), x => !x.Name.Equals("ID"), false);

            foreach (var prop in DatabaseSchema.Properties(GetType()))
            {
                if (prop.Name == "ID" || !(prop is StandardProperty stdProp) || stdProp.Property.DeclaringType.Equals(typeof(BaseObject))) continue;

                var entityProp = DatabaseSchema.Property(typeof(T), prop.Name);
                if(entityProp != null && entityProp.PropertyType == stdProp.PropertyType)
                {
                    var oldValue = prop.Getter()(this);
                    var newValue = CoreUtils.GetDefault(prop.PropertyType);
                    if(!object.Equals(oldValue, newValue))
                    {
                        result = true;
                        prop.Setter()(this, newValue);
                    }
                }
            }

            return result;
        }

        protected override void DoPropertyChanged(string name, object? before, object? after)
        {
            LinkedProperties.GetParent(this)?.CascadePropertyChanged(LinkedProperties.GetPath(this) + "." + name, before, after);

            if (LinkedProperties.Find(this, name, out var link, out var parent))
            {
                link.Update(this, parent);
            }
        }

        public bool IsValid() => !ID.Equals(Guid.Empty);
    }
}