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

namespace BaseDomain
{
    public abstract class AValueObject
    {
        protected static bool EqualOperator(AValueObject left, AValueObject right)
        {
            if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
            {
                return false;
            }
            return ReferenceEquals(left, right) || left.Equals(right);
        }

        protected static bool NotEqualOperator(AValueObject left, AValueObject right)
        {
            return !EqualOperator(left, right);
        }
        protected abstract IEnumerable<object> GetEqualityComponents();

        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType() != GetType())
            {
                return false;
            }

            var other = (AValueObject)obj;

            return GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
        }

        public static bool operator ==(AValueObject one, AValueObject two)
        {
            return EqualOperator(one, two);
        }

        public static bool operator !=(AValueObject one, AValueObject two)
        {
            return NotEqualOperator(one, two);
        }

        public override int GetHashCode()
        {
            return GetEqualityComponents()
                .Select(x => x != null ? x.GetHashCode() : 0)
                .Aggregate((x, y) => x ^ y);
        }

        public Func<bool> GetValidationFunc() => GetValidationExpression;
        public abstract bool GetValidationExpression();

        public const string ErrorMessage = "";

        public bool IsValid { get; protected set; }
    }
}
