using Exam.Infrastructure.Extensions;
using Exam.Infrastructure.Validation.Validation;
using FluentValidation;
using Hotline.Exams.ExamManages;
using Hotline.Exams.Validate;
using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
using Hotline.Repository.SqlSugar.Exam.Validate;

namespace Exam.Repository.Sqlsugar.Validators.ExamManages
{
    public class ExtractRuleValidator : BaseValidator<ExamExtractRule>
    {
        private readonly IExtractRuleRepository _extractRuleRepository;

        public ExtractRuleValidator(IExtractRuleRepository extractRuleRepository)
        {
            RuleSet(ValidatorTypeConstants.Create, () =>
            {
                BaseValidateRule();

                ValidateRuleWithAdd();
            });

            RuleSet(ValidatorTypeConstants.Modify, () =>
            {
                BaseValidateRule();

                ValidateRuleWithModify();
            });

            _extractRuleRepository = extractRuleRepository;
        }

        protected override void BaseValidateRule()
        {
            base.BaseValidateRule();

            RuleFor(m => m.RuleType).NotNull().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamExtractRule.RuleType))));
            RuleFor(m => m.Name).NotEmpty().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamExtractRule.Name))));
            RuleFor(m => m.Code).NotEmpty().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamExtractRule.Code))));

        }

        protected override void ValidateRuleWithAdd()
        {
            base.ValidateRuleWithAdd();
            RuleFor(m => m.CreationTime).NotEmpty().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamExtractRule.CreationTime))));

            RuleFor(m => m.Name).Must(v => !_extractRuleRepository.Queryable().Any(x => x.Name == v)).WithMessage(x => string.Format(ExamErrorMessage.IsRepeat, x.GetType().GetDescription(nameof(ExamExtractRule.Name))));

            RuleFor(m => m.Code).Must(v => !_extractRuleRepository.Queryable().Any(x => x.Code == v)).WithMessage(x => string.Format(ExamErrorMessage.IsRepeat, x.GetType().GetDescription(nameof(ExamExtractRule.Code))));
        }

        protected override void ValidateRuleWithModify()
        {
            base.ValidateRuleWithModify();
            RuleFor(m => m.LastModificationTime).NotEmpty().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamExtractRule.LastModificationTime))));
            RuleFor(m => m.Name).Must((e,v) => !_extractRuleRepository.Queryable().Any(x => x.Name == v && x.Id!= e.Id)).WithMessage(x => string.Format(ExamErrorMessage.IsRepeat, x.GetType().GetDescription(nameof(ExamExtractRule.Name))));

            RuleFor(m => m.Code).Must((e,v) => !_extractRuleRepository.Queryable().Any(x => x.Code == v && x.Id!=e.Id)).WithMessage(x => string.Format(ExamErrorMessage.IsRepeat, x.GetType().GetDescription(nameof(ExamExtractRule.Code))));

        }

    }
}
