using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using PDF_Office.Model.CloudDrive;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PDF_Office.ViewModels.HomePanel.CloudDrive.CloudDriveType
{
    //多用户UX交互
    public class GoogleDriveManager
    {
        private static GoogleDriveManager instance;
        public static GoogleDriveManager GoogleDrive => instance ?? (instance = new GoogleDriveManager());
        private GoogleDriveManager()
        {
             GoogleDriveStatic.GetFilesPathTemp();
             GoogleDriveStatic.GetCredentialsPath();
        }

        #region   请求身份验证

        /// <summary>
        /// 获取登录过的用户
        /// </summary>
        public async Task<bool> GetHistoryUsers()
        {
            var tuples = await GoogleDriveStatic.GetHistoryService();
            Cloud.CloudLists.Clear();
            foreach (var tuple in tuples)
            {

                if (tuple != null && tuple.Item1 != null && tuple.Item2 != null)
                {
                    AddGoogleDriveUser(tuple);
                }
            }

            if (Cloud.CloudLists.Count > 0)
            {
                return true;
            }
            return false;
        }

        public async void AddGoogleDriveUser(Tuple<DriveService, UserCredential> tuple)
        {
            GoogleDriveUserItem userItem = new GoogleDriveUserItem();
            userItem.Service = tuple.Item1;
            userItem.CurrentCredential = tuple.Item2;
            userItem.cloudType = CloudType.GoogleDrive;

            UserInfo user = new UserInfo();
            user = await userItem.GetUserAcountAsync();
            userItem.userInfo = user;

            Cloud.CloudLists.Add(userItem);
        }

        public async Task<bool> LoginUserCount()
        {
            var tuple = await GoogleDriveStatic.GetServiceAsync();
            bool result = false;
            if (tuple != null)
            {
                result = true;
                await GetUserInfo(tuple);//此处不可用异步处理，否则导致的结果是没法刷新集合呈现在UI上
            }

            return result;
        }

        public async Task<bool> GetUserInfo(Tuple<DriveService, UserCredential> tuple)
        {
            GoogleDriveUserItem userItem = new GoogleDriveUserItem();
            userItem.Service = tuple.Item1;
            userItem.CurrentCredential = tuple.Item2;
            userItem.cloudType = CloudType.GoogleDrive;

            UserInfo user = new UserInfo();
            user = await userItem.GetUserAcountAsync();
            userItem.userInfo = user;

            Cloud.CloudLists.Add(userItem);
            return true;
        }

        public async Task<bool> RemoveUser(GoogleDriveUserItem userIttem)
        {
           return await userIttem.RemoveUser();
        }

        #endregion
    }
}
