using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using Hydra.Framework.Helpers;

namespace Hydra.Framework.Validation
{
    /// <summary>
    /// Property Validator
    /// </summary>
    public class PropertyValidator
        : IValidator<object>
    {
        #region Member Variables
        /// <summary>
        /// Validate Attribute
        /// </summary>
        private readonly ValidateAttribute attribute;

        /// <summary>
        /// Property Information
        /// </summary>
        private readonly PropertyInfo propertyInfo;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyValidator"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        public PropertyValidator(PropertyInfo info)
        {
            propertyInfo = info;
            object[] attributes = info.GetCustomAttributes(typeof(ValidateAttribute), true);
            Check.That(attributes, Is.Not.Null);

            attribute = attributes[0] as ValidateAttribute;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyValidator"/> class.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <param name="attribute">The attribute.</param>
        public PropertyValidator(PropertyInfo info, ValidateAttribute attribute)
        {
            this.propertyInfo = info;
            this.attribute = attribute;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the info.
        /// </summary>
        /// <value>The info.</value>
        public PropertyInfo Info
        {
            get
            {
                return propertyInfo;
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Determines whether the specified entity is valid.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="violations">The violations.</param>
        /// <returns>
        /// 	<c>true</c> if the specified entity is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(object entity, out IEnumerable<IViolation> violations)
        {
            List<IViolation> violationList = new List<IViolation>();
            object obj = propertyInfo.GetValue(entity, BindingFlags.Default, null, null, CultureInfo.InvariantCulture);
            bool valid = ValidateProperty(obj, violationList);
            violations = violationList;

            return valid;
        }
        #endregion

        #region Private / Protected Methods
        /// <summary>
        /// Validates the property.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="violationList">The violation list.</param>
        /// <returns></returns>
        protected virtual bool ValidateProperty(object obj, List<IViolation> violationList)
        {
            if (obj == null)
            {
                if (attribute.AllowNull == false)
                {
                    violationList.Add(new NullValueViolation(Info.Name));
                    return false;
                }
            }
            else
            {
                if (propertyInfo.PropertyType == typeof(string))
                {
                    string value = (string)obj;
                    if (value.Length == 0 &&
                        !attribute.AllowEmpty)
                    {
                        violationList.Add(new EmptyValueViolation(Info.Name));
                        return false;
                    }

                    if (value.Length < attribute.MinLength)
                    {
                        violationList.Add(new InvalidFormatViolation(Info.Name, "must be at least " + attribute.MinLength + " characters"));
                        return false;
                    }

                    if (value.Length > attribute.MaxLength)
                    {
                        violationList.Add(new InvalidFormatViolation(Info.Name, "must be no more than " + attribute.MaxLength + " characters"));
                        return false;
                    }
                }
            }

            return true;
        }
        #endregion
    }
}