using PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePagePrinter;
using Prism.Ioc;
using Prism.Regions;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using UserControl = System.Windows.Controls.UserControl;

namespace PDF_Office.Views.Dialog.HomePageToolsDialogs.HomePagePrinter
{
    /// <summary>
    /// Interaction logic for PrinterDialog
    /// </summary>
    public partial class HomePagePrinterDialog : UserControl
    {
        private IContainerProvider containerProvider;

        private Regex regexForPageIndex = new Regex(@"^[0-9]\d*$");
        private Regex regexForZeroIndex = new Regex(@"^[0]\d*$");

        private int _currentPreviewPageIndex;
        public int CurrentPreviewPageIndex
        {
            get { return _currentPreviewPageIndex; }
            set { _currentPreviewPageIndex = value; }
        }

        public HomePagePrinterDialog(IContainerProvider containerProvider)
        {
            InitializeComponent();
            this.containerProvider = containerProvider;
            var regionManager = containerProvider.Resolve<IRegionManager>();
            RegionManager.SetRegionManager(this, regionManager);
            RegionManager.UpdateRegions();
        }

        private void PageRangeComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            PageRangeComboBox.CurrentPage = true;
            if (PageRangeComboBox.CurrentPage)
            {
                ComboBoxItem comboBoxItem = new ComboBoxItem();
                comboBoxItem.Content = "当前页面";
                PageRangeComboBox.writableComboBox.Items.Insert(1, comboBoxItem);
            }
            PageRangeComboBox.writableComboBox.SelectionChanged += WritableComboBox_SelectionChanged;
        }

        private void WritableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((HomePagePrinterDialogViewModel)this.DataContext).SetPageRangeCommand.Execute();
            this.CurrentPageIndexTextBox.Text = "1";
            CurrentPreviewPageIndex= 0;
        }

        private void PrintOrientationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((HomePagePrinterDialogViewModel)this.DataContext).SetPrintOrientationCommand.Execute();
        }

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

        private void CurrentPageIndexTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber != null)
            {
                if (e.Key == System.Windows.Input.Key.Enter && this.CurrentPageIndexTextBox.Text != (CurrentPreviewPageIndex + 1).ToString())
                {
                    var CurrentPageTextBox = sender as System.Windows.Controls.TextBox;

                    //非子集、空集、边界值
                    if (!regexForPageIndex.IsMatch(CurrentPageTextBox.Text) || CurrentPageTextBox.Text == "" || regexForZeroIndex.IsMatch(CurrentPageTextBox.Text))
                    {
                        CurrentPreviewPageIndex = 1;
                        CurrentPageTextBox.Text = CurrentPreviewPageIndex.ToString();
                    }

                    if (int.Parse(CurrentPageTextBox.Text) > int.Parse(((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber))
                    {
                        CurrentPreviewPageIndex = int.Parse(((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber);
                        CurrentPageTextBox.Text = ((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber;
                    }

                    CurrentPreviewPageIndex = int.Parse(CurrentPageIndexTextBox.Text);
                    CurrentPreviewPageIndex = CurrentPreviewPageIndex - 1;
                    ((HomePagePrinterDialogViewModel)this.DataContext).CurrentListIndexString = (CurrentPreviewPageIndex).ToString();
                    ((HomePagePrinterDialogViewModel)this.DataContext).GetPagePreviewWhenIndexChangedCommand.Execute();
                }
            }
        }

        private void PrePageBtn_Click(object sender, RoutedEventArgs e)
        {
            CurrentPreviewPageIndex--;
            this.CurrentPageIndexTextBox.Text = (CurrentPreviewPageIndex + 1).ToString();
           ((HomePagePrinterDialogViewModel)this.DataContext).CurrentListIndexString = CurrentPreviewPageIndex.ToString();
            ((HomePagePrinterDialogViewModel)this.DataContext).GetPagePreviewWhenIndexChangedCommand.Execute();
        }

        private void NextPageBtn_Click(object sender, RoutedEventArgs e)
        {
            CurrentPreviewPageIndex++;
            this.CurrentPageIndexTextBox.Text = (CurrentPreviewPageIndex + 1).ToString();
            ((HomePagePrinterDialogViewModel)this.DataContext).CurrentListIndexString = CurrentPreviewPageIndex.ToString();
            ((HomePagePrinterDialogViewModel)this.DataContext).GetPagePreviewWhenIndexChangedCommand.Execute();
        }

        //private void CurrentPageIndexTextBox_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    if (((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber != null)
        //    {
        //        if (this.CurrentPageIndexTextBox.Text != "")
        //        {
        //            var CurrentPageTextBox = sender as System.Windows.Controls.TextBox;
        //            if (regexForPageIndex.IsMatch(CurrentPageTextBox.Text))
        //            {
        //                if (int.Parse(((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber) > 2)
        //                {
        //                    if (int.Parse(CurrentPageTextBox.Text) > 1 && int.Parse(CurrentPageTextBox.Text) < int.Parse(((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber))
        //                    {
        //                        PrePageBtn.IsEnabled = true;
        //                        NextPageBtn.IsEnabled = true;
        //                    }
        //                    else if (int.Parse(CurrentPageTextBox.Text) <= 1 && int.Parse(CurrentPageTextBox.Text) < int.Parse(((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber))
        //                    {
        //                        PrePageBtn.IsEnabled = false;
        //                        NextPageBtn.IsEnabled = true;
        //                    }
        //                    else if (int.Parse(CurrentPageTextBox.Text) > 1 && int.Parse(CurrentPageTextBox.Text) >= int.Parse(((HomePagePrinterDialogViewModel)this.DataContext).MaxPageNumber))
        //                    {
        //                        PrePageBtn.IsEnabled = true;
        //                        NextPageBtn.IsEnabled = false;
        //                    }
        //                }
        //                else
        //                {
        //                    PrePageBtn.IsEnabled = false;
        //                    NextPageBtn.IsEnabled = false;
        //                }
        //            }
        //            else
        //            {
        //                this.PrePageBtn.IsEnabled = false;
        //                this.NextPageBtn.IsEnabled = false;
        //            }
        //        }
        //        else
        //        {
        //            this.PrePageBtn.IsEnabled = false;
        //            this.NextPageBtn.IsEnabled = false;
        //        }
        //    }

        //}

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((HomePagePrinterDialogViewModel)this.DataContext).SetPrintContentCommand.Execute();
        }

        private void ReversePageCheckBox_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentPageIndexTextBox.Text = "1";
            CurrentPreviewPageIndex = 0;
        }
    }
}
