using Exam.Infrastructure.Extensions;
using Exam.Infrastructure.Validation.Validation;
using FluentValidation;
using Hotline.Exams.ExamManages;
using Hotline.Exams.Practices;
using Hotline.Exams.Questions;
using Hotline.Exams.Sourcewares;
using Hotline.Exams.TestPapers;
using Hotline.Exams.Validate;
using Hotline.Repository.SqlSugar.DataPermissions;
using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
using Hotline.Repository.SqlSugar.Exam.Repositories;
using Hotline.Repository.SqlSugar.Exam.Validate;

namespace Exam.Repository.Sqlsugar.Validators.ExamManages
{
    public class ExamTagValidator : BaseValidator<ExamTag>
    {
        private readonly IExamTagRepository _examTagRepository;
        private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
        private readonly IServiceProvider _serviceProvider;
        public ExamTagValidator(IExamTagRepository examTagRepository, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider)
        {
            _examTagRepository = examTagRepository;
            _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
            _serviceProvider = serviceProvider;

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

                ValidateRuleWithAdd();
            });

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

                ValidateRuleWithModify();
            });

            RuleSet(ValidatorTypeConstants.Remove, () =>
            {
                ValidateRuleWithRemove();
            });
        }

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

        }

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

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

        private void ValidateRuleWithRemove()
        {
            RuleFor(m => m.Id).Must(v => !new ExamRepository<ExamQuestionTag>(_examTagRepository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Any(x => x.TagId == v)).WithMessage(x => string.Format(ExamErrorMessage.IsSimpleRefrence, typeof(ExamTag).GetDescription()));
            RuleFor(m => m.Id).Must(v => !new ExamRepository<ExamPracticeTag>(_examTagRepository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Any(x => x.TagId == v)).WithMessage(x => string.Format(ExamErrorMessage.IsSimpleRefrence, typeof(ExamTag).GetDescription()));
            RuleFor(m => m.Id).Must(v => !new ExamRepository<ExamRuleTag>(_examTagRepository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Any(x => x.TagId == v)).WithMessage(x => string.Format(ExamErrorMessage.IsSimpleRefrence, typeof(ExamTag).GetDescription()));
            RuleFor(m => m.Id).Must(v => !new ExamRepository<ExamTestPaperRuleTag>(_examTagRepository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Any(x => x.TagId == v)).WithMessage(x => string.Format(ExamErrorMessage.IsSimpleRefrence, typeof(ExamTag).GetDescription()));
        }

    }
}
