using System.Collections.Generic;
using GxPress.Request.Media;
using GxPress.Result.Category;
using GxPress.Result.Media;
using GxPress.Service.Interface.Epub;
using GxPress.Repository.Interface.Media;
using VersOne.Epub;
using System;
using System.Threading.Tasks;
using GxPress.Common.Tools;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.Order;

namespace GxPress.Service.Implement.Epub
{
    public class EpubService : IEpubService
    {
        private readonly IMediaRepository mediaRepository;
        private readonly IUserRepository userRepository;
        private readonly IOrderRepository orderRepository;
        public EpubService(IMediaRepository mediaRepository, IUserRepository userRepository, IOrderRepository orderRepository)
        {
            this.mediaRepository = mediaRepository;
            this.userRepository = userRepository;
            this.orderRepository = orderRepository;
        }
        /// <summary>
        /// 获取书籍目录
        /// </summary>
        public List<BookCatalogResult> GetCatalog(string path)
        {
            var result = new List<BookCatalogResult>();
            EpubBook epubBook = EpubReader.ReadBook(path);
            int i = 1;
            // 目录
            //列举章节
            foreach (EpubNavigationItem chapter in epubBook.Navigation)
            {
                //本章标题
                var bookCatalog = new BookCatalogResult();
                bookCatalog.Id = i;
                bookCatalog.CatalogNameg = chapter.Title;
                bookCatalog.CatalogId = chapter.HtmlContentFile.FileName;
                result.Add(bookCatalog);
                //嵌套章节
                List<EpubNavigationItem> subChapters = chapter.NestedItems;
                bookCatalog.IsChildren = chapter.NestedItems != null && subChapters.Count > 0;
                bookCatalog.Children = new List<BookCatalogResult>();
                foreach (var item in subChapters)
                {
                    var bookCatalogModel = new BookCatalogResult();
                    bookCatalogModel.Children = new List<BookCatalogResult>();
                    i++;
                    bookCatalogModel.Id = i;
                    bookCatalogModel.CatalogNameg = item.Title;
                    bookCatalogModel.CatalogId = item.HtmlContentFile.FileName;
                    bookCatalog.Children.Add(bookCatalogModel);
                }
                i++;
            }
            return result;
        }
        /// <summary>
        /// 获取章节内容
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetBookCatalogContent(BookCatalogRequest request)
        {
            if (string.IsNullOrEmpty(request.Path))
                return string.Empty;
            //获取media
            var media = await mediaRepository.GetAsync(request.MediaId);
            if (request.UserId > 0)
            {
                var user = await userRepository.GetAsync(request.UserId);
                if (user.IsVip && user.EndTime > DateTime.Now)
                    media.FreeProportion = 0;
                else
                {
                    if (await orderRepository.GetExistsAsync(request.UserId, request.MediaId))
                        media.FreeProportion = 0;
                }
            }
            var sectionValue = 0;
            if (media.FreeProportion > 0)
            {
                var freeProportion = media.FreeProportion / 100;
                var catalogs = GetCatalog(request.Path);
                sectionValue = Convert.ToInt32(catalogs.Count * freeProportion);
            }
            EpubBook epubBook = EpubReader.ReadBook(request.Path);
            string htmlContent = string.Empty;
            int i = 1;
            foreach (EpubNavigationItem chapter in epubBook.Navigation)
            {


                if (chapter.HtmlContentFile.FileName.Equals(request.CatalogId) || chapter.Title.Equals(request.CatalogNameg))
                {
                    if (i > sectionValue && sectionValue > 0)
                    {
                        htmlContent = HtmlAgilityPackHelper.GetHmtlContent(chapter.HtmlContentFile.Content);
                        break;
                    }
                    //当前文本内容文件的HTML
                    htmlContent = HtmlAgilityPackHelper.GetHmtl(chapter.HtmlContentFile.Content);
                    break;
                }
                foreach (var item in chapter.NestedItems)
                {
                    if (item.HtmlContentFile.FileName.Equals(request.CatalogId) || item.Title.Equals(request.CatalogNameg))
                    {
                        if (i > sectionValue && sectionValue > 0)
                        {
                            htmlContent = HtmlAgilityPackHelper.GetHmtlContent(item.HtmlContentFile.Content);
                            break;
                        }
                        //当前文本内容文件的HTML
                        htmlContent = HtmlAgilityPackHelper.GetHmtl(item.HtmlContentFile.Content);
                        break;
                    }
                    i++;
                }
                i++;
            }
            return htmlContent;
        }
        /// <summary>
        /// C#去除HTML标签
        /// </summary>
        /// <param name="html">带有html标签的文本</param>
        /// <param name="length">截取长度</param>
        /// <returns></returns>
        public string ReplaceHtmlTag(string html, int length = 0)
        {
            string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
            strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");

            if (length > 0 && strText.Length > length)
                return strText.Substring(0, length);

            return strText;
        }
    }
}