using Microsoft.Win32;
using PDF_Office.EventAggregators;
using PDF_Office.Helper;
using PDF_Office.Model;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PDF_Office.ViewModels
{
    public class HomeContentViewModel : BindableBase, INavigationAware
    {
        private string fileName = "Home";
        public string FileName
        {
            get { return fileName; }
            set { SetProperty(ref fileName, value); }
        }

        private string regionName;

        public string ToolRegionName
        {
            get { return regionName; }
            set { SetProperty(ref regionName, value); }
        }

        private Visibility isLoading = Visibility.Collapsed;

        public Visibility IsLoading
        {
            get { return isLoading; }
            set
            {
                SetProperty(ref isLoading, value);
            }
        }

        private MainContentViewModel mainContentViewModel;

        public DelegateCommand OpenFileCommand { get; set; }

        public DelegateCommand<string> ShowToolCommand { get; set; }

        public DelegateCommand CreateBlackPDFCommand { get; set; }

        public DelegateCommand CreateFromOtherFile { get; set; }

        public IRegionManager toolregion;

        public IEventAggregator eventer;

        public HomeContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            toolregion = regionManager;
            eventer = eventAggregator;

            ToolRegionName = Guid.NewGuid().ToString();
            OpenFileCommand = new DelegateCommand(OpenFile);
            ShowToolCommand = new DelegateCommand<string>(ShowToolContent);
            CreateBlackPDFCommand = new DelegateCommand(CreatBlankPDF);
            CreateFromOtherFile = new DelegateCommand(createFromOtherFile);

            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
            {
                toolregion.RequestNavigate(ToolRegionName, "Guid");
            }));
        }

        /// <summary>
        /// 显示右侧不同的工具栏页面
        /// </summary>
        /// <param name="view"></param>
        public void ShowToolContent(string view)
        {
            toolregion.RequestNavigate(ToolRegionName, view);
        }

        /// <summary>
        /// 从其他格式文件创建PDF
        /// </summary>
        private void createFromOtherFile()
        {

        }

        /// <summary>
        /// 打开文件
        /// </summary>
        public async void OpenFile()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
            openFileDialog.Multiselect = true;
            if ((bool)openFileDialog.ShowDialog())
            {
                IsLoading = Visibility.Visible;
                await Task.Delay(3);
                if (openFileDialog.FileNames.Count() == 1)
                {
                    if (App.OpenedFileList.Contains(openFileDialog.FileName))
                    {
                        App.mainWindowViewModel.SelectItem(openFileDialog.FileName);
                    }
                    else
                    {
                        mainContentViewModel.OpenFile(openFileDialog.FileName);
                    }
                    ToolMethod.SetFileThumbImg(openFileDialog.FileName);
                }
                else
                {
                    var fileList = openFileDialog.FileNames.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
                    if (fileList.Count <= 0)
                        return;

                    mainContentViewModel.OpenFile(fileList[0]);
                    for (int i = 1; i < fileList.Count(); i++)
                    {
                        if (!App.OpenedFileList.Contains(fileList[i]))
                        {
                            App.mainWindowViewModel.AddTabItem(fileList[i]);
                        }
                        ToolMethod.SetFileThumbImg(fileList[i]);
                    }
                }
                IsLoading = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 打开其他格式文件
        /// </summary>
        public void OpenFromOtherFile()
        {

        }

        public void CreatBlankPDF()
        {
            mainContentViewModel.CreateFile();
        }

        #region Navigate
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var mainVM = navigationContext.Parameters[ParameterNames.MainViewModel] as MainContentViewModel;
            if (mainVM != null)
            {
                mainContentViewModel = mainVM;
            }
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }
        #endregion
    }
}
