using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using System;
using System.Collections.Generic;
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 PageTurningPreview()
        {
            InitializeComponent();
        }

        public async Task RenderBitmap(CPDFDocument doc)
        {
            CPDFPage page = doc.PageAtIndex(int.Parse(this.CurrentPage.Text) - 1,true);

            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 (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
            {
                this.CurrentPage.Text = "1";
            }
            if (int.Parse(this.CurrentPage.Text) > 1 && int.Parse(this.CurrentPage.Text) <= document.PageCount)
            {
                int Page = int.Parse(this.CurrentPage.Text);
                this.CurrentPage.Text = (--Page).ToString();
                if (document != null)
                {
                    AwaitRenderBitmap(document);
                }
            }
            else if (int.Parse(this.CurrentPage.Text) <= 1) { this.CurrentPage.Text = "1"; }
            else { this.CurrentPage.Text = document.PageCount.ToString(); }
            NextPage.IsEnabled = true;
            if (this.CurrentPage.Text == "1")
            {
                PrePage.IsEnabled = false;
            }

        }

        private void NextPage_Click(object sender, RoutedEventArgs e)
        {

            if (this.CurrentPage.Text == ""|| !int.TryParse(this.CurrentPage.Text,out int _))
            {
                this.CurrentPage.Text = "1";
            }
            if (int.Parse(this.CurrentPage.Text) < document.PageCount)
            {
                int Page = int.Parse(this.CurrentPage.Text);
                this.CurrentPage.Text = (++Page).ToString();
                if (document != null)
                {
                    AwaitRenderBitmap(document);
                }
            }
            PrePage.IsEnabled= true;
            if (this.CurrentPage.Text == document.PageCount.ToString())
            {
                 NextPage.IsEnabled = false;
            }
        }

        private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.CurrentPage.Text == ""|| !int.TryParse(this.CurrentPage.Text, out int _))
            {
                this.CurrentPage.Text = "1";
            }
            if (e.Key == Key.Enter)
            {
                if (this.CurrentPage.Text == "" || int.Parse(this.CurrentPage.Text) < 1)
                {
                    this.CurrentPage.Text = "1";
                }
                if (this.PageIndex != null)
                {
                    if (int.Parse(this.CurrentPage.Text) > int.Parse(this.PageIndex.Text))
                    {
                        this.CurrentPage.Text = this.PageIndex.Text;
                    }
                }
                if (int.Parse(this.CurrentPage.Text) <= document.PageCount && int.Parse(this.CurrentPage.Text) > 0)
                {
                    AwaitRenderBitmap(document);
                }
                else
                {
                    MessageBox.Show("超出页面范围");
                }
            }
        }

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


