using Edu.Model.ViewModel.Exam;
using Edu.Model.ViewModel.Question;
using Edu.Repository.Exam;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB;

namespace Edu.Module.Exam
{
    /// <summary>
    /// 处理阅卷
    /// </summary>
    public partial class PaperModule
    {

        /// <summary>
        /// 学员答案仓储层对象
        /// </summary>
        private readonly RB_Examination_StudentDetailsRepository examination_StudentDetailsRepository = new RB_Examination_StudentDetailsRepository();

        /// <summary>
        /// 自动阅卷方法
        /// </summary>
        /// <param name="IsHalfScore">多选题未选全给一半分(1-是,0-否)</param>
        /// <param name="PaperId">试卷编号</param>
        /// <param name="stuList">学员答题列表</param>
        /// <returns></returns>
        public bool AutoReviewPaperModule(int IsHalfScore, int PaperId, List<RB_Examination_StudentDetails_ViewModel> stuList)
        {
            bool flag = false;
            List<RB_Examination_Details_ViewModel> questionList = examination_DetailsRepository.GetExaminationDetailsListRepository(new RB_Examination_Details_ViewModel()
            {
                QPaperIds = PaperId.ToString()
            });
            List<RB_Examination_StudentDetails_ViewModel> answerList = new List<RB_Examination_StudentDetails_ViewModel>();
            if (questionList != null && questionList.Count > 0)
            {
                foreach (var sItem in questionList)
                {
                    var answerModel = stuList?.Where(qitem => qitem.DetailsId == sItem.Id)?.FirstOrDefault();
                    if (answerModel != null && answerModel.Id > 0)
                    {
                        switch (sItem.QuestionTypeKey)
                        {
                            //单选题
                            case "single":
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = (sItem.Answer == answerModel.StundetAnswer) ? sItem.Score : 0
                                });
                                break;
                            //多选题
                            case "multiple":
                                if (IsHalfScore == 0)
                                {
                                    answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                    {
                                        Id = answerModel.Id,
                                        StudentScore = (sItem.Answer == answerModel.StundetAnswer) ? sItem.Score : 0
                                    });
                                }
                                else
                                {
                                    decimal multScore = 0;
                                    var oldAnswerList= Common.ConvertHelper.StringToFileList(sItem.Answer);
                                    var newAnswerList = Common.ConvertHelper.StringToFileList(answerModel.StundetAnswer);
                                    bool isExists = true;
                                    if (newAnswerList != null && newAnswerList.Count > 0)
                                    {
                                        foreach (var nItem in newAnswerList)
                                        {
                                            if (isExists)
                                            {
                                                if (!oldAnswerList.Contains(nItem))
                                                {
                                                    isExists = false;
                                                }
                                            }
                                        }
                                        if (sItem.Answer == answerModel.StundetAnswer)
                                        {
                                            multScore = sItem.Score;
                                        }
                                        else if (isExists)
                                        {
                                            multScore = sItem.Score / 2;
                                        }
                                    }
                                    answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                    {
                                        Id = answerModel.Id,
                                        StudentScore = multScore
                                    }); 
                                }
                                break;
                            //填空题
                            case "fill-in":

                                break;
                            //判断题
                            case "judge":
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = (sItem.Answer == answerModel.StundetAnswer) ? sItem.Score : 0
                                });
                                break;
                            //简答题
                            case "short-answer":

                                break;
                            //名词解释
                            case "noun-explanation":

                                break;
                            //论述题
                            case "essay-question":

                                break;
                            //计算题
                            case "calculation":

                                break;
                            //分录题
                            case "entry-problem":

                                break;
                            //资料题
                            case "data-question":

