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 ContextMenu FlyoutMenu;
        private ContextMenu UserFlyoutMenu;
        private List<CloudBoxItem> CloudeDrives = new List<CloudBoxItem>();

        public CloudFilesContent()
        {
            InitializeComponent();
            InitCloudDrive();
            this.Loaded -= usercontrol_Loaded;
            this.Loaded += usercontrol_Loaded;
        }

        private void InitCloudDrive()
        {
            CloudeDrives = Cloud.InitCloudBoxs();
            combCloudDrive.ItemsSource = CloudeDrives;
            combCloudDrive.SelectedIndex = 0;
            FlyoutMenu = Resources["FlyoutMenu"] as ContextMenu;
            UserFlyoutMenu = Resources["UserFlyoutMenu"] as ContextMenu;
            
        }

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


        private UserBaseItem CurrentUser;

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

                switch(cloudFileUser.cloudType)
                {
                    case CloudType.GoogleDrive:
                        {
                            FileOperation fileOperation = new FileOperation(cloudFileUser, null);
                            ViewModel?.OpenFolderCommand?.Execute(fileOperation);
                        }
                        break;

                    case CloudType.DropBox:
                        {
                            FileOperation fileOperation = new FileOperation(cloudFileUser, null);
                            ViewModel?.OpenFolderCommand?.Execute(fileOperation);
                        }
                        break;
                }
            }
        }

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

            var file = menuItem.DataContext as FilesBaseItem;
            if (file != null)
            {
                var cloudFileUser = Listusers.SelectedItem as UserBaseItem;
                if (cloudFileUser != null)
                {
                    FileOperation fileOperation = new FileOperation(cloudFileUser, file);
                    ViewModel?.OpenFileCommand?.Execute(fileOperation);
                }

            }


        }

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

            var user = menuItem.DataContext as UserBaseItem;
            if (user != null)
            {
                ViewModel?.RemoveUserCommand?.Execute(user);

            }
        }
        private void SelectListFolder_Click(object sender, MouseButtonEventArgs e)
        {
            var folderItem = (sender as FrameworkElement).DataContext as FolderBaseItem;
            if (folderItem != null)
            {
                ViewModel?.SelectedFolderCommand?.Execute(folderItem);
            }

        }

        private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var frame = sender as FrameworkElement;
            if (frame != null && frame.DataContext as FilesBaseItem != null)
            {
                var file = frame.DataContext as FilesBaseItem;
                if ((file == null || file.IsFolder == false) && FlyoutMenu != null)
                {
                    FlyoutMenu.DataContext = file;
                    FlyoutMenu.IsOpen = true;
                }
            }
        }

        /// <summary>
        /// 打开文件夹，显示文件列表内容
        /// </summary>
        private void ListViewItem_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var frame = sender as FrameworkElement;
            if (frame != null && frame.DataContext as FilesBaseItem != null)
            {
                var file = frame.DataContext as FilesBaseItem;
                if (file != null && file.IsFolder == true)
                {
                    var cloudFileUser = Listusers.SelectedItem as UserBaseItem;
                    if (cloudFileUser != null)
                    {
                        FileOperation fileOperation = new FileOperation(cloudFileUser, file);
                        ViewModel?.OpenFolderCommand?.Execute(fileOperation);
                    }

                }
            }
        }

        private void userItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var frame = sender as FrameworkElement;
            if (frame != null && frame.DataContext as UserBaseItem != null)
            {
                var user = frame.DataContext as UserBaseItem;
                if (user != null && UserFlyoutMenu != null)
                {
                    UserFlyoutMenu.DataContext = user;
                    UserFlyoutMenu.IsOpen = true;
                }
            }
        }

        private void UpLoadFileBtn_Click(object sender, RoutedEventArgs e)
        {
            var folder = ListFolder.Items[ListFolder.Items.Count - 1] as FolderBaseItem;
            if (folder != null && folder.Operation != null)
            {
                FileOperation fileOperation = new FileOperation(CurrentUser, folder.Operation.DoFile);
                ViewModel?.UpLoadFileCommand?.Execute(fileOperation);
            }

        }
    }
}
