using ComPDFKitViewer.PdfViewer;
using Microsoft.Win32;
using PDF_Office.EventAggregators;
using PDF_Office.Views;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
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;
using System.Windows.Controls;

namespace PDF_Office.ViewModels
{
    public class MainContentViewModel : BindableBase, INavigationAware
    {
        private string fileName = "Home";

        public string FileName
        {
            get { return fileName; }
            set { SetProperty(ref fileName, value); }
        }

        private string filePath;

        public string FilePath
        {
            get { return filePath; }
            set
            {
                SetProperty(ref filePath, value);
                if (!string.IsNullOrEmpty(filePath))
                {
                    FileName = System.IO.Path.GetFileName(filePath);
                }

            }
        }

        private Visibility fileChanged;

        public Visibility FileChanged
        {
            get { return fileChanged; }
            set { SetProperty(ref fileChanged, value); }
        }

        private Visibility isLoading = Visibility.Collapsed;

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

        public CPDFViewer PDFViewer { get; set; }


        public DelegateCommand<object> CloseTab { get; set; }

        public DelegateCommand<object> Loaded { get; set; }


        private string regionName;

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

        public IRegionManager toolregion;

        public IEventAggregator eventer;

        public IContainerProvider container;

        public MainContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider containerProvider)
        {
            toolregion = regionManager;
            eventer = eventAggregator;
            container = containerProvider;

            CloseTab = new DelegateCommand<object>(CloseTabItem);

            MainContentRegionName = Guid.NewGuid().ToString();

            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
            {
                NavigationParameters parameters = new NavigationParameters
                {
                    {
                        "MainViewModel", this
                    }
                };
                if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
                    toolregion.RequestNavigate(MainContentRegionName, "HomeContent", parameters);
            }));
        }

        private void CloseTabItem(object item)
        {
            App.mainWindowViewModel?.CloseTabItem(item);
            App.OpenedFileList.Remove(FilePath);
        }


        public void OpenFile(string filePath)
        {
            FilePath = filePath;
            NavigationParameters parameters = new NavigationParameters { { "MainViewModel", this } };
            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
                {
                    if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
                        toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);



                }));
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.Count <= 0)
                return;
            var filepath = navigationContext.Parameters["filePath"];
            if (filepath != null)
            {
                OpenFile(filepath.ToString());
            }
        }

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

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }
    }
}
