using System.Reflection;
using Xunit.Sdk;

namespace AsbCloudWebApi.IntegrationTests;

public static class MatchHelper
{
   public static void Match<T>(T expected, T actual, IEnumerable<string>? excludeProps = null)
   {
      if (ReferenceEquals(expected, actual))
         throw EqualException.ForMismatchedValues(expected, actual, "Reference are equals");

      if (expected is null || actual is null)
         throw EqualException.ForMismatchedValues(expected, actual);

      var type = typeof(T);
      var props = type.GetProperties(
         BindingFlags.Public
         | BindingFlags.Instance).Where(prop => prop.CanWrite);

      if (excludeProps is not null && excludeProps.Any())
         props = props.Where(prop => !excludeProps.Contains(prop.Name));

      foreach (var prop in props)
      {
         var objValue = prop.GetValue(expected);
         var anotherValue = prop.GetValue(actual);
         if (objValue != null && !objValue.Equals(anotherValue)) 
         {
            var banner = $" of {type.Name} props {prop.Name} ";
                throw EqualException.ForMismatchedValues(expected, actual, banner);
            }            
      }
   }
}