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

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class CourseWordsController : BaseController
    {
        /// <summary>
        /// 课程单词处理类对象
        /// </summary>
        private readonly CourseWordsModule courseWordsModule = new CourseWordsModule();

        /// <summary>
        /// 导入Excel单词
        /// </summary>
        /// <returns></returns>
        public ApiResult ImportExcelWords(string filePath, int CourseId, int Uid)
        {
            var userInfo = base.GetUserInfo(Uid);
            var dataList = Common.Data.CourseWordsHelper.GetXlsWordsData(filePath);
            List<RB_Course_Words_Extend> result = new List<RB_Course_Words_Extend>();
            if (dataList != null && dataList.Count > 0)
            {
                foreach (var item in dataList)
                {
                    var model = new RB_Course_Words_Extend()
                    {
                        Id = 0,
                        CourseId = CourseId,
                        ChapterId = item.ChapterId,
                        WordType = item.WordType,
                        WordContent = item.WordContent,
                        WordTone = item.WordTone,
                        WordWrite = item.WordWrite,
                        ChineseMean = item.ChineseMean,
                        Status = Common.Enum.DateStateEnum.Normal,
                        Group_Id = userInfo.Group_Id,
                        CreateBy = userInfo.Id,
                        CreateTime = DateTime.Now,
                        UpdateBy = userInfo.Id,
                        UpdateTime = DateTime.Now
                    };
                    if (!string.IsNullOrEmpty(model.WordContent))
                    {
                        result.Add(model);
                    }
                }
            }
            bool flag = false;
            if (result != null && result.Count > 0)
            {
                flag = courseWordsModule.BatchInsertCourseWordsModule(result);
            }
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取课程单词列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseWordsPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Course_Words_Extend()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                ChapterId = base.ParmJObj.GetInt("ChapterId"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            var list = courseWordsModule.GetCourseWordsPageModule(pageModel.PageIndex,pageModel.PageSize,out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取课程单词列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseWordsList()
        {
            var query = new RB_Course_Words_Extend()
            {
                CourseId = base.ParmJObj.GetInt("CourseId"),
                ChapterId = base.ParmJObj.GetInt("ChapterId"),
            };
            query.Group_Id = base.UserInfo.Group_Id;
            var list = courseWordsModule.GetCourseWordsListModule(query);
            return ApiResult.Success(data: list);
        }

        /// <summary>
        /// 添加修改课程单词
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetCourseWords()
        {
            var extModel = new RB_Course_Words_Extend()
            {
                Id = base.ParmJObj.GetInt("Id"),
                CourseId = base.ParmJObj.GetInt("CourseId"),
                ChapterId = base.ParmJObj.GetInt("ChapterId"),
                WordType = base.ParmJObj.GetStringValue("WordType"),
                WordContent = base.ParmJObj.GetStringValue("WordContent"),
                WordTone = base.ParmJObj.GetStringValue("WordTone"),
                WordWrite = base.ParmJObj.GetStringValue("WordWrite"),
                ChineseMean = base.ParmJObj.GetStringValue("ChineseMean"),
                FileUrl = base.ParmJObj.GetStringValue("FileUrl"),
            };
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = UserInfo.Id;
            extModel.UpdateBy = UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            bool flag = courseWordsModule.SetCourseWordsModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 根据编号获取单词详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCourseWords()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var extModel = courseWordsModule.GetCourseWordsModule(Id);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 根据编号删除单词
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RemoveCourseWords()
        {
            var Id = base.ParmJObj.GetInt("Id", 0);
            var flag = courseWordsModule.RemoveCourseWordsModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
    }
}