using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Properties;
using PDF_Office.ViewModels.HomePanel.RecentFiles;
using PDFSettings.Settings;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Navigation;
using winform = System.Windows.Forms;

namespace PDF_Office.Views.HomePanel.RecentFiles
{
    public partial class RecentFilesContent : UserControl
    {
        private RecentFilesContentViewModel ViewModel => DataContext as RecentFilesContentViewModel;
        public RecentFilesContent()
        {
            InitializeComponent();
        }

        #region 初始化
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitVariables();
            InitEvents();
        }

        private void InitVariables()
        {
            if (RecentFilesList.SelectedItems != null)
                RecentFilesList.SelectedItems.Clear();

            if (GridRecentFilesList.SelectedItems != null)
                GridRecentFilesList.SelectedItems.Clear();
        }
        private void InitEvents()
        {
            ViewModel.RecentFilesSelectionHandler -= ViewModel_RecentFilesSelection;
            ViewModel.RecentFilesSelectionHandler += ViewModel_RecentFilesSelection;
        }
        #endregion

        #region UI操作
        private void ViewModel_RecentFilesSelection(object sender, bool e)
        {
            if (e)
            {
                RecentFilesList.SelectedItems.Clear();
                if (GridRecentFilesList.SelectedItems.Count > 0)
                {
                    for (int i = 0; i < GridRecentFilesList.SelectedItems.Count; i++)
                    {
                        RecentFilesList.SelectedItems.Add(GridRecentFilesList.SelectedItems[i]);
                    }

                }
            }
            else
            {
                GridRecentFilesList.SelectedItems.Clear();
                if (RecentFilesList.SelectedItems.Count > 0)
                {
                    for (int i = 0; i < RecentFilesList.SelectedItems.Count; i++)
                    {
                        GridRecentFilesList.SelectedItems.Add(RecentFilesList.SelectedItems[i]);
                    }

                }
            }
        }

        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            try
            {
                var contexMenu = sender as ContextMenu;
                if (contexMenu != null)
                {
                    foreach (var item in contexMenu.Items)
                    {
                        var menuitem = item as MenuItem;
                        menuitem.Visibility = Visibility.Visible;
                    }
                }
                if (RecentFilesList.SelectedItems.Count >= 2)
                {
                    foreach (var item in contexMenu.Items)
                    {
                        var menuitem = item as MenuItem;
                        if (menuitem.Name == "FilePathMenuItem")
                            menuitem.Visibility = Visibility.Collapsed;
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// 禁用触发鼠标右键，列表项被选中
        /// </summary>
        private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Trace.WriteLine("Preview MouseRightButtonDown");
            e.Handled = true;
        }

        #endregion


        #region Events


        /// <summary>
        /// 右键菜单：删除文件记录
        /// </summary>
        private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (RecentFilesList.SelectedItems == null || RecentFilesList.SelectedItems.Count == 0)
            {
                var item = (sender as MenuItem).DataContext as OpenFileInfo;
                if (item != null)
                {
                    ViewModel?.RemoveFileItemCommand.Execute(item);
                }
            }
            else
            {
                ViewModel?.RemoveFilesFromContainerCommand.Execute(RecentFilesList.SelectedItems);
            }

        }

        /// <summary>
        /// 右键菜单：打开文件路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if ((sender as MenuItem) != null)
            {
                var menuItem = sender as MenuItem;
                var file = menuItem.DataContext as OpenFileInfo;
                ViewModel?.ExplorerFileCommand.Execute(file);
            }
        }

        /// <summary>
        /// 双击打开最近列表
        /// </summary>
        private void RecentFilesList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ListViewItem;
            if (item != null && (item.DataContext as OpenFileInfo) != null)
            {
                var fileInfo = item.DataContext as OpenFileInfo;
                if(fileInfo != null)
                {
                    ViewModel?.OpenRecentFilesCommand.Execute(fileInfo);
                }
            }

        }

        /// <summary>
        /// 文件列表项：打开文件
        /// </summary>
        private void DocItemListViewControl_OpenFileItemHandler(object sender, RoutedEventArgs e)
        {
            var item = (sender as MenuItem).DataContext as OpenFileInfo;
            if (item != null)
            {
                ViewModel?.OpenRecentFilesCommand.Execute(item);
            }
        }

        /// <summary>
        ///  文件列表项：删除文件记录
        /// </summary>
        private void DocItemListViewControl_DeleteItemHandler(object sender, RoutedEventArgs e)
        {
            var item = (sender as MenuItem).DataContext as OpenFileInfo;
            if (item != null)
            {
                ViewModel?.RemoveFileItemCommand.Execute(item);
            }
        }

        #endregion
    }
}