                                break;
                            //连线题
                            case "matching":
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = (sItem.Answer == answerModel.StundetAnswer) ? sItem.Score : 0
                                });
                                break;
                            //排序题
                            case "sorting-problem":
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = (sItem.Answer == answerModel.StundetAnswer) ? sItem.Score : 0
                                });
                                break;
                            //完型填空
                            case "cloze":
                                var cloze_AnswerList= Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.Answer.ToString());
                                var cloze_Stu_AnswerList= Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(answerModel.StundetAnswer.ToString());
                                decimal clozeScore = 0;
                                if (cloze_AnswerList != null && cloze_Stu_AnswerList != null 
                                    && cloze_AnswerList.Count > 0 && cloze_Stu_AnswerList.Count > 0 
                                    && cloze_AnswerList.Count == cloze_Stu_AnswerList.Count)
                                {
                                    for (var i = 0; i < cloze_AnswerList.Count; i++)
                                    {
                                        if (cloze_AnswerList[i].SubAnswer == cloze_Stu_AnswerList[i].SubAnswer)
                                        {
                                            clozeScore += cloze_AnswerList[i].SubScore;
                                            cloze_Stu_AnswerList[i].StudentScore = cloze_AnswerList[i].SubScore;
                                        }
                                    }
                                }
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = clozeScore,
                                    StundetAnswer=Common.Plugin.JsonHelper.Serialize(cloze_Stu_AnswerList)
                                });
                                break;
                            //阅读理解
                            case "reading-comprehensio":
                                var reading_AnswerList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.Answer.ToString());
                                var reading_Stu_AnswerList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(answerModel.StundetAnswer.ToString());
                                decimal readingScore = 0;
                                if (reading_AnswerList != null && reading_Stu_AnswerList != null
                                    && reading_AnswerList.Count > 0 && reading_Stu_AnswerList.Count > 0 
                                    && reading_AnswerList.Count == reading_Stu_AnswerList.Count)
                                {
                                    for (var i = 0; i < reading_AnswerList.Count; i++)
                                    {
                                        if (reading_AnswerList[i].SubQuestionKey != "fill-in" && reading_AnswerList[i].SubQuestionKey != "short-answer")
                                        {
                                            if (reading_AnswerList[i].SubAnswer == reading_Stu_AnswerList[i].SubAnswer)
                                            {
                                                readingScore += reading_AnswerList[i].SubScore;
                                                reading_Stu_AnswerList[i].StudentScore = reading_AnswerList[i].SubScore;
                                            }
                                        }     
                                    }
                                }
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = readingScore,
                                    StundetAnswer = Common.Plugin.JsonHelper.Serialize(reading_Stu_AnswerList)
                                });
                                break;
                            //口语题
                            case "spoken":

                                break;
                            //听力题
                            case "listening":
                                var listening_AnswerList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(sItem.Answer.ToString());
                                var listening_Stu_AnswerList = Common.Plugin.JsonHelper.DeserializeObject<List<SubAnswerItem>>(answerModel.StundetAnswer.ToString());
                                decimal listeningScore = 0;
                                if (listening_AnswerList != null && listening_Stu_AnswerList != null
                                    && listening_AnswerList.Count > 0 && listening_Stu_AnswerList.Count > 0
                                    && listening_AnswerList.Count == listening_Stu_AnswerList.Count)
                                {
                                    for (var i = 0; i < listening_AnswerList.Count; i++)
                                    {
                                        if (listening_AnswerList[i].SubQuestionKey != "fill-in"&& listening_AnswerList[i].SubQuestionKey != "short-answer")
                                        {
                                            if (listening_AnswerList[i].SubAnswer == listening_Stu_AnswerList[i].SubAnswer)
                                            {
                                                listeningScore += listening_AnswerList[i].SubScore;
                                                listening_Stu_AnswerList[i].StudentScore = listening_AnswerList[i].SubScore;
                                            }
                                        }
                                    }
                                }
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = listeningScore,
                                    StundetAnswer = Common.Plugin.JsonHelper.Serialize(listening_Stu_AnswerList)
                                });
                                break;
                            //公用选项题
                            case "sharing-choose":

                                break;
                            //其它题
                            case "other":

                                break;
                            //单选题-数字
                            case "single-number":
                                answerList.Add(new RB_Examination_StudentDetails_ViewModel()
                                {
                                    Id = answerModel.Id,
                                    StudentScore = (sItem.Answer == answerModel.StundetAnswer) ? sItem.Score : 0
                                });
                                break;
                        }
                    }
                }
            }
            if (answerList != null && answerList.Count > 0)
            {
                foreach (var item in answerList)
                {
                    Dictionary<string, object> fileds = new Dictionary<string, object>()
                    {
                        {nameof(RB_Examination_StudentDetails_ViewModel.StudentScore),item.StudentScore },
                        {nameof(RB_Examination_StudentDetails_ViewModel.IsMarking),1 },
                    };
                    if (!string.IsNullOrEmpty(item.StundetAnswer))
                    {
                        fileds.Add(nameof(RB_Examination_StudentDetails_ViewModel.StundetAnswer), item.StundetAnswer);
                    }
                    examination_StudentDetailsRepository.Update(fileds, new WhereHelper(nameof(RB_Examination_StudentDetails_ViewModel.Id), item.Id));
                }
            }
            return flag;
        }
    }
}