using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace CoviDok.BLL
{
    public class PropertyCopier<T> where T : class
    {
        public static void Copy(T source, T dest, string[] except = null)
        {
            List<string> forbidden = new List<string>();
            if (except != null)
            {
                forbidden.AddRange(except);
            }
            PropertyInfo[] properties = source.GetType().GetProperties();
            foreach (var property in properties)
            {
                if (property.Name.ToLower() != "id" && !forbidden.Contains(property.Name.ToLower())) {
                    var val = property.GetValue(source);
                    if (val != null)
                    {
                        property.SetValue(dest, val);
                    }                    
                }
            }
        }
    }

    public class PropertyCopier<TSource, TDest> where TSource : class
                                            where TDest : class
    {
        public static void Copy(TSource source, TDest dest)
        {
            var sourceProperties = source.GetType().GetProperties();
            var destProperties = dest.GetType().GetProperties();

            foreach (var sourceProperty in sourceProperties)
            {
                foreach (var destProperty in destProperties)
                {
                    if (sourceProperty.Name == destProperty.Name && sourceProperty.PropertyType == destProperty.PropertyType)
                    {
                        if (sourceProperty.Name.ToLower() != "id")
                        {
                            var val = sourceProperty.GetValue(source);
                            if (val != null) destProperty.SetValue(dest, val);
                        }
                        break;
                    }
                }
            }
        }
    }
}