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

namespace EgtBEAMWALL.DataLayer
{
    public static partial class LinqExtensions
    {
        #region Public Methods

        public static IEnumerable<T> ExceptUsingJSonCompare<T>
            (this IEnumerable<T> first, IEnumerable<T> second)
        {
            return first.Except(second, new JSonEqualityComparer<T>());
        }

        #endregion Public Methods
    }

    /// <summary>
    /// Comparer generico basato su Json
    /// https://stackoverflow.com/questions/7042090/linq-except-with-custom-iequalitycomparer/7098076#7098076
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class JSonEqualityComparer<T> : IEqualityComparer<T>
    {
        #region Public Methods

        public bool Equals(T x, T y)
        {
            return String.Equals
            (
                Newtonsoft.Json.JsonConvert.SerializeObject(x),
                Newtonsoft.Json.JsonConvert.SerializeObject(y)
            );
        }

        public int GetHashCode(T obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj).GetHashCode();
        }

        #endregion Public Methods
    }
}