using ComPDFKit.PDFDocument;
using PDF_Office.Helper;
using PDF_Office.ViewModels.BOTA;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace PDF_Office.Views.BOTA
{
    /// <summary>
    /// BookmarkContent.xaml 的交互逻辑
    /// </summary>
    public partial class BookmarkContent : UserControl
    {
        private BookmarkContentViewModel viewModel = null;

        /// <summary>
        /// 上一个ListBoxItem，为选中状态做准备
        /// </summary>
        private ListBoxItem histotyListBoxItem = null;

        public BookmarkContent()
        {
            InitializeComponent();
            viewModel = this.DataContext as BookmarkContentViewModel;
        }

        /// <summary>
        ///ListViewItem，鼠标左键点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            object[] objects = new object[] { sender, e };
            TextBlock textBlock = null;
            ListBoxItem listBoxItem = (sender as ListBoxItem);
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (e.ClickCount >= 2)
                {
                    histotyListBoxItem = listBoxItem;
                    if (e.OriginalSource is TextBlock)
                    {
                        textBlock = (TextBlock)e.OriginalSource;

                        if (textBlock != null)
                        {
                            TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);

                            SetSelectedStatus(listBoxItem, textBox, textBlock);
                        }
                    }
                }
                else if (e.ClickCount == 1)
                {
                    if (histotyListBoxItem != listBoxItem)
                    {
                        if (histotyListBoxItem != null)
                        {
                            histotyListBoxItem.IsSelected = false;
                            textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
                            textBlock.Visibility = Visibility.Visible;
                            textBlock.Focusable = true;
                        }
                    }

                    viewModel.ListViewItemMouseDownCommand.Execute(sender);
                }
            }
        }

        /// <summary>
        /// ListViewItem双击时选中状态
        /// </summary>
        /// <param name="listBoxItem"></param>
        /// <param name="textBox"></param>
        /// <param name="textBlock"></param>
        private void SetSelectedStatus(ListBoxItem listBoxItem, TextBox textBox, TextBlock textBlock)
        {
            listBoxItem.IsSelected = true;
            listBoxItem.Focus();
            textBlock.Visibility = Visibility.Collapsed;

            textBox.Dispatcher.BeginInvoke(new Action(() =>
            {
                textBox.Focus();
                textBox.SelectAll();
            }));
        }

        /// <summary>
        /// ListViewItem失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ListViewItem_LostFocus(object sender, RoutedEventArgs e)
        {
            ListBoxItem listItem = sender as ListBoxItem;
            if (listItem != null)
            {
                viewModel.LostFocusCommand.Execute(listItem);
                TextBlock textBox = CommonHelper.FindVisualChild<TextBlock>(listItem);
                if (textBox.Visibility == Visibility.Collapsed)
                {
                    listItem.IsSelected = true;
                }
                else
                {
                    listItem.IsSelected = false;
                }
            }
        }

        /// <summary>
        /// 右键菜单-重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemRename_Click(object sender, RoutedEventArgs e)
        {
            if (sender is MenuItem)
            {
                MenuItem menuItem = (MenuItem)sender;
                CPDFBookmark bookmark = menuItem.CommandParameter as CPDFBookmark;
                if (bookmark != null)
                {
                    ListBoxItem listBoxItem = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(bookmark));
                    histotyListBoxItem = listBoxItem;

                    TextBox textBox = CommonHelper.FindVisualChild<TextBox>(listBoxItem);
                    TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listBoxItem);
                    SetSelectedStatus(listBoxItem, textBox, textBlock);
                }
            }
        }

        /// <summary>
        /// BookMarkListView，鼠标点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BookMarkListView_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var pos = e.GetPosition(BookMarkListView);

                var result = VisualTreeHelper.HitTest(BookMarkListView, pos);
                if (result != null)
                {
                    //获取当前鼠标指针下的容器
                    var listBoxItem = CommonHelper.FindVisualParent<ListBoxItem>(result.VisualHit);
                    if (listBoxItem == null)
                    {
                        if (BookMarkListView.SelectedItem != null)
                        {
                            ListBoxItem item = (ListBoxItem)(BookMarkListView.ItemContainerGenerator.ContainerFromItem(BookMarkListView.SelectedItem));
                            item.IsSelected = false;
                            TextBlock box = CommonHelper.FindVisualChild<TextBlock>(item);
                            box.Visibility = Visibility.Visible;
                            BookMarkListView.SelectedItems.Clear();
                        }
                        if (histotyListBoxItem != null)
                        {
                            var pos1 = e.GetPosition(histotyListBoxItem);
                            var result1 = VisualTreeHelper.HitTest(BookMarkListView, pos1);
                            if (result1 == null)
                            {
                                histotyListBoxItem.IsSelected = false;
                                TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(histotyListBoxItem);
                                textBlock.Visibility = Visibility.Visible;
                                textBlock.Focusable = true;
                            }
                        }
                    }
                    else
                    {
                        listBoxItem.Focus();
                    }
                }
                BookMarkListView.Focus();
            }
        }

        /// <summary>
        /// 右键菜单-删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemDeleteCommand_Click(object sender, RoutedEventArgs e)
        {
            List<int> pagelist = new List<int>();
            for (int i = 0; i < BookMarkListView.SelectedItems.Count; i++)
            {
                CPDFBookmark item = BookMarkListView.SelectedItems[i] as CPDFBookmark;
                pagelist.Add(BookMarkListView.Items.IndexOf(item));
            }
            pagelist.Sort();
            for (int i = 0; i < pagelist.Count; i++)
            {
                CPDFBookmark data = BookMarkListView.Items[pagelist[pagelist.Count - i - 1]] as CPDFBookmark;
                if (data != null)
                {
                    viewModel.DeleteCommand.Execute(data);
                }
            }
        }
    }
}