using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using YGNT.QuestionLibrary.Core.Models;

namespace YGNT.QuestionLibrary.Papers.Dto
{
    /// <summary>
    /// 自动组卷设置
    /// </summary>
    public class AutoPaperSettingDto : IValidatableObject
    {
        /// <summary>
        /// 试卷总分
        /// </summary>
        [Required]
        public float TotalScore { get; set; }

        /// <summary>
        /// 自动组卷学科设置
        /// </summary>
        [Required]
        public List<AutoPaperDisciplineSettingDto> AutoPaperDisciplineSettings { get; set; }

        /// <summary>
        /// 组卷模式
        /// </summary>
        [Required]
        public PaperCompositionModel PaperCompositionModel { get; set; }


        /// <summary>
        /// 验证分值设置是否正确
        /// </summary>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var totalScore = 0f;
            foreach (var disciplineSetting in AutoPaperDisciplineSettings)
            {
                var disciplineTotalScore = disciplineSetting.GetDisciplineTotalScore();
                if (disciplineTotalScore != disciplineSetting.TotalScore)
                    yield return new ValidationResult($"学科id:{disciplineSetting.DisciplineId}的分值设置不正确!",
                        new[] { "AutoPaperDisciplineSettings.TotalScore" });
                totalScore += disciplineTotalScore;
            }
            if (totalScore != TotalScore)
                yield return new ValidationResult($"试卷总分的分值设置不匹配!", new[] { nameof(TotalScore) });
        }
    }
}