using Edu.Model.ViewModel.Question;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Module.Question
{
    /// <summary>
    /// 解析问题
    /// </summary>
    public class AnalysisQuestionTypeModule
    {
        /// <summary>
        /// 字母选项数组
        /// </summary>
        private static readonly string [] LetterArray= new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        /// <summary>
        /// 问题解析
        /// </summary>
        /// <param name="key">问题Key</param>
        /// <param name="data">选项内容</param>
        /// <param name="isNoAnswer">是否去掉答案</param>
        /// <param name="isOptionRandom">是否选项随机(1-随机选项)</param>
        /// <param name="isEdit">是否选项随机(编辑状态)</param>
        /// <returns></returns>
        public object ParsingQuestion(string key, string data,bool isNoAnswer=false,int isOptionRandom=0,bool isEdit=false)
        {
            var obj = new object();
            switch (key)
            {
                //单选题
                case "single":
                    obj = GetChooseOptionList(data, isOptionRandom, isNoAnswer, isEdit: isEdit);
                    break;
                //多选题
                case "multiple":
                    obj= GetChooseOptionList(data, isOptionRandom, isNoAnswer, isEdit: isEdit);
                    break;
                //填空题
                case "fill-in":
                    obj = GetFillInList(data, isNoAnswer);
                    break;
                //判断题
                case "judge":
                    obj = GetJudgeList(data,isNoAnswer);
                    break;
                //简答题
                case "short-answer":
                    obj = data;
                    break;
                //名词解释
                case "noun-explanation":
                    obj = data;
                    break;
                //论述题
                case "essay-question":
                    obj = data;
                    break;
                //计算题
                case "calculation":
                    obj = data;
                    break;
                //分录题
                case "entry-problem":
                    obj = GetFillInList(data,isNoAnswer);
                    break;
                //资料题
                case "data-question":
                    obj = GetFillInList(data, isNoAnswer);
                    break;
                //连线题
                case "matching":
                    obj = GetMatchingList(data, isNoAnswer);
                    break;
                //排序题
                case "sorting-problem":
                    obj = GetSortingProblemList(data,isNoAnswer);
                    break;
                //完型填空
                case "cloze":
                    obj = GetClozeList(data,isNoAnswer);
                    break;
                //阅读理解
                case "reading-comprehensio":
                    var readingList = Common.Plugin.JsonHelper.DeserializeObject<List<readingComprehensioItem>>(data);
                    if (readingList != null && readingList.Count > 0)
                    {
                        foreach (var item in readingList)
                        {
                            switch (item.QuestionKey)
                            {
                                //单选题
                                case "single":
                                    item.SubAnwser = GetChooseOptionList(item.SubAnwser.ToString(), 0, isNoAnswer);
                                    break;
                                //多选题
                                case "multiple":
                                    item.SubAnwser = GetChooseOptionList(item.SubAnwser.ToString(), 0, isNoAnswer);
                                    break;
                                //填空题
                                case "fill-in":
                                    item.SubAnwser = GetFillInList(item.SubAnwser.ToString(), isNoAnswer);
                                    break;
                                //判断题
                                case "judge":
                                    item.SubAnwser = GetJudgeList(item.SubAnwser.ToString(), isNoAnswer);
                                    break;
                                //简答题
                                case "short-answer":
                                    item.SubAnwser = GetShortAnswer(item.SubAnwser.ToString(), isNoAnswer);
                                    break;
                                //单选题(数字)
                                case "single-number":
                                    item.SubAnwser = GetChooseOptionList(item.SubAnwser.ToString(), 0, isNoAnswer);
                                    break;
                            }
                        }
                    }
                    obj = readingList;
                    break;
                //口语题
                case "spoken":
                    obj = data;
                    break;
                //听力题
                case "listening":
                    var listenList = Common.Plugin.JsonHelper.DeserializeObject<List<readingComprehensioItem>>(data);
                    if (listenList != null && listenList.Count > 0)
                    {
                        foreach (var item in listenList)
                        {
                            switch (item.QuestionKey)
                            {
                                //单选题
                                case "single":
                                    item.SubAnwser = GetChooseOptionList(item.SubAnwser.ToString(),0, isNoAnswer);
                                    break;
                                //多选题
                                case "multiple":
                                    item.SubAnwser = GetChooseOptionList(item.SubAnwser.ToString(), 0, isNoAnswer);
                                    break;
                                //填空题
                                case "fill-in":
                                    item.SubAnwser = GetFillInList(item.SubAnwser.ToString(), isNoAnswer);
                                    break;
                                //判断题
                                case "judge":
                                    item.SubAnwser = GetJudgeList(item.SubAnwser.ToString(), isNoAnswer);
                                    break;
                                //简答题
                                case "short-answer":
                                    item.SubAnwser = GetShortAnswer(item.SubAnwser.ToString(), isNoAnswer);
                                    break;
                                //单选题(数字)
                                case "single-number":
                                    item.SubAnwser = GetChooseOptionList(item.SubAnwser.ToString(), 0, isNoAnswer);
                                    break;
                            }
                        }
                    }
                    obj = listenList;
                    break;
                //共用选择题
                case "sharing-choose":
                    obj = GetShareChooseList(data, isNoAnswer);
                    break;
                //其它题
                case "other":
                    obj = data;
                    break;
                //单选题(数字)
                case "single-number":
                    obj = GetChooseOptionList(data, isOptionRandom, isNoAnswer,isSingleNum:true,isEdit:isEdit);
                    break;
            }
            return obj;
        }

        /// <summary>
        /// 获取选择题选项
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isOptionRandom"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetChooseOptionList(string data, int isOptionRandom, bool isNoAnswer, bool isSingleNum = false, bool isEdit = false)
        {
            var obj = new object();
            
                var singleList = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(data);
                var newList = new List<optionItem>();
                if (!isEdit)
                {
                    singleList.ForEach(item =>
                    {
                        item.ShowName = item.Name;
                    });
                }
                //选项随机
                if (isOptionRandom == 1)
                {
                    var tempList = singleList.OrderBy(qitem => Guid.NewGuid()).ToList();
                    for (var i = 0; i < tempList.Count; i++)
                    {
                        var item = new optionItem()
                        {
                            Name = tempList[i].Name,
                            Content = tempList[i].Content,
                            IsAnswer = tempList[i].IsAnswer
                        };
                        item.ShowName = isSingleNum ? (i + 1).ToString() : LetterArray[i];
                        newList.Add(item);
                    }
                }
                //去掉答案
                if (isNoAnswer)
                {
                    singleList.ForEach(item =>
                    {
                        item.IsAnswer = false;
                    });
                    newList.ForEach(item =>
                    {
                        item.IsAnswer = false;
                    });
                }
                if (isOptionRandom == 1)
                {
                    obj = newList;
                }
                else
                {
                    obj = singleList;
                }
            
            return obj;
        }

        /// <summary>
        /// 获取填空题填空列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetFillInList(string data, bool isNoAnswer)
        {
            var obj = new object();
            if (!string.IsNullOrEmpty(data))
            {
                var fillInList = Common.Plugin.JsonHelper.DeserializeObject<List<fillInItem>>(data);
                if (isNoAnswer)
                {
                    fillInList.ForEach(item =>
                    {
                        item.Content = "";
                    });
                }
                obj = fillInList;
            }
            else
            {
                obj = null;
            }
            return obj;
        }

        /// <summary>
        /// 获取判断题选项列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetJudgeList(string data, bool isNoAnswer)
        {
            var judgeList = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(data);
            if (isNoAnswer)
            {
                judgeList.ForEach(item =>
                {
                    item.IsAnswer = false;
                });
            }
            return judgeList;
        }

        /// <summary>
        /// 获取连线题列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetMatchingList(string data, bool isNoAnswer)
        {
            var matchingList = Common.Plugin.JsonHelper.DeserializeObject<List<List<matchingItem>>>(data);
            if (isNoAnswer)
            {
                if (matchingList != null && matchingList.Count > 2)
                {
                    matchingList[2].ForEach(item =>
                    {
                        item.Content = "";
                    });
                }
            }
            return matchingList;
        }

        /// <summary>
        /// 获取排序题列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetSortingProblemList(string data, bool isNoAnswer)
        {
            var sortingProblemList = Common.Plugin.JsonHelper.DeserializeObject<List<List<matchingItem>>>(data);
            if (isNoAnswer)
            {
                if (sortingProblemList != null && sortingProblemList.Count > 1)
                {
                    sortingProblemList[1].ForEach(item =>
                    {
                        item.Name = "";
                    });
                }
            }
            return sortingProblemList;
        }

        /// <summary>
        /// 完型填空选项列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetClozeList(string data, bool isNoAnswer)
        {
            var clozeList = Common.Plugin.JsonHelper.DeserializeObject<List<colzeItem>>(data);
            if (isNoAnswer)
            {
                if (clozeList != null && clozeList.Count > 0)
                {
                    clozeList.ForEach(item =>
                    {
                        item.OptionList.ForEach(subItem =>
                        {
                            subItem.IsAnswer = false;
                        });
                    });
                }
            }
            return clozeList;
        }

        /// <summary>
        /// 获取阅读理解、听力题简答题
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetShortAnswer(string data, bool isNoAnswer)
        {
          var shortAnswerList=  Common.Plugin.JsonHelper.DeserializeObject<List<fillInItem>>(data);
            if (isNoAnswer)
            {
                shortAnswerList.ForEach(item =>
                {
                    item.Content = "";
                });
            }
            return shortAnswerList;
        }

        /// <summary>
        /// 共用选项题列表
        /// </summary>
        /// <param name="data"></param>
        /// <param name="isNoAnswer"></param>
        /// <returns></returns>
        private object GetShareChooseList(string data, bool isNoAnswer)
        {
            var shareChooseList = new List<List<matchingItem>>();
            if (!string.IsNullOrEmpty(data))
            {
                shareChooseList = Common.Plugin.JsonHelper.DeserializeObject<List<List<matchingItem>>>(data);
            }
            if (isNoAnswer && shareChooseList != null && shareChooseList.Count > 1)
            {
                shareChooseList[1].ForEach(item =>
                {
                    item.Name = "";
                });
            }
            return shareChooseList;
        }

        /// <summary>
        /// 问题验证
        /// </summary>
        /// <param name="QuestionTypeKey">问题类型</param>
        /// <param name="QuestionTypeKey">问题内容</param>
        /// <param name="Answer">问题答案</param>
        /// <param name="AnalysisAnswer">解析答案</param>
        /// <param name="IsRequire">是否开启验证</param>
        /// <returns></returns>
        public string CheckQuestion(string QuestionTypeKey,string QuestionContent,string Answer, out string AnalysisAnswer,bool IsRequire=true)
        {
            AnalysisAnswer = "";
            string message = "";
            //单选、多选、单选(数字)题
            if (QuestionTypeKey == "single" || QuestionTypeKey == "multiple" || QuestionTypeKey == "single-number")
            {
                if (!string.IsNullOrEmpty(QuestionContent))
                {
                    var optionItems = new List<optionItem>();
                    try
                    {
                        optionItems = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(QuestionContent);
                    }
                    catch (Exception ex)
                    {
                        Common.Plugin.LogHelper.Write(ex, " CheckQuestion:____" + QuestionContent);
                    }

                    message = CheckChoose(optionItems);
                    var tempList = optionItems.Where(qitem => qitem.IsAnswer == true);
                    if (tempList != null && tempList.Count() > 0)
                    {
                        AnalysisAnswer = string.Join(",", tempList.OrderBy(qitem => qitem.Name).Select(qitem => qitem.Name));
                    }
                    else
                    {
                        AnalysisAnswer = "";
                    }
                    if (!string.IsNullOrEmpty(message))
                    {
                        return message;
                    }
                }
            }
            //判断题
            else if (QuestionTypeKey == "judge")
            {
                var optionItems = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(QuestionContent);
                message = CheckChoose(optionItems);
                var judgeModel = optionItems.Where(qitem => qitem.IsAnswer == true)?.FirstOrDefault();
                if (judgeModel != null )
                {
                    AnalysisAnswer = string.Join(",", judgeModel?.Name);
                }
                else
                {
                    AnalysisAnswer = "";
                }
                if (!string.IsNullOrEmpty(message))
                {
                    return message;
                }
            }
            //填空题
            else if (QuestionTypeKey == "fill-in")
            {
                var fillInList = Common.Plugin.JsonHelper.DeserializeObject<List<fillInItem>>(QuestionContent);
                message = CheckFillIn(fillInList);
                AnalysisAnswer = string.Join("★", fillInList.Select(qitem => Common.Plugin.StringHelper.AppHtmlFilterr(qitem.Content)));
                if (!string.IsNullOrEmpty(message))
                {
                    return message;
                }
            }
            //简答题
            else if (QuestionTypeKey == "short-answer")
            {
                message = CheckShortAnswer(Answer);
                AnalysisAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(Answer);
                if (!string.IsNullOrEmpty(message))
                {
                    return message;
                }
            }
            //名词解释
            else if (QuestionTypeKey == "noun-explanation")
            {
                AnalysisAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(Answer);
                if (string.IsNullOrEmpty(Answer))
                {
                    message = "请填写名词解释答案";
                    return message;
                }
            }
            //论述题
            else if (QuestionTypeKey == "essay-question")
            {
                AnalysisAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(Answer);
                if (string.IsNullOrEmpty(Answer))
                {
                    message = "请填写论述题答案";
                    return message;
                }
            }
            //计算题
            else if (QuestionTypeKey == "calculation")
            {
                AnalysisAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(Answer);
                if (string.IsNullOrEmpty(Answer))
                {
                    message = "请填写计算题答案";
                    return message;
                }
            }
            //分录题、资料题
            else if (QuestionTypeKey == "entry-problem" || QuestionTypeKey == "data-question")
            {
                var entryList = Common.Plugin.JsonHelper.DeserializeObject<List<fillInItem>>(QuestionContent);
                if (entryList != null && entryList.Count > 0)
                {
                    AnalysisAnswer = string.Join("★", entryList.Select(qitem => Common.Plugin.StringHelper.AppHtmlFilterr(qitem.Content)));
                    if (entryList.Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                    {
                        message = "请输入答案";
                        return message;
                    }
                }
                else
                {
                    message = "请添加答案";
                    return message;
                }
            }
            //排序题
            else if (QuestionTypeKey == "sorting-problem")
            {
                var sortList = Common.Plugin.JsonHelper.DeserializeObject<List<List<matchingItem>>>(QuestionContent);
                if (sortList != null && sortList.Count > 0)
                {
                    if (sortList.Count > 1)
                    {
                        AnalysisAnswer = string.Join(",", sortList[1].Select(qitem => qitem.Name));
                    }
                    if (sortList[0].Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                    {
                        message = "请输入选项内容";
                        return message;
                    }
                }
                else
                {
                    message = "请添加选项";
                    return message;
                }
            }
            //连线题
            else if (QuestionTypeKey == "matching")
            {
                var matchList = Common.Plugin.JsonHelper.DeserializeObject<List<List<matchingItem>>>(QuestionContent);
                if (matchList != null && matchList.Count > 0)
                {
                    if (matchList[0] != null && matchList[0].Count > 0)
                    {
                        if (matchList[0].Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                        {
                            message = "第一组选项内容不能为空";
                            return message;
                        }
                    }
                    else
                    {
                        message = "请添加第一组选项";
                        return message;
                    }
                    if (matchList.Count > 1 && matchList[1] != null && matchList[1].Count > 0)
                    {
                        if (matchList[1].Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                        {
                            message = "第二组选项内容不能为空";
                            return message;
                        }
                    }
                    else
                    {
                        message = "请添加第二组选项";
                        return message;
                    }
                    if (matchList.Count > 2)
                    {
                        AnalysisAnswer = string.Join(",", matchList[2].Select(qitem => qitem.Content));
                        if (matchList[2].Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                        {
                            message = "请设置答案";
                            return message;
                        }
                    }
                }
                else
                {
                    message = "请添加选项";
                    return message;
                }
            }
            //完型填空
            else if (QuestionTypeKey == "cloze")
            {
                var clozeList = Common.Plugin.JsonHelper.DeserializeObject<List<colzeItem>>(QuestionContent);
                if (clozeList != null && clozeList.Count > 0)
                {
                    int index = 1;
                    List<SubAnswerItem> clozeAnswerList = new List<SubAnswerItem>(); 
                    foreach (var rootItem in clozeList)
                    {
                        var tempModel = rootItem.OptionList.Where(qitem => qitem.IsAnswer == true)?.FirstOrDefault();
                        clozeAnswerList.Add(new SubAnswerItem()
                        {
                           SubQuestionId= index.ToString(),
                           SubAnswer= tempModel?.Name ?? "",
                           SubScore = rootItem.SubScore,
                        });
                        if (IsRequire)
                        {
                            if (rootItem.OptionList.Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                            {
                                message = string.Format("第{0}小题选项内容不能为空!", index);
                                return message;
                            }
                            if (!(rootItem.OptionList.Where(qitem => qitem.IsAnswer == true).Count() > 0))
                            {
                                message = string.Format("请设置第{0}小题选项的正确答案!", index);
                                return message;
                            }
                        }
                        index++;
                    }
                    AnalysisAnswer = Common.Plugin.JsonHelper.Serialize(clozeAnswerList);
                }
                else
                {
                    message = "请添加小题!";
                    return message;
                }
            }
            //口语题
            else if (QuestionTypeKey == "spoken")
            {
                AnalysisAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(Answer);
                if (string.IsNullOrEmpty(Answer))
                {
                    message = "请填写口语题答案!";
                    return message;
                }
            }
            //其它题
            else if (QuestionTypeKey == "other")
            {
                AnalysisAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(Answer);
                if (string.IsNullOrEmpty(Answer))
                {
                    message = "请填写其它题答案!";
                    return message;
                }
            }
            //阅读理解和听力题
            else if (QuestionTypeKey == "reading-comprehensio" || QuestionTypeKey == "listening")
            {
                var readingList = Common.Plugin.JsonHelper.DeserializeObject<List<readingComprehensioItem>>(QuestionContent);
                if (readingList != null && readingList.Count > 0)
                {
                    List<SubAnswerItem> resultList = new List<SubAnswerItem>();
                    int Index = 1;
                    foreach (var item in readingList)
                    {
                        if (string.IsNullOrEmpty(item.SubTitle))
                        {
                            message = "请填写题干!";
                            return message;
                        }
                        switch (item.QuestionKey)
                        {
                            //单选题
                            case "single":
                                var singleAnwser = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(item.SubAnwser.ToString());
                                string singleMessage = CheckChoose(singleAnwser);
                                resultList.Add(new SubAnswerItem
                                {
                                    SubQuestionId = Index.ToString(),
                                    SubQuestionKey=item.QuestionKey,
                                    SubAnswer = singleAnwser.Where(qitem => qitem.IsAnswer == true)?.FirstOrDefault()?.Name??"",
                                    SubScore=item.SubScore,
                                });
                                if (IsRequire)
                                {
                                    if (!string.IsNullOrEmpty(singleMessage))
                                    {
                                        return singleMessage;
                                    }
                                }
                                break;
                            //单选题(数字)
                            case "single-number":
                                var singleNumberAnwser = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(item.SubAnwser.ToString());
                                string singleNumberMessage = CheckChoose(singleNumberAnwser);
                                resultList.Add(new SubAnswerItem
                                {
                                    SubQuestionId = Index.ToString(),
                                    SubQuestionKey = item.QuestionKey,
                                    SubAnswer = singleNumberAnwser.Where(qitem => qitem.IsAnswer == true)?.FirstOrDefault()?.Name ?? "",
                                    SubScore = item.SubScore,
                                });
                                if (IsRequire)
                                {
                                    if (!string.IsNullOrEmpty(singleNumberMessage))
                                    {
                                        return singleNumberMessage;
                                    }
                                }
                                break;
                            //多选题
                            case "multiple":
                                var multipleAnwser = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(item.SubAnwser.ToString());
                                string multipleMessage = CheckChoose(multipleAnwser);
                                resultList.Add(new SubAnswerItem
                                {
                                    SubQuestionId = Index.ToString(),
                                    SubQuestionKey = item.QuestionKey,
                                    SubAnswer = string.Join(",", multipleAnwser.Where(qitem => qitem.IsAnswer == true).Select(qitem => qitem.Name)),
                                    SubScore=item.SubScore,
                                });
                                if (IsRequire)
                                {
                                    if (!string.IsNullOrEmpty(multipleMessage))
                                    {
                                        return multipleMessage;
                                    }
                                }
                                break;
                            //填空题
                            case "fill-in":
                                var r_fillInList = Common.Plugin.JsonHelper.DeserializeObject<List<fillInItem>>(item.SubAnwser.ToString());
                                string r_fillMessage = CheckFillIn(r_fillInList);
                                resultList.Add(new SubAnswerItem
                                {
                                    SubQuestionId = Index.ToString(),
                                    SubQuestionKey = item.QuestionKey,
                                    SubAnswer = string.Join(",", r_fillInList.Select(qitem => Common.Plugin.StringHelper.AppHtmlFilterr(qitem.Content))),
                                    SubScore=item.SubScore,
                                });
                                if (IsRequire)
                                {
                                    if (!string.IsNullOrEmpty(r_fillMessage))
                                    {
                                        return r_fillMessage;
                                    }
                                }
                                break;
                            //判断题
                            case "judge":
                                var judgeList = Common.Plugin.JsonHelper.DeserializeObject<List<optionItem>>(item.SubAnwser.ToString());
                                resultList.Add(new SubAnswerItem
                                {
                                    SubQuestionId = Index.ToString(),
                                    SubQuestionKey = item.QuestionKey,
                                    SubAnswer = judgeList.Where(qitem=>qitem.IsAnswer==true)?.FirstOrDefault()?.Name??"",
                                    SubScore=item.SubScore,
                                });
                                break;
                            //简答题
                            case "short-answer":
                                var shortAnswer = Common.Plugin.JsonHelper.DeserializeObject<List<fillInItem>>(item.SubAnwser.ToString());
                                string r_shortMessage = CheckShortAnswer(shortAnswer[0].Content);
                                resultList.Add(new SubAnswerItem
                                {
                                    SubQuestionId = Index.ToString(),
                                    SubQuestionKey = item.QuestionKey,
                                    SubAnswer = Common.Plugin.StringHelper.AppHtmlFilterr(shortAnswer[0].Content),
                                    SubScore=item.SubScore,
                                });
                                if (IsRequire)
                                {
                                    if (!string.IsNullOrEmpty(r_shortMessage))
                                    {
                                        return r_shortMessage;
                                    }
                                }
                                break;
                        }

                        Index++;
                    }
                    if (resultList != null && resultList.Count > 0)
                    {
                        AnalysisAnswer = Common.Plugin.JsonHelper.Serialize(resultList);
                    }
                }
                else
                {
                    message = "请添加小题!";
                    return message;
                }
            }
            //共用选择题
            else if (QuestionTypeKey == "sharing-choose")
            {
                var shareList = Common.Plugin.JsonHelper.DeserializeObject<List<List<matchingItem>>>(QuestionContent);
                if (shareList != null && shareList.Count == 2)
                {
                    if (shareList[0].Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                    {
                        message = string.Format("选项内容不能为空!");
                        return message;
                    }
                    //判断选项是否重复
                    if (shareList[0].GroupBy(qitem => new { qitem.Content }).Where(qitem => qitem.Count() > 1).Count() > 0)
                    {
                        message = string.Format("选项内容不能相同!");
                        return message;
                    }
                    if (shareList[1].Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                    {
                        message = string.Format("题干不能为空!");
                        return message;
                    }
                    //判断题干是否重复
                    if (shareList[1].GroupBy(qitem => new { qitem.Content }).Where(qitem => qitem.Count() > 1).Count() > 0)
                    {
                        message = string.Format("题干不能相同!");
                        return message;
                    }

                    AnalysisAnswer = string.Join(",", shareList[1].Select(qitem => qitem.Name));
                    //判断题干是否设置正确选项
                    if (shareList[1].Where(qitem => string.IsNullOrEmpty(qitem.Name)).Count() > 0)
                    {
                        message = string.Format("请设置题干的正确选项!");
                        return message;
                    }
                }
                else
                {
                    message = string.Format("请添加选项!");
                    return message;
                }
            }
            return message;
        }

        /// <summary>
        /// 选择题验证
        /// </summary>
        /// <param name="optionItems"></param>
        /// <returns></returns>
        private string CheckChoose(List<optionItem> optionItems)
        {
            string message = "";
            if (optionItems != null && optionItems.Count > 0)
            {
                if (optionItems.Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                {
                    message = "选项不能为空!";
                }
                //判断选项是否重复
                var tempList = optionItems.GroupBy(qitem => new { qitem.Content }).Select(qitem => new
                {
                    qitem.Key.Content
                });
                foreach (var item in tempList)
                {
                    var tempCount = optionItems.Where(qitem => qitem.Content == item.Content).Count();
                    if (tempCount > 1 && string.IsNullOrEmpty(message))
                    {
                        message = string.Format("选项【{0}】不能相同!", item.Content);
                    }
                }
                if (!(optionItems.Where(qitem => qitem.IsAnswer == true).Count() > 0))
                {
                    message = "请设置正确的选项!";
                }
            }
            else
            {
                message = "请添加选择题选项!";
            }
            return message;
        }

        /// <summary>
        /// 验证填空题
        /// </summary>
        /// <param name="fillInList"></param>
        /// <returns></returns>
        private string CheckFillIn(List<fillInItem> fillInList)
        {
            string message = "";
            if (fillInList != null && fillInList.Count > 0)
            {
                if (fillInList.Where(qitem => string.IsNullOrEmpty(qitem.Content)).Count() > 0)
                {
                    message = "请输入答案!";
                }
            }
            else
            {
                message = "请添加答案!";
            }
            return message;
        }

        /// <summary>
        /// 验证简答题
        /// </summary>
        /// <param name="answer"></param>
        /// <returns></returns>
        private string CheckShortAnswer(string answer)
        {
            string message = "";
            if (string.IsNullOrEmpty(answer))
            {
                message = "请填写简答题答案!";
            }
            return message;
        }
    }
}
