using System.Collections.Generic;
using System.Linq;
using Abp.UI;
using Microsoft.AspNetCore.Hosting;
using QuestionSwapDoc;
using QuestionSwapDoc.SwapQuestionModel;
using YGNT.QuestionLibrary.Core.BaseData;
using YGNT.QuestionLibrary.Core.Disciplines.Cache;
using YGNT.QuestionLibrary.Core.Disciplines.Dto;
using YGNT.QuestionLibrary.Core.Questions.Cache;

namespace YGNT.QuestionLibrary.Core.Helper
{
    public class DocHelper : IDocHelper
    {
        public const string Filters = ".doc;.docx;.txt";
        private readonly IQuestionTypeCache _questionTypeCache;
        private readonly IDisciplineQuestionTypeCache _disciplineQuestionTypeCache;


        public DocHelper(IQuestionTypeCache questionTypeCache, IDisciplineQuestionTypeCache disciplineQuestionTypeCache)
        {
            _questionTypeCache = questionTypeCache;
            _disciplineQuestionTypeCache = disciplineQuestionTypeCache;
        }

        /// <summary>
        /// 过滤后缀
        /// </summary>
        /// <param name="suffix"></param>
        public void Filter(string suffix)
        {
            var pass = Filters.Split(';', System.StringSplitOptions.RemoveEmptyEntries)
                .Any(x => x.ToUpperInvariant() == suffix.ToUpperInvariant());
            if (!pass)
                throw new UserFriendlyException($"文件类型不支持!");
        }

        /// <summary>
        /// 判断是否为选择题
        /// </summary>
        /// <param name="disciplineQuestionTypeId"></param>
        /// <returns></returns>
        public bool IsChoiceQuestion(int disciplineQuestionTypeId)
        {
            var disciplineQuestionType = _disciplineQuestionTypeCache.Get(disciplineQuestionTypeId);
            var questionType = _questionTypeCache.Get(disciplineQuestionType.QuestionTypeId);
            return questionType.QuestionCategory == BaseData.Enum.QuestionCategory.客观题;
        }

        /// <summary>
        /// 判断是否为判断题
        /// </summary>
        /// <param name="disciplineQuestionTypeId"></param>
        /// <returns></returns>
        public bool IsTrueFalseQuestion(int disciplineQuestionTypeId)
        {
            var disciplineQuestionType = _disciplineQuestionTypeCache.Get(disciplineQuestionTypeId);
            var questionType = _questionTypeCache.Get(disciplineQuestionType.QuestionTypeId);
            return questionType.QuestionCategory == BaseData.Enum.QuestionCategory.判断题;
        }

        /// <summary>
        /// 解析文档
        /// </summary>
        /// <param name="webHostEnvironment"></param>
        /// <param name="filePath">文件路径</param>
        /// <param name="questionTypes"></param>
        /// <returns></returns>
        public static QueDoc ResolveDoc(IWebHostEnvironment webHostEnvironment, string filePath, List<DisciplineQuestionTypeDto> questionTypes)
        {
            var dic = questionTypes
                .GroupBy(x => x.QuestionCategory)
                .ToDictionary(x => x.Key);
            var queTypeString = new QueTypeString()
            {
                PanDuanStrings = dic.GetValueOrDefault(BaseData.Enum.QuestionCategory.判断题)?.Select(x => x.Name).ToArray(),
                XuanZheStrings = dic.GetValueOrDefault(BaseData.Enum.QuestionCategory.客观题)?.Select(x => x.Name).ToArray()
            };
            var resolvePath = PathHelper.GetResolvePath();
            var imgPath = PathHelper.GetResolveImgPath(resolvePath);
            var questions = SwapQuestion.GetQue(filePath,
                webHostEnvironment.SplicePath(resolvePath),
                webHostEnvironment.SplicePath(imgPath),
                QuestionLibraryConsts.ResolveImgPlaceholder + imgPath,
                queTypeString);
            return questions;
        }
    }
}