using Dropbox.Api;
using PDF_Office.Model.CloudDrive;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
{


    public class DropbBoxManager
    {
        private static DropbBoxManager instance;
        public static DropbBoxManager DropbBox => instance ?? (instance = new DropbBoxManager());

        private DropbBoxManager()
        {
        }



        #region 用户帐号

        /// <summary>
        /// 登录
        /// </summary>
        public async Task<bool> LoginUser()
        {
            bool result = false;
            DropbBoxUserItem dropbBoxUserItem = new DropbBoxUserItem();
            dropbBoxUserItem.cloudType = CloudType.DropBox;
            result = await dropbBoxUserItem.LoginUser();
            return result;
        }

        /// <summary>
        /// 移除用户
        /// </summary>
        public async Task<bool> RemoveUser(DropbBoxUserItem user)
        {
            var result = await user.RemoveUser();
            if(result == true)
            {
                Cloud.CloudLists.Remove(user);
            }
            return result;
        }

        #endregion


        #region 文件

        public async Task<List<DropbBoxFiles>> GetFolder(FileOperation fileOperation)
        {
            var user = fileOperation.User as DropbBoxUserItem;
            if (user != null)
            {
                if (fileOperation.DoFile == null)
                {
                    return await user.RefreshList();
                }
                else
                {
                    return await user.RefreshList((fileOperation.DoFile as DropbBoxFiles).Id);
                }
              
            }
            return null;
        }

        public async Task<bool> UpLoadFile(FileOperation fileOperation)
        {
            bool result = false;
            var user = fileOperation.User as DropbBoxUserItem;
            if (user != null && fileOperation.DoFile as DropbBoxFiles != null)
            {
                var pathDisplay = (fileOperation.DoFile as DropbBoxFiles).PathDisplay;
                result = await user.UpLoadFile(fileOperation.SourceFile, fileOperation.SourceFileName,pathDisplay);
            }
            return result;
        }

        public async Task<string> DownloadFile(FileOperation fileOperation, string savePath)
        {
            string result = "";
            var user = fileOperation.User as DropbBoxUserItem;
            if (user != null)
            {
                result = await user.Download((fileOperation.DoFile as DropbBoxFiles).Name, savePath);
               
            }
            return result;
        }
        #endregion
    }
}
