using System;
using System.Globalization;
using System.Windows.Controls;
using System.Net.Mail;

namespace Mercat_1
{
    /// <summary>
    /// Rule for email
    /// </summary>
    public class EmailValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);

            if (string.IsNullOrEmpty(value.ToString()))
            {
                result = new ValidationResult(false, "Email obligatori.");
            }
            else
            {
                try
                {
                    new MailAddress(value.ToString());
                }
                catch (Exception)
                {
                    result = new ValidationResult(false, "Si us plau introdueix un email valid.");
                }
            }

            return result;
        }
    }

    /// <summary>
    /// Rule for num
    /// </summary>
    public class NumValidationRule : ValidationRule
    {
        public int Min { get; set; }
        public int Max { get; set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            var num = 0;

            ValidationResult result = new ValidationResult(true, null);
            if (string.IsNullOrEmpty(value.ToString()))
            {
                result = new ValidationResult(false, "Quantitat es obligatoria");
            }
            else
            {
                try
                {
                    if (((string)value).Length > 0)
                        num = int.Parse((string)value);
                }
                catch (Exception e)
                {
                    result = new ValidationResult(false, "Caracters no permesos o : " + e.Message);
                }

                if ((num < Min) || (num > Max))
                {
                    result = new ValidationResult(false, "Introdueix un numero entre: " + Min + " - " + Max + ".");
                }
            }

            return result;
        }
    }

    /// <summary>
    /// Rule for name
    /// </summary>
    public class NameValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);

            if (string.IsNullOrEmpty(value.ToString()))
            {
                result = new ValidationResult(false, "Nom de l'empresa es obligatori.");
            }
 
            return result;
        }
    }

    public class DateValidation : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);
            if (value is null)
            {
                result = new ValidationResult(false, "Data d'entrada obligatoria.");
            }
            return result;
        }
    }
}
