using Microsoft.Win32;
using PDF_Office.Model.CloudDrive;
using PDF_Office.ViewModels.HomePanel.CloudDrive;
using PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.HomePanel.CloudDrive
{
    /// <summary>
    /// CloudFilesContent.xaml 的交互逻辑
    /// </summary>
    public partial class CloudFilesContent : UserControl
    {
     
        private CloudFilesContentViewModel ViewModel => DataContext as CloudFilesContentViewModel;

        private List<CloudBoxItem> CloudeDrives = new List<CloudBoxItem>();

        private CloudType cloudType;
        public CloudFilesContent()
        {
            InitializeComponent();
            InitCloudDrive();
            this.Loaded += usercontrol_Loaded;
        }

        private void InitCloudDrive()
        {
            CloudeDrives = Cloud.InitCloudBoxs();
            combCloudDrive.ItemsSource = CloudeDrives;
            combCloudDrive.SelectedIndex = 0;
        }

        private void usercontrol_Loaded(object sender, RoutedEventArgs e)
        {
          if(ViewModel != null)
            {
                ViewModel.CheckDriveUsers();
                if (Listusers.ItemsSource == null)
                    Listusers.ItemsSource = Cloud.CloudLists;
            }
        }

        private void combCloudDrive_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cloudDriveItem = combCloudDrive.SelectedItem as CloudBoxItem;
            if (cloudDriveItem != null)
            {
                ViewModel.CheckDriveCommand.Execute(cloudDriveItem);
            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {

         
        }

        private UserBaseItem CurrentUser;

        private async void Listusers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cloudFileUser = Listusers.SelectedItem as UserBaseItem;
            if(cloudFileUser != null)
            {
                CurrentUser = cloudFileUser;

                switch(cloudFileUser.cloudType)
                {
                    case CloudType.GoogleDrive:
                        if ((CurrentUser as GoogleDriveUserItem) != null)
                        {
                            var item = CurrentUser as GoogleDriveUserItem;
                            var files = await item.GetDriveFiles(item.Service);
                            ObservableCollection<GoogleDriveFiles> filesList = new ObservableCollection<GoogleDriveFiles>();
                            foreach (var file in files)
                            {
                                if (file.Name.EndsWith(".pdf") == true)
                                {
                                    filesList.Add(file);
                                }
                            }
                            ListvmFiles.ItemsSource = filesList;
                        }
                        break;

                    case CloudType.DropBox:
                        if ((CurrentUser as DropbBoxUserItem) != null)
                        {
                            var item = CurrentUser as DropbBoxUserItem;
                            var files = await item.RefreshList();
                            ObservableCollection<DropbBoxFiles> filesList = new ObservableCollection<DropbBoxFiles>();
                            foreach (var file in files)
                            {
                                if (file.Name.EndsWith(".pdf") == true)
                                {
                                    filesList.Add(file);
                                }
                            }
                            ListvmFiles.ItemsSource = filesList;
                        }
                        break;

                }
               
            }
        }

        private async void OpenDocMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var menuItem = sender as MenuItem;
            if (menuItem == null)
                return;

            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            switch (CurrentUser.cloudType)
            {
                case CloudType.GoogleDrive:
               
                    var item = menuItem.DataContext as GoogleDriveFiles;
                    if (menuItem == null || item == null)
                        return;

                    var googleItem = CurrentUser as GoogleDriveUserItem;
                    var filename = await googleItem.DownloadGoogleFile(item, docPath);
                    if (filename != null)
                    {
                        string[] filePaths = { filename };
                        await Task.Delay(3);
                        MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
                        parentWindow.LoadPdfViewer(filePaths);
                    }
                    break;

                case CloudType.DropBox:

                    var file = menuItem.DataContext as DropbBoxFiles;
                    var dropBoxItem = CurrentUser as DropbBoxUserItem;

                    var dropBoxFileName = await dropBoxItem.Download(file.Name, docPath);
                    if (dropBoxFileName != null)
                    {
                        string[] filePaths = { dropBoxFileName };
                        await Task.Delay(3);
                        MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
                        parentWindow.LoadPdfViewer(filePaths);
                    }
                    break;

            }



           
        }

       
    }
}
