using Entities.DTO;

namespace TechHelper.Client.Exam
{
	public class AssignmentCheckData
	{
		public string Title { get; set; }
		public Guid AssignmentId { get; set; }
		public Guid StudentId { get; set; }
		public List<AssignmentCheckQuestion> Questions { get; set; } = new List<AssignmentCheckQuestion>();
	}


	public class AssignmentCheckQuestion
	{
		public string Sequence { get; set; } = string.Empty;
		public ExamQuestionDto ExamQuestionDto { get; set; } = new ExamQuestionDto();
		public float Score { get; set; }
	}

	public class Student
	{
		public Guid Id { get; set; } = Guid.NewGuid();
		public string Name { get; set; } = string.Empty;
	}

	public class QuestionAnswerStatus
	{
		public string QuestionSequence { get; set; } = string.Empty;    // 题目序号，例如 "1.1"
		public string QuestionText { get; set; } = string.Empty;        // 题目文本
		public float QuestionScore { get; set; }                        // 题目分值
		public Dictionary<Guid, bool> StudentCorrectStatus { get; set; } = new Dictionary<Guid, bool>();
		// Key: Student.Id, Value: true 表示正确，false 表示错误
	}

	public class QuestionRowData
	{
		public AssignmentCheckQuestion QuestionItem { get; set; } // 原始题目信息
		public Dictionary<Guid, bool> StudentAnswers { get; set; } = new Dictionary<Guid, bool>();
	}


	public static class ExamStructExtensions
	{
		public static AssignmentCheckData GetStruct(this ExamDto dto)
		{
			if (dto == null)
			{
				return new AssignmentCheckData { Title = "无效试卷", Questions = new List<AssignmentCheckQuestion>() };
			}

			var examStruct = new AssignmentCheckData
			{
				Title = dto.Title
			};

			GetSeqRecursive(dto.ExamStruct, null, examStruct.Questions);

			return examStruct;
		}

		/// <summary>
		/// 递归方法，用于生成所有题目和子题目的完整序号。
		/// </summary>
		/// <param name="currentGroup">当前正在处理的题目组。</param>
		/// <param name="parentSequence">当前题目组的父级序号（例如："1", "2.1"）。如果为空，则表示顶级题目组。</param>
		/// <param name="allQuestions">用于收集所有生成题目项的列表。</param>
		private static void GetSeqRecursive(
			ExamQuestionDto currentGroup,
			string? parentSequence,
			List<AssignmentCheckQuestion> allQuestions)
		{
			string currentGroupSequence = parentSequence != null
										? $"{parentSequence}.{currentGroup.Index}"
										: currentGroup.Index.ToString();

			foreach (var subGroup in currentGroup.ChildExamQuestions)
			{
				GetSeqRecursive(subGroup, currentGroupSequence, allQuestions);
			}
			if (!string.IsNullOrEmpty(currentGroup.Sequence))
			{

				allQuestions.Add(new AssignmentCheckQuestion
				{
					ExamQuestionDto = currentGroup,
					//Sequence = currentGroupSequence,
					Sequence = currentGroup.Sequence,
					Score = currentGroup.Score,
				});
			}
		}
	}
}
