using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OfficeAppHelp.Word
{
    /// <summary>
    /// 文档扩展
    /// </summary>
    public static class DocEx
    {
        /// <summary>
        /// 根据页码获取页内容
        /// </summary>
        /// <param name="pageNumber">页码（非调整后的页码）</param>
        /// <returns>页内容</returns>
        public static Range GetPageRange(this Document doc, int pageNumber)
        {
            int numberOfPages = (int)doc.Content.Information[WdInformation.wdNumberOfPagesInDocument];

            //超出页码范围
            if (pageNumber > numberOfPages)
                return null;

            int Start, End;

            Range rangeStart = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, pageNumber);
            Start = rangeStart.Start;

            //只有一页
            int countPlusOne = pageNumber + 1;
            if (countPlusOne > numberOfPages)
                End = doc.Range().End;
            else
            {
                Range rangeEnd = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, countPlusOne);
                End = rangeEnd.End;
            }

            return doc.Range(Start, End);
        }

        /// <summary>
        /// 获取页内容
        /// </summary>
        /// <param name="pageNumber">页码（非调整后的页码）</param>
        /// <returns>页内容</returns>
        public static List<Range> GetPageRanges(this Document doc)
        {
            //int numberOfPages = (int)doc.Content.Information[WdInformation.wdNumberOfPagesInDocument];
            int numberOfPages = (int)doc.Content.get_Information(WdInformation.wdActiveEndPageNumber);

            List<Range> rs = new List<Range>();
            for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++)
            {
                int Start, End;

                Range rangeStart = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, pageNumber);
                Start = rangeStart.Start;

                //只有一页
                int countPlusOne = pageNumber + 1;
                if (countPlusOne > numberOfPages)
                    End = doc.Range().End;
                else
                {
                    Range rangeEnd = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, countPlusOne);
                    End = rangeEnd.End;
                }

                rs.Add(doc.Range(Start, End));
            }
            return rs;
        }

        /// <summary>
        /// 得到文档的末尾
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static Range RangeEnd(this Document doc)
        {
            int end = doc.Content.End;
            return doc.Range(end - 1, end);
        }

        /// <summary>
        /// 文档末尾的空段落数量
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="count">末尾的空段落数量</param>
        public static void ParagraphEndCount(this Document doc, int count)
        {
            if (count < 0)
                return;

            while (true)
            {
                var pRange = doc.Paragraphs.Last.Range;
                if (pRange.IsNullOrSpace())
                    pRange.Delete();
                else
                    break;
            }

            if (count == 0)
                return;

            string han = new string('\n', count);
            doc.Content.InsertAfter(han);
        }

    }
}
