using Exam.Infrastructure.Validation.Constants;

namespace Exam.Infrastructure.Validation.Validation
{
    /// <summary>
    /// 验证错误
    /// </summary>
    [Serializable]
    public class ValidationErrors : IValidationErrors
    {

        private readonly List<ValidationErrorItem> _errorItemList = new List<ValidationErrorItem>();

        public bool IsValid
        {
            get
            {
                return _errorItemList.Count == 0;
            }
        }

        /// <summary>
        /// 错误明细
        /// </summary>
        public IEnumerable<ValidationErrorItem> ErrorItems { get { return _errorItemList; } }

        /// <summary>
        /// 添加系统错误
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="errorMessage"></param>
        /// <param name="attemptedValue"></param>
        /// <param name="customState"></param>
        /// <returns></returns>
        public IValidationErrors AddSystemError(string propertyName = InfrastructureConstant.SystemError, string errorMessage = InfrastructureConstant.SystemErrorDescription,
            object attemptedValue = null, bool customState = false)
        {
            return AddError(propertyName, errorMessage, attemptedValue, customState);
        }

        /// <summary>
        /// 添加错误
        /// </summary>
        /// <param name="errorItems"></param>
        /// <returns></returns>
        public IValidationErrors AddErrors(IEnumerable<ValidationErrorItem> errorItems)
        {
            if (errorItems == null)
            {
                return this;
            }
            foreach (var errorItem in errorItems)
            {
                if (errorItem.CustomState)
                {
                    errorItem.PropertyName = "IsAlter";
                }
                _errorItemList.Add(errorItem);
            }

            return this;
        }

        /// <summary>
        /// 添加错误
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="attemptedValue"></param>
        /// <param name="errorMessage"></param>
        /// <param name="customState"></param>
        /// <param name="validateKey"></param>
        /// <returns></returns>
        public IValidationErrors AddError(string propertyName, object attemptedValue, string errorMessage = null, bool customState = false, string validateKey = "")
        {
            var errorItem = new ValidationErrorItem(propertyName, errorMessage, attemptedValue, customState, validateKey);
            _errorItemList.Add(errorItem);
            return this;
        }

        /// <summary>
        /// 添加错误
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="errorMessage"></param>
        /// <param name="attemptedValue"></param>
        /// <param name="customState"></param>
        /// <returns></returns>
        public IValidationErrors AddError(string propertyName, string errorMessage, object attemptedValue, bool customState = false)
        {
            var errorItem = new ValidationErrorItem(propertyName, errorMessage, attemptedValue, customState);
            _errorItemList.Add(errorItem);
            return this;
        }

        /// <summary>
        /// 添加领域业务错误
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="errorMessage"></param>
        /// <param name="errorCode"></param>
        /// <param name="errorDescription"></param>
        /// <param name="attemptedValue"></param>
        /// <returns></returns>
        public IValidationErrors AddDomainError(string propertyName, string errorMessage, string errorCode = null, string errorDescription = null, object attemptedValue = null)
        {
            var errorItem = new ValidationErrorItem(propertyName, errorMessage, attemptedValue, false, errorCode);
            _errorItemList.Add(errorItem);
            return this;
        }
    }
}
