using Microsoft.Win32;
using PDF_Office.Helper;
using PDF_Office.Model.CloudDrive;
using PDF_Office.Properties;
using PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PDF_Office.ViewModels.HomePanel.CloudDrive
{

    //各云盘事件
    public class CloudFilesContentViewModel : BindableBase
    {
        #region 云盘变量
        public CloudDriveManager CloudManager => CloudDriveManager.CloudManager;
        private ObservableCollection<FolderBaseItem> _folderItems = new ObservableCollection<FolderBaseItem>();
        public ObservableCollection<FolderBaseItem> FolderItems
        {
            get { return _folderItems; }
            set { SetProperty (ref _folderItems, value); }
        }
        private ObservableCollection<FilesBaseItem> _filesList = new ObservableCollection<FilesBaseItem>();
        public ObservableCollection<FilesBaseItem> FilesList
        {
            get { return _filesList; }
            set { SetProperty(ref _filesList, value); }
        }
      
        private CloudType _currentCloudType = CloudType.GoogleDrive;
        public CloudType CurrentCloudType
        {
            get { return _currentCloudType; }
            set { SetProperty(ref _currentCloudType, value); }
        }
        #endregion

        #region  事件
        /// <summary>
        /// 根据目录，更新文件列表
        /// </summary>
        public DelegateCommand<FileOperation> OpenFolderCommand { get; set; }

        /// <summary>
        /// 选中文件夹，更新指定文件夹里的文件列表
        /// </summary>
        public DelegateCommand<FolderBaseItem> SelectedFolderCommand { get; set; }
        /// <summary>
        /// 下载并打开文件
        /// </summary>
        public DelegateCommand<FileOperation> OpenFileCommand { get; set; }

        /// <summary>
        /// 上传文件
        /// </summary>
        public DelegateCommand<FileOperation> UpLoadFileCommand { get; set; }

        public DelegateCommand<CloudBoxItem> LoginCommand { get; set; }
        public DelegateCommand<UserBaseItem> RemoveUserCommand { get; set; }

        #endregion

        public CloudFilesContentViewModel()
        {
            InitVariable();
            InitCommand();
        }


        private void InitVariable()
        {

        }

        private void InitCommand()
        {
            OpenFolderCommand = new DelegateCommand<FileOperation>(OpenFolder);
            SelectedFolderCommand = new DelegateCommand<FolderBaseItem>(SelectedFolder);

            OpenFileCommand = new DelegateCommand<FileOperation>(OpenFile);
            UpLoadFileCommand = new DelegateCommand<FileOperation>(UpLoadFile);

            LoginCommand = new DelegateCommand<CloudBoxItem>(LoginUser);
            RemoveUserCommand = new DelegateCommand<UserBaseItem>(RemoveUser);

        }

        #region 云盘公用接口

        #endregion

        #region 用户帐号

        /// <summary>
        /// 登录
        /// </summary>
        private async void LoginUser(CloudBoxItem cloudDriveItem)
        {
            if (cloudDriveItem != null)
                await CloudManager.LoginUser(cloudDriveItem.CloudDiskType);
            
        }

        /// <summary>
        /// 移除用户
        /// </summary>
        private async void RemoveUser(UserBaseItem user)
        {
            if(user != null)
            {
              var result =  await CloudManager.RemoveUser(user);
                if(result)
                {
                    FilesList.Clear();
                    FolderItems.Clear();
                }
            }
        }

        #endregion


        #region 文件

        private async void OpenFolder(FileOperation fileOperation)
        {
            FilesList = await CloudManager.OpenFolder(fileOperation);
            AddFolder(fileOperation);
        }

        /// <summary>
        /// 在文件夹列表，选择文件夹
        /// </summary>
        private void SelectedFolder(FolderBaseItem obj)
        {
            if (obj != null)
            {
                var objIndex = FolderItems.IndexOf(obj);
                var count = FolderItems.Count;

                if (objIndex == count - 1)
                    return;

                if (obj.Level == -1)
                {

                    for (int i = 1; i < count; i++)
                    {
                        FolderItems.RemoveAt(i);
                    }
                }
                else
                {
                    if (objIndex != -1)
                    {
                        for (int i = count - 1; i > objIndex; i--)
                        {
                            FolderItems.Remove(FolderItems[i]);
                        }
                    }
                }

                SelectedFolder(obj.Operation);
            }
        }

        /// <summary>
        /// 在文件列表点击文件夹，而更新文件列表
        /// </summary>
        /// <param name="fileOperation"></param>
        private async void SelectedFolder(FileOperation fileOperation)
        {
            FilesList = await CloudManager.OpenFolder(fileOperation);
        }

        private void AddFolder(FileOperation folder)
        {

            if (folder != null)
            {
                if (folder.DoFile != null)
                {
                    FolderBaseItem folderItem = new FolderBaseItem();
                    folderItem.FolderName = folder.DoFile.Name;
                    folderItem.Level = folder.DoFile.Level;
                    folderItem.Operation = folder;
                    FolderItems.Add(folderItem);
                }
                else
                {
                    FolderItems.Clear();
                    FolderBaseItem folderItem = new FolderBaseItem();
                    folderItem.FolderName = "云文档";
                    folderItem.Level = -1;
                    folderItem.Operation = folder;
                    FolderItems.Add(folderItem);
                }
            }
        }

        /// <summary>
        /// 下载并打开文件
        /// </summary>
        private async void OpenFile(FileOperation fileOperation)
        {
            await CloudManager.OpenFile(fileOperation);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        private async void UpLoadFile(FileOperation fileOperation)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = Properties.Resources.OpenDialogFilter;
            openFileDialog.Multiselect = true;
            if ((bool)openFileDialog.ShowDialog())
            {
                if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
                {
                    fileOperation.SourceFile = openFileDialog.FileName;
                    fileOperation.SourceFileName = openFileDialog.SafeFileName;
                    var result = await CloudManager.UpLoadFile(fileOperation);
                    if (result)
                    {
                        FilesList = await CloudManager.OpenFolder(fileOperation);
                        MessageBox.Show("文件上传完成");
                    }
                    else
                    {
                        MessageBox.Show("文件上传失败");
                    }
                }
            }

        }
        #endregion

    }
}
