using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Exam;
using Edu.Module.Exam;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Edu.WebApi.Controllers.Applet
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class AppletWordsController : AppletBaseController
    {
        private readonly CourseExamModule courseExamModule = new CourseExamModule();

        /// <summary>
        /// 获取今日学习单词列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudyWords()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId");
            var ChapterId = base.ParmJObj.GetInt("ChapterId");
            var ReviewChapterId = base.ParmJObj.GetInt("ReviewChapterId");
            var list = courseExamModule.GetStudyCourseWords(CourseId, ChapterId, ReviewChapterId,base.AppletUserInfo.Id);
            return ApiResult.Success(data: list);
        }
 

        /// <summary>
        /// 新增修改学员学习
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStuWordsPrep()
        {
            var model = new RB_StuWords_Prep_Extend()
            {
                Id = base.ParmJObj.GetInt("Id"),
                ClassId = base.ParmJObj.GetInt("ClassId"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                Stu_Account_Id = base.ParmJObj.GetInt("Stu_Account_Id"),
                ChapterId=base.ParmJObj.GetInt("ChapterId"),
                PrepNum=base.ParmJObj.GetInt("PrepNum"),
                ReviewNum=base.ParmJObj.GetInt("ReviewNum"),
                StudyNum=base.ParmJObj.GetInt("StudyNum"),
                ReviewCourseChapterId=base.ParmJObj.GetInt("ReviewCourseChapterId"),
                StudyType=base.ParmJObj.GetInt("StudyType"),
            };
            model.CreateBy = base.AppletUserInfo.Id;
            model.CreateTime = DateTime.Now;
            model.School_Id = 0;
            model.Group_Id = base.AppletUserInfo.Group_Id;
            model.Status = Common.Enum.DateStateEnum.Normal;
            bool flag = courseExamModule.SetStuWordsPrepModule(model);
            return flag ? ApiResult.Success(data:model) : ApiResult.Failed();
        }

        /// <summary>
        /// 新增修改学员复习
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStuWordsReview()
        {
            var model = new RB_StuWords_Review_Extend()
            {
                Id = base.ParmJObj.GetInt("Id"),
                ClassId = base.ParmJObj.GetInt("ClassId"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                Stu_Account_Id = base.ParmJObj.GetInt("Stu_Account_Id"),
                ChapterId = base.ParmJObj.GetInt("ChapterId"),
                ReviewNum = base.ParmJObj.GetInt("ReviewNum"),
            };
            model.CreateBy = base.AppletUserInfo.Id;
            model.CreateTime = DateTime.Now;
            model.School_Id = 0;
            model.Group_Id = base.AppletUserInfo.Group_Id;
            model.Status = Common.Enum.DateStateEnum.Normal;
            bool flag = courseExamModule.SetStuWordsReviewModule(model);
            return flag ? ApiResult.Success(data:model) : ApiResult.Failed();
        }

        /// <summary>
        /// 生成学员单词测试
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CreateCourseExam()
        {
            var CourseId = base.ParmJObj.GetInt("CourseId");
            var ChapterId = base.ParmJObj.GetInt("ChapterId");
            var NextChapterId = base.ParmJObj.GetInt("NextChapterId");
            var obj = courseExamModule.CreateCourseExamModule(CourseId, ChapterId, NextChapterId);
            return ApiResult.Success(data: obj);
        }

        /// <summary>
        /// 提交单词测试
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SubmitWordsExam()
        {
            var model = new RB_StuWords_Exam_Extend()
            {
                Id = base.ParmJObj.GetInt("Id"),
                ClassId = base.ParmJObj.GetInt("ClassId"),
                CourseId=base.ParmJObj.GetInt("CourseId"),
                ChapterId=base.ParmJObj.GetInt("ChapterId"),
                ExamStartTime = base.ParmJObj.GetDateTime("ExamStartTime"),
            };
            model.Stu_Account_Id = base.AppletUserInfo.Id;
            model.CreateBy = base.AppletUserInfo.Id;
            model.CreateTime = DateTime.Now;
            model.School_Id = base.AppletUserInfo.School_Id;
            model.Group_Id = base.AppletUserInfo.Group_Id;
            model.Status = Common.Enum.DateStateEnum.Normal;
            model.Score = 0;
            model.ExamEndTime = DateTime.Now;
            model.ExamDetailList = new List<RB_StuWords_ExamDetail_Extend>();
            string details = base.ParmJObj.GetStringValue("Details");
            if (!string.IsNullOrEmpty(details))
            {
                JArray jArray = JArray.Parse(details);
                if (jArray != null && jArray.Count > 0)
                {
                    foreach (var obj in jArray)
                    {
                        JObject jobj = JObject.Parse(obj.ToString());
                        model.ExamDetailList.Add(new RB_StuWords_ExamDetail_Extend()
                        {
                            DetailId = 0,
                            ExamId = 0,
                            WordId = jobj.GetInt("WordId"),
                            Title = jobj.GetStringValue("Title"),
                            QuestionContent = jobj.GetStringValue("QuestionContent"),
                            QuestionTypeId = jobj.GetInt("QuestionTypeId"),
                            QuestionTypeKey=jobj.GetStringValue("QuestionTypeKey"),
                            IsRight = jobj.GetInt("IsTrue"),
                            Answer=jobj.GetStringValue("Answer"),
                        });
                    }
                }
            }

            var flag = courseExamModule.SubmitStuWordsExamModule(model);
            return flag ? ApiResult.Success(data: new
            {
                model.Id,
                model.Score,
                model.ExamMinutes
            }) : ApiResult.Failed();
        }
    }
}
