public async Task<bool> SignIn()
 {
     if (!this.IsSignIn()) return false;
     PtcAccount account = await this.GetPtcAccountAsync();
     if (account == null) return false;
     this.myAccount = account;
     return true;
 }
        public async Task<bool> UpdatePtcAccount(PtcAccount account)
        {
            MSPtcAccount mspa = PtcAccount.ConvertToMSPtcAccount(account);
            List<MSStorageAccount> saList = new List<MSStorageAccount>();
            await App.MobileService.GetTable<MSPtcAccount>().UpdateAsync(mspa);

            this.myAccount = account;
            return true;
        }
        public async Task<bool> DeletePtcAccount(PtcAccount account)
        {
            MSPtcAccount mspa = PtcAccount.ConvertToMSPtcAccount(account);
            await App.MobileService.GetTable<MSPtcAccount>().DeleteAsync(mspa);

            App.ApplicationSettings.Remove(PTCACCOUNT_ID);
            App.ApplicationSettings.Save();
            this.myAccount = null;
            return true;
        }
        public async Task<bool> CreateNewPtcAccountAsync(PtcAccount account)
        {
            MSPtcAccount mspa = PtcAccount.ConvertToMSPtcAccount(account);
            PtcAccount p = await this.GetPtcAccountAsync(account.Email);
            if (p != null) return false;
            await App.MobileService.GetTable<MSPtcAccount>().InsertAsync(mspa);

            this.SavePtcId(account.Email, account.ProfilePassword);
            this.myAccount = account;
            return true;
        }
        public static PtcAccount ConvertToPtcAccount(MSPtcAccount mspa)
        {
            PtcAccount account = new PtcAccount();

            account.Name            = mspa.name;
            account.Email           = mspa.email;
            account.PhoneNumber     = mspa.phone_number;
            account.ProfilePassword = mspa.profile_password;
            account.UsedSize        = mspa.used_size;
            account.AccountType     = new StorageAccountType();
            return(account);
        }
        public async Task<PtcAccount> GetPtcAccountAsync()
        {
            // TODO : Reset after presentation

            //TaskCompletionSource<PtcAccount> tcs = new TaskCompletionSource<PtcAccount>();
            //if (this.myAccount == null)
            //{
            //    if (App.ApplicationSettings.Contains(PTCACCOUNT_ID))
            //    {
            //        try
            //        {
            //            PtcAccount account = await this.GetPtcAccountAsync((string)App.ApplicationSettings[PTCACCOUNT_ID]);
            //            if (account == null) tcs.SetResult(null);
            //            else tcs.SetResult(account);
            //        }
            //        catch
            //        {
            //            tcs.SetResult(null);
            //        }
            //    }
            //    else
            //    {
            //        tcs.SetResult(null);
            //    }
            //}
            //else
            //{
            //    tcs.SetResult(this.myAccount);
            //}
            //return tcs.Task.Result;

            TaskCompletionSource<PtcAccount> tcs = new TaskCompletionSource<PtcAccount>();
            PtcAccount account = new PtcAccount();
            account.Email = App.AccountManager.GetPtcId();
            account.AccountType = new StorageAccountType();
            account.Name = "User Name";
            account.PhoneNumber = "010-3795-8626";
            account.UsedSize = 10.0;
            tcs.SetResult(account);
            return tcs.Task.Result;
        }
        public async Task<PtcAccount> GetPtcAccountAsync(string accountId, string password = null)
        {
            Expression<Func<MSPtcAccount, bool>> lamda = (a => a.email == accountId);
            if (password != null)
                lamda = (a => a.email == accountId && a.profile_password == password);

            MobileServiceCollection<MSPtcAccount, MSPtcAccount> list =
                await App.MobileService.GetTable<MSPtcAccount>().Where(lamda).ToCollectionAsync();

            if (list.Count >= 1)
            {
                PtcAccount account = PtcAccount.ConvertToPtcAccount(list.First());
                this.myAccount = account;
                return account;
            }
            else
                return null;
        }
        //public async Task<bool> CreateStorageAccountAsync(StorageAccount sa)
        //{
        //    MSStorageAccount mssa = StorageAccount.ConvertToMSStorageAccount(sa);
        //    mssa.ptc_account_id = this.Email;
        //    try
        //    {
        //        await App.MobileService.GetTable<MSStorageAccount>().InsertAsync(mssa);
        //        StorageAccounts.Add(sa.StorageName, sa);
        //        return true;
        //    }
        //    catch(Exception ex)
        //    {
        //        Debug.WriteLine(ex.ToString());
        //        return false;
        //    }
        //}
        //private async Task<StorageAccount> GetStorageAccountAsync(string storageAccountId)
        //{
        //    MobileServiceCollection<MSStorageAccount, MSStorageAccount> accounts = null;
        //    try
        //    {
        //        accounts = await App.MobileService.GetTable<MSStorageAccount>()
        //            .Where(a => a.account_platform_id == storageAccountId)
        //            .ToCollectionAsync();
        //    }
        //    catch (MobileServiceInvalidOperationException ex)
        //    {
        //        Debug.WriteLine(ex.ToString());
        //        throw new Exception("AccountManager.GetAccount() ERROR");
        //    }

        //    if (accounts.Count == 1)
        //        return StorageAccount.ConvertToStorageAccount(accounts.First());
        //    else if (accounts.Count > 1)
        //        throw new Exception("AccountManager.GetAccount() ERROR");
        //    else
        //        return null;
        //}

        //public StorageAccount GetStorageAccountById(string storageAccountId)
        //{
        //    if(storageAccountId == null || string.Empty.Equals(storageAccountId)) return null;
        //    using (var itr = StorageAccounts.GetEnumerator())
        //    {
        //        while (itr.MoveNext())
        //        {
        //            if (storageAccountId.Equals(itr.Current.Value.Id))
        //                return itr.Current.Value;
        //        }
        //    }
        //    return null;
        //}

        //public StorageAccount GetStorageAccountByName(string storageName)
        //{
        //    if (StorageAccounts.ContainsKey(storageName))
        //        return StorageAccounts[storageName];
        //    else
        //        return null;
        //}
        //public IEnumerator<KeyValuePair<string, StorageAccount>> GetStorageAccountEnumerator()
        //{
        //    return StorageAccounts.GetEnumerator();
        //}

        public static MSPtcAccount ConvertToMSPtcAccount(PtcAccount pa)
        {
            MSPtcAccount mspa = new MSPtcAccount(pa.Name, pa.Email, pa.PhoneNumber, pa.ProfilePassword, 0.0, pa.AccountType.Id, "for_later_develop");

            return(mspa);
        }
 public static PtcAccount ConvertToPtcAccount(MSPtcAccount mspa)
 {
     PtcAccount account = new PtcAccount();
     account.Name = mspa.name;
     account.Email = mspa.email;
     account.PhoneNumber = mspa.phone_number;
     account.ProfilePassword = mspa.profile_password;
     account.UsedSize = mspa.used_size;
     account.AccountType = new StorageAccountType();
     return account;
 }
 //public async Task<bool> CreateStorageAccountAsync(StorageAccount sa)
 //{
 //    MSStorageAccount mssa = StorageAccount.ConvertToMSStorageAccount(sa);
 //    mssa.ptc_account_id = this.Email;
 //    try
 //    {
 //        await App.MobileService.GetTable<MSStorageAccount>().InsertAsync(mssa);
 //        StorageAccounts.Add(sa.StorageName, sa);
 //        return true;
 //    }
 //    catch(Exception ex)
 //    {
 //        Debug.WriteLine(ex.ToString());
 //        return false;
 //    }
 //}
 //private async Task<StorageAccount> GetStorageAccountAsync(string storageAccountId)
 //{
 //    MobileServiceCollection<MSStorageAccount, MSStorageAccount> accounts = null;
 //    try
 //    {
 //        accounts = await App.MobileService.GetTable<MSStorageAccount>()
 //            .Where(a => a.account_platform_id == storageAccountId)
 //            .ToCollectionAsync();
 //    }
 //    catch (MobileServiceInvalidOperationException ex)
 //    {
 //        Debug.WriteLine(ex.ToString());
 //        throw new Exception("AccountManager.GetAccount() ERROR");
 //    }
 //    if (accounts.Count == 1)
 //        return StorageAccount.ConvertToStorageAccount(accounts.First());
 //    else if (accounts.Count > 1)
 //        throw new Exception("AccountManager.GetAccount() ERROR");
 //    else
 //        return null;
 //}
 //public StorageAccount GetStorageAccountById(string storageAccountId)
 //{
 //    if(storageAccountId == null || string.Empty.Equals(storageAccountId)) return null;
 //    using (var itr = StorageAccounts.GetEnumerator())
 //    {
 //        while (itr.MoveNext())
 //        {
 //            if (storageAccountId.Equals(itr.Current.Value.Id))
 //                return itr.Current.Value;
 //        }
 //    }
 //    return null;
 //}
 //public StorageAccount GetStorageAccountByName(string storageName)
 //{
 //    if (StorageAccounts.ContainsKey(storageName))
 //        return StorageAccounts[storageName];
 //    else
 //        return null;
 //}
 //public IEnumerator<KeyValuePair<string, StorageAccount>> GetStorageAccountEnumerator()
 //{
 //    return StorageAccounts.GetEnumerator();
 //}
 public static MSPtcAccount ConvertToMSPtcAccount(PtcAccount pa)
 {
     MSPtcAccount mspa = new MSPtcAccount(pa.Name, pa.Email, pa.PhoneNumber, pa.ProfilePassword, 0.0, pa.AccountType.Id, "for_later_develop");
     return mspa;
 }