using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace InABox.Core
{
    public static class LinkedProperties
    {

        private static readonly List<ILinkedProperty> _LinkedProperties = new List<ILinkedProperty>();

        public static void Register<TLinkedEntity, TEntityLink, TType>(Expression<Func<TLinkedEntity,TEntityLink>> path, Expression<Func<TEntityLink, TType>> source,
            Expression<Func<TLinkedEntity, TType>> target)
        {
            var map = new LinkedProperty<TLinkedEntity, TEntityLink, TType>(path, source, target);
            if (!_LinkedProperties.Any(x => x.Equals(map)))
                _LinkedProperties.Add(map);
        }

        public static IEnumerable<ILinkedProperty> Find(object parent, object path)
        {
            return _LinkedProperties.Where(x => (x.Type == parent.GetType()) && (CoreUtils.GetPropertyValue(parent,x.Path) == path));
        }
        
    }
}