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

namespace Exam.Repository.Sqlsugar.Validators.Trains
{
    public class TrainTemplateValidator:BaseValidator<ExamTrainTemplate>
    {
        private readonly ITrainTemplateRepository _trainTemplateRepository;

        public TrainTemplateValidator(ITrainTemplateRepository trainTemplateRepository)
        {
            RuleSet(ValidatorTypeConstants.Create, () =>
            {
                BaseValidateRule();

                ValidateRuleWithAdd();
            });

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

                ValidateRuleWithModify();
            });
            this._trainTemplateRepository = trainTemplateRepository;
        }

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

        protected override void ValidateRuleWithAdd()
        {
            base.ValidateRuleWithAdd();
            RuleFor(m => m.CreationTime).NotEmpty().WithMessage(x => string.Format(ExamErrorMessage.IsRequired, x.GetType().GetDescription(nameof(ExamTrainTemplate.CreationTime))));
            RuleFor(m => m.Name).Must(v => !_trainTemplateRepository.Queryable().Any(x => x.Name == v)).WithMessage(x => string.Format(ExamErrorMessage.IsRepeat, x.GetType().GetDescription(nameof(ExamTrainPlan.Name))));
            RuleFor(m => m.Code).Must(v => !_trainTemplateRepository.Queryable().Any(x => x.Code == v)).WithMessage(x => string.Format(ExamErrorMessage.IsRepeat, x.GetType().GetDescription(nameof(ExamTrainPlan.Code))));
        }

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

        }

    }
}
