using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using PDF_Office.Helper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace PDF_Office.CustomControl
{
    /// <summary>
    /// PageTurningPreview.xaml 的交互逻辑
    /// </summary>
    public partial class PageTurningPreview : UserControl
    {
        public CPDFDocument document;
        public List<int> PageIndexLists = new List<int>();

        private int currentIndex = 0;
        public int CurrentIndex
        {
            get { return currentIndex; }
            set
            {
                currentIndex = value;
                //合理的使用set方法来进行一些操作，可以帮助理清很多思路，且极大减少代码量
                this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
                if (document != null)
                {
                    AwaitRenderBitmap(document);
                }
            }
        }
        public PageTurningPreview()
        {   
            InitializeComponent();
         
        }

        public async Task RenderBitmap(CPDFDocument doc)
        {
            CPDFPage page = doc.PageAtIndex(PageIndexLists[CurrentIndex]);
            byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
            await Task.Run(delegate
            {
                page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
            });
            PixelFormat fmt = PixelFormats.Bgra32;
            BitmapSource bps = BitmapSource.Create((int)page.PageSize.Width, (int)page.PageSize.Height, 96.0, 96.0, fmt, null, bmp_data, ((int)page.PageSize.Width * fmt.BitsPerPixel + 7) / 8);
            this.Image.Source = bps;
        }
        public async void AwaitRenderBitmap(CPDFDocument doc)
        {
            await RenderBitmap(doc);
        }

        private void PrePage_Click(object sender, RoutedEventArgs e)
        {

            if (CurrentIndex > 0)
            {
                CurrentIndex--;
            }
        }

        private void NextPage_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentIndex < PageIndexLists.Count()-1 && PageIndexLists.Count() > 0)
            {
                CurrentIndex++;
            }
           
            
        }

        private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.Key == Key.Enter)
            {
                if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
                {
                    this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
                }
                if (this.PageIndex != null)
                {
                    if (PageIndexLists.Contains( int.Parse(this.CurrentPage.Text)-1))
                    {
                        CurrentIndex=PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text)-1);
                        this.CurrentPage.Text = (PageIndexLists[CurrentIndex]+1).ToString();
                        AwaitRenderBitmap(document);
                    }
                    else
                    {
                        this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
                        MessageBox.Show("超出页面范围");
                    }
                }
                
            }
        }

        private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
        }
    }
}


