using Exam.Infrastructure.Extensions;
using Exam.Infrastructure.Validation.Validation;
using FluentValidation;
using Hotline.Exams.ExamManages;
using Hotline.Repository.SqlSugar.Exam.Core.Constants;
using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
using Hotline.Repository.SqlSugar.Exam.Validate;
using ExamErrorMessage = Hotline.Exams.Validate.ExamErrorMessage;

namespace Exam.Repository.Sqlsugar.Validators.ExamManages
{
    public class UserExamValidator:BaseValidator<ExamUserExam>
    {
        private readonly IUserExamRepository _userExamRepository;

        public UserExamValidator(IUserExamRepository userExamRepository)
        {
            RuleSet(ValidatorTypeConstants.Create, () =>
            {
                BaseValidateRule();

                ValidateRuleWithAdd();
            });

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

                ValidateRuleWithModify();
            });

            RuleSet(ValidatorTypeConstants.Remove, () =>
            {              

                ValidateRuleWithRemove();
            });
            this._userExamRepository = userExamRepository;
        }

        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(ExamUserExam.CreationTime))));
        }

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

        }

        private void ValidateRuleWithRemove()
        {
            RuleFor(m => m.Id).Must(v=>!_userExamRepository.Queryable().Where(x=>x.Id == v && x.ExamStatus != Hotline.Share.Enums.Exams.EExamStatus.NoStart).Any()).WithMessage(Hotline.Repository.SqlSugar.Exam.Core.Constants.ExamErrorMessage.DeleteUnableUserExam);

        }

    }
}
