using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using PDF_Office.CustomControl;
using PDF_Office.Helper;
using PDF_Office.Properties;
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 RecentFilesView : UserControl
    {
       
        private ObservableCollection<OpenFileInfo> RecentFilesGroup = new ObservableCollection<OpenFileInfo>();
        public RecentFilesView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            RecentFilesGroup = new ObservableCollection<OpenFileInfo>(Settings.Default.RecentOpenFiles);
            RecentFilesList.ItemsSource = RecentFilesGroup;
            GridRecentFilesList.ItemsSource = RecentFilesGroup;
            RecentFilesList.SelectedItem = null;
            ShowListViewContentUI();
        }

        private void SetLangText()
        {

        }

        private void InitContextMenu()
        {

        }

        private void InitUI()
        {

        }


        #region UI
        #region 列表和网格模式
        private void ListModeBtn_Click(object sender, RoutedEventArgs e)
        {
            SwitchFilesMode(sender);

            if (BtnListMode.IsChecked == false)
                BtnListMode.IsChecked = true;
        }

        private void GridModeBtn_Click(object sender, RoutedEventArgs e)
        {
            SwitchFilesMode(sender);

            if (BtnGridMode.IsChecked == false)
                BtnGridMode.IsChecked = true;
        }

        private void SwitchFilesMode(object sender)
        {
            try
            {
                var btn = sender as CustomIconToggleBtn;
                switch (btn.Tag.ToString())
                {
                    case "ListMode":
                        BtnGridMode.IsChecked = false;

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

                        }
                        
                        break;

                    case "GridMode":
                        BtnListMode.IsChecked=false;

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

                        }
                        
                        break;
                }
              
            }
            catch { }
        }

        private void ShowListViewContentUI()
        {
            if (RecentFilesList.Items.Count == 0)
            {
                BlankContentPanel.Visibility = Visibility.Visible;
                RecentFilesList.Visibility = Visibility.Collapsed;
                PnlRightBtns.Visibility = Visibility.Collapsed;
            }
            else
            {
                RecentFilesList.Visibility = Visibility.Visible;
                PnlRightBtns.Visibility = Visibility.Visible;
                BlankContentPanel.Visibility = Visibility.Collapsed;
            }
        }

        #endregion

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            string msg = "";
            int SelectedItemsType = 0;
            if (RecentFilesList.SelectedItems.Count == RecentFilesList.Items.Count || RecentFilesList.SelectedItems.Count == 0)
            {
                if (RecentFilesList.SelectedItems.Count == 1 && RecentFilesList.Items.Count == 1)
                    msg = "ClearFile";
                else
                    msg = "AllClearFiles";

                SelectedItemsType = 0;
            }
            else if (RecentFilesList.SelectedItems.Count == 1)
            {
                msg = "ClearFile";
                SelectedItemsType = 1;
            }
            else
            {
                msg = "ClearSelectedFiles";
                SelectedItemsType = 2;
            }

            winform.DialogResult result = MessageBoxEx.Show(msg, "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question);
            if (result == winform.DialogResult.OK)
            {
                RemoveRecentFilesFrom(SelectedItemsType);
            }
        }

        private void RecentFilesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
        }

        private void GridRecentFilesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        
        }

        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
           
        }

        /// <summary>
        /// 未被选中的项目，或只有一个被选中的项
        /// </summary>
        private void showMoreContexMenu(ContextMenu contexMenu)
        {
            foreach (var item in contexMenu.Items)
            {
                var menuitem = item as MenuItem;
                menuitem.Visibility = Visibility.Visible;
               
            }
        }

        private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Trace.WriteLine("Preview MouseRightButtonDown");
            e.Handled = true;
        }
        #endregion

        #region Events
        /// <summary>
        /// 删除最近文件的操作
        /// </summary>
        /// <param name="selectedItemsType">0:全部文件；1:一个文件；2:多个文件</param>
        private void RemoveRecentFilesFrom(int selectedItemsType)
        {
            if (selectedItemsType == 0)
            {
                SettingHelper.RemoveAllRecentOpenFiles();
                RecentFilesGroup.Clear();
            }
            else if (selectedItemsType == 1)
            {
                var file = RecentFilesList.SelectedItems[0] as OpenFileInfo;
                SettingHelper.RemoveRecentOpenFile(file.FilePath);
                RecentFilesGroup.Remove(file);
            }
            else
            {
                List<OpenFileInfo> OpenFileInfos = new List<OpenFileInfo>();
                foreach (var item in RecentFilesList.SelectedItems)
                {
                    var openFileInfo = item as OpenFileInfo;
                    OpenFileInfos.Add(openFileInfo);
                }

                foreach (var item in OpenFileInfos)
                {
                    SettingHelper.RemoveRecentOpenFile(item.FilePath);
                    RecentFilesGroup.Remove(item);
                }
                OpenFileInfos.Clear();
            }

            ShowListViewContentUI();
        }

        private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
        {

            DeleteFileItem(sender);
        }

        private void DeleteItemHandler_Click(object sender, RoutedEventArgs e)
        {
            DeleteFileItem(sender);
        }

        private bool IsSelectedFile(object sender)
        {

            return true;
        }

        private void DeleteFileItem(object sender)
        {
            if (RecentFilesList.SelectedItems.Count > 1)
            {
                var isSelected = IsSelectedFile(sender);
                if (!isSelected)
                {
                    ShowListViewContentUI();
                    return;
                }

                string msg = "";
                if (RecentFilesList.SelectedItems.Count == RecentFilesList.Items.Count)
                    msg = "RecentFiles_AllClearMsg";
                else
                    msg = "RecentFiles_SelectedClearMsg";

                if (MessageBoxEx.Show(msg, "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question) == winform.DialogResult.OK)
                    RemoveRecentFilesFrom(2);
                else
                    return;
            }
            else
            {
                if (MessageBoxEx.Show("RecentFiles_ClearMsg", "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question) != winform.DialogResult.OK)
                    return;

                OpenFileInfo file = null;
                if ((sender as MenuItem) != null)
                {
                    var menuItem = sender as MenuItem;
                    file = menuItem.DataContext as OpenFileInfo;
                    if (file != null)
                    {
                        SettingHelper.RemoveRecentOpenFile(file.FilePath);
                        RecentFilesGroup.Remove(file);
                    }
                }
                else if ((sender as Button) != null)
                {
                    var btn = sender as Button;
                    file = btn.DataContext as OpenFileInfo;
                    if (file != null)
                    {
                        SettingHelper.RemoveRecentOpenFile(file.FilePath);
                        RecentFilesGroup.Remove(file);
                    }

                }
            }
            ShowListViewContentUI();
        }

        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 (File.Exists(fileInfo.FilePath))
                {
                    MainWindow parentWindow = Window.GetWindow(this) as MainWindow;

                    string[] filePath = new string[1];
                    filePath[0] = fileInfo.FilePath;
                    parentWindow.LoadPdfViewer(filePath);
                }
                else
                {
                    //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileNotExistWarning"));
                    SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
                    RecentFilesGroup.Remove(fileInfo);
                    ShowListViewContentUI(); 
                }

            }

        }

        private async void OpenFiles_Click(object sender, RoutedEventArgs e)
        {

            var dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            dlg.Filter = Properties.Resources.OpenDialogFilter;

            if (dlg.ShowDialog() == true)
            {
                await Task.Delay(3);
                MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
                parentWindow.LoadPdfViewer(dlg.FileNames);
            }
        }
        #endregion

        private void PrintFile(CPDFViewer pdfViewer)
        {
           
        }


        private void PrintMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void FileInfoMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ShareMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

      
    }
}
