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

namespace Exam.Repository.Sqlsugar.Validators.TestPapers
{
    public class TestPaperValidator:BaseValidator<ExamTestPaper>
    {
        private readonly ITestPaperRepository _testPaperRepository;

        public TestPaperValidator(ITestPaperRepository testPaperRepository)
        {
            RuleSet(ValidatorTypeConstants.Create, () =>
            {
                BaseValidateRule();

                ValidateRuleWithAdd();
            });

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

                ValidateRuleWithModify();
            });
            _testPaperRepository = testPaperRepository;
        }

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

            RuleFor(m => m.Name).NotEmpty().WithMessage(x=>string.Format(ExamErrorMessage.IsRequired,x.GetType().GetDescription(nameof(ExamTestPaper.Name))));
            RuleFor(m => m.Code).NotEmpty().WithMessage(x=>string.Format(ExamErrorMessage.IsRequired,x.GetType().GetDescription(nameof(ExamTestPaper.Code))));
            RuleFor(m => m.ExamType).NotNull().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamTestPaper.ExamType))));
            RuleFor(m => m.Mode).NotNull().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamTestPaper.Mode))));
        }

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

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

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

        }

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

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

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

        }

    }
}
