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

namespace YGNT.QuestionLibrary.Papers.Dto
{
    /// <summary>
    /// 自动组卷组卷类型设置
    /// </summary>
    public class AutoPaperTypeSettingDto : IValidatableObject
    {
        /// <summary>
        /// 章节id或知识点id
        /// </summary>
        [Required]
        public long Id { get; set; }

        /// <summary>
        /// 学科下细分题型id
        /// </summary>
        [Required]
        public int DisciplineQuestionTypeId { get; set; }

        /// <summary>
        /// 题量
        /// </summary>
        [Required]
        public int Count { get; set; }

        /// <summary>
        /// 分值
        /// </summary>
        [Required]
        public float Score { get; set; }

        /// <summary>
        /// 自动组卷题型难度设置
        /// </summary>
        [Required]
        public List<AutoPaperTypeDifficultySettingDto> DifficultySettings { get; set; }

        /// <summary>
        /// 模型验证
        /// </summary>
        /// <param name="validationContext"></param>
        /// <returns></returns>
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var group = DifficultySettings.GroupBy(x => x.Difficulty).ToList();
            if (group.Any(x => x.Count() > 1))
            {
                yield return new ValidationResult("难度不能有重复");
            }
            if (DifficultySettings.Sum(x => x.Count) != Count)
                yield return new ValidationResult("难度题量和总题量不一致");
        }
    }


    /// <summary>
    /// 自动组卷题型难度设置
    /// </summary>
    public class AutoPaperTypeDifficultySettingDto
    {
        /// <summary>
        /// 难度
        /// </summary>
        public Difficulty Difficulty { get; set; }

        /// <summary>
        /// 题量
        /// </summary>
        public int Count { get; set; }
    }
}