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

namespace Exam.Repository.Sqlsugar.Validators.Sourcewares
{
    public class SourcewareCategoryValidator : BaseValidator<ExamSourcewareCategory>
    {
        private readonly ISourcewareCategoryRepository _sourcewareCategoryRepository;
        private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
        private readonly IServiceProvider _serviceProvider;
        public SourcewareCategoryValidator(ISourcewareCategoryRepository sourcewareCategoryRepository, IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider)
        {
            _sourcewareCategoryRepository = sourcewareCategoryRepository;
            _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(ExamSourcewareCategory.Name))));
        }

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

        }

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

        private void ValidateRuleWithRemove()
        {
            RuleFor(m => m.Id).Must(v => !new ExamRepository<ExamSourceware>(_sourcewareCategoryRepository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Any(x => x.CategoryId == v)).WithMessage(x => x.Name.IsNullOrEmpty()? string.Format(ExamErrorMessage.IsSimpleRefrence, typeof(ExamSourcewareCategory).GetDescription()): string.Format(ExamErrorMessage.IsRefrence, typeof(ExamSourcewareCategory).GetDescription(), x.Name));
        }
    }
}
