using System;
using System.Collections.Generic;
using System.Diagnostics;
using ComPDFKitViewer.PdfViewer;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace PDF_Office.Helper
{
    public static class HomePageEditHelper
    {
        /// <summary>
        /// 获取所需要分文档份数
        /// </summary>
        /// <param name="pagecontent">文档总页数</param>
        /// <param name="averagepage">平均几页分为一个文档</param>
        /// <param name="remainder">最后文档的页数的引用 例如11页文档每3页为一份PDF，3，3，3，2，remainder=2</param>
        /// <returns></returns>
        public static int AverageSplitPages(int pagecontent, int averagepage, ref int remainder)
        {
            if (pagecontent <= 0)
            {
                remainder = 0;
                return 0;
            }
            if (pagecontent <= averagepage)
            {
                remainder = pagecontent;

                return 1;
            }
            if (pagecontent > averagepage)
            {
                if (pagecontent % averagepage == 0)
                {
                    remainder = 0;
                    return pagecontent / averagepage;
                }
                remainder = pagecontent % averagepage;
                return (pagecontent / averagepage) + 1;

            }
            remainder = 0;
            return -1;
        }
        /// <summary>
        /// 获取平均每个文档的页数
        /// </summary>
        /// <param name="pagecontent">文档总页数</param>
        /// <param name="averagedoc">平均几页分为一个文档</param>
        /// <param name="remainder">引用，截至第几位文档比后面文档多一 例如11页文档 分为3份 4，4，3 remainder为1 第一个文档和第二文档比后续文档多一</param>
        /// <returns></returns>
        public static int AverageSplitDocs(int pagecontent, int averagedoc, ref int remainder)
        {
            if (pagecontent <= 0)
            {
                remainder = 0;
                return 0;
            }
            if (pagecontent <= averagedoc)
            {
                remainder = 0;
                return 1;
            }
            if (pagecontent > averagedoc)
            {
                remainder = pagecontent % averagedoc;
                return pagecontent / averagedoc;
            }
            return 0;

        }

        public static void GetPagerange(string PageRangeSelectIndex, CPDFViewer currentViewer, ref string pageRange, string pageRangeText, bool currentpage = false)
        {
            if (!currentpage)
            {
                if (PageRangeSelectIndex == "0")
                {
                    pageRange = "1-" + currentViewer.Document.PageCount.ToString();
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                else if (PageRangeSelectIndex == "1")
                {
                    pageRange = "1";
                    for (int i = 3; i <= currentViewer.Document.PageCount; i += 2)
                        pageRange = pageRange + "," + i;
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                else if (PageRangeSelectIndex == "2")
                {
                    pageRange = "2";
                    for (int i = 4; i <= currentViewer.Document.PageCount; i += 2)
                        pageRange = pageRange + "," + i;
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);

                }
                else if (PageRangeSelectIndex == "3")
                {
                    if (pageRangeText != "")
                    {
                        pageRange = pageRangeText;
                    }
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                else { return; }
            }
            else
            {
                if (PageRangeSelectIndex == "0")
                {
                    pageRange = "1-" + currentViewer.Document.PageCount.ToString();
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                if (PageRangeSelectIndex == "1")
                {
                    //pageRange = pageRange;
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                else if (PageRangeSelectIndex == "2")
                {
                    pageRange = "1";
                    for (int i = 3; i <= currentViewer.Document.PageCount; i += 2)
                        pageRange = pageRange + "," + i;
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                else if (PageRangeSelectIndex == "3")
                {
                    pageRange = "2";
                    for (int i = 4; i <= currentViewer.Document.PageCount; i += 2)
                        pageRange = pageRange + "," + i;
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);

                }
                else if (PageRangeSelectIndex == "4")
                {
                    if (pageRangeText != "")
                    {
                        pageRange = pageRangeText;
                    }
                    Trace.WriteLine(" extractModel.PageRange" + pageRange);
                }
                else { return; }

            }
        }
    }

}
