using PDF_Office.Helper;
using PDF_Office.Model.CloudDrive;
using PDF_Office.Properties;
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;

namespace PDF_Office.ViewModels.HomePanel.CloudDrive
{
   
    //各云盘的管理类
    //单实例化
    public class CloudDriveManager
    {

        #region 云盘类型
        public GoogleDriveManager GoogleDrive => GoogleDriveManager.GoogleDrive;
        public DropbBoxManager DropbBox => DropbBoxManager.DropbBox;
       
        #endregion

        public CloudType cloudType;

        private static CloudDriveManager instance;
        public static CloudDriveManager CloudManager => instance ?? (instance = new CloudDriveManager());
        private CloudDriveManager()
        {
        }


        #region 用户帐号

        /// <summary>
        /// 登录
        /// </summary>
        public async Task<bool> LoginUser(CloudType cloudType)
        {
            switch(cloudType)
            {
                case CloudType.GoogleDrive:
                   return await GoogleDrive.LoginUserCount();
                case CloudType.DropBox:
                    return await DropbBox.LoginUser();
            }
            return false;
        }

        public async Task<ObservableCollection<FilesBaseItem>> OpenFolder(FileOperation fileOperation)
        {
            ObservableCollection<FilesBaseItem> FilesList = null;
            switch (fileOperation.User.cloudType)
            {
                case CloudType.GoogleDrive:
                    {
                        var list = await GoogleDrive.GetFolder(fileOperation);
                        FilesList = new ObservableCollection<FilesBaseItem>();
                        foreach (var item in list)
                        {
                            FilesList.Add(item);
                        }
                    }

                    break;
                case CloudType.DropBox:
                    {
                        var list = await DropbBox.GetFolder(fileOperation);
                        FilesList = new ObservableCollection<FilesBaseItem>();
                        foreach (var item in list)
                        {
                            FilesList.Add(item);
                        }
                    }
                    break;
            }

            return FilesList;
        }

        public async Task<bool> OpenFile(FileOperation fileOperation)
        {
            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string tempPath = "";
            switch (fileOperation.User.cloudType)
            {
                case CloudType.GoogleDrive:
                    tempPath = await GoogleDrive.DownloadGoogleFile(fileOperation, docPath);
                    break;
                case CloudType.DropBox:
                    tempPath = await DropbBox.DownloadFile(fileOperation, docPath);
                    break;
            }

            if(string.IsNullOrEmpty(tempPath) == false)
            {
                string[] filePaths = { tempPath };
                await Task.Delay(3);
                LoadPdfViewer(filePaths);
            }

            return false;
        }

        public void LoadPdfViewer(string[] filePaths)
        {
            var content = App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel;
            if (filePaths.Count() == 1)
            {
                if (App.OpenedFileList.Contains(filePaths[0]))
                {
                    App.mainWindowViewModel.SelectItem(filePaths[0]);
                }
                else
                {
                    content.OpenFile(filePaths[0]);
                }
                ToolMethod.SetFileThumbImg(filePaths[0]);
            }
            else
            {
                var fileList = filePaths.ToList().Where(x => !App.OpenedFileList.Exists(y => y == x)).ToList();
                if (fileList.Count <= 0)
                    return;

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

            Settings.Default.Save();
        }

        public bool LoadedUsers()
        {
            bool isUsers = false;
            if (Cloud.CloudLists.Count > 0)
                isUsers = true;

            return isUsers;

        }
        /// <summary>
        /// 移除用户
        /// </summary>
        public async Task<bool> RemoveUser(UserBaseItem user)
        {
            bool result = false;
            switch (user.cloudType)
            {
                case CloudType.GoogleDrive:
                    {
                        var googleUser = user as GoogleDriveUserItem;
                        if(googleUser != null)
                        {
                            result = await GoogleDrive.RemoveUser(googleUser);
                        }
                    }
                    break;
                   
                case CloudType.DropBox:
                    {
                        var dropBoxUser = user as DropbBoxUserItem;
                        if (dropBoxUser != null)
                        {
                            result = await DropbBox.RemoveUser(dropBoxUser);
                        }
                    }
                    break;
            }

            return result;
        }

        #endregion


        #region 文件

        public async Task<bool> UpLoadFile(FileOperation fileOperation)
        {
            bool result = false;
            switch (fileOperation.User.cloudType)
            {
                case CloudType.GoogleDrive:
                    {
                        var googleUser = fileOperation.User as GoogleDriveUserItem;
                        if (googleUser != null)
                        {
                            result = await GoogleDrive.UpLoadFile(fileOperation);
                        }
                    }
                    break;

                case CloudType.DropBox:
                    {
                        var dropBoxUser = fileOperation.User as DropbBoxUserItem;
                        if (dropBoxUser != null)
                        {
                            result = await DropbBox.UpLoadFile(fileOperation);
                        }
                    }
                    break;
            }
            return result;
        }
     
        #endregion

    }
}
