using BaseDomain;
using System.Net.Mail;

namespace VCart.Domain
{
    public class Email : AValueObject
    {
        public Email(string email) 
        {
            this.Value = email;
        }
        public string Value { get; private set; }

        public static implicit operator string(Email e) => e.Value;
        public static explicit operator Email(string email) => new Email(email);

        public override bool GetValidationExpression()
        {
            return IsEMailValid(this.Value);
        }

        protected override IEnumerable<object> GetEqualityComponents()
        {
            yield return this.Value;
        }

        private bool IsEMailValid(string email)
        {
            var valid = true;

            try
            {
                var emailAddress = new MailAddress(email);
            }
            catch
            {
                valid = false;
            }

            return valid;

        }
    }
}
