<commit_before>﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using DropNet;
using DropNet.Exceptions;
using DropNet.Models;
using GalaSoft.MvvmLight.Messaging;
using RestSharp;
using TodotxtTouch.WindowsPhone.Messages;

namespace TodotxtTouch.WindowsPhone.Service
{
	public class DropboxService
	{
		private DropNetClient _dropNetClient;

		private String _secret = String.Empty;
		private String _token = String.Empty;

		public bool WeHaveTokens
		{
			get { return !String.IsNullOrEmpty(Token) && !String.IsNullOrEmpty(Secret); }
		}

		/// <summary>
		/// Gets the Secret property.
		/// Changes to that property's value raise the PropertyChanged event. 
		/// </summary>
		public string Secret
		{
			get
			{
				if (String.IsNullOrEmpty(_secret))
				{
					String secret;
					if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("dropboxSecret",
					                                                            out secret))
					{
						_secret = secret;
					}
				}

				return _secret;
			}

			set
			{
				if (_secret == value)
				{
					return;
				}

				_secret = value;
				IsolatedStorageSettings.ApplicationSettings["dropboxSecret"] = _secret;
			}
		}

		/// <summary>
		/// Gets the Token property.
		/// Changes to that property's value raise the PropertyChanged event. 
		/// </summary>
		public string Token
		{
			get
			{
				if (String.IsNullOrEmpty(_token))
				{
					String token;
					if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("dropboxToken",
					                                                            out token))
					{
						_token = token;
					}
				}

				return _token;
			}

			set
			{
				if (_token == value)
				{
					return;
				}

				_token = value;
				IsolatedStorageSettings.ApplicationSettings["dropboxToken"] = _token;
			}
		}

		private void ExecuteDropboxAction(Action dropboxAction)
		{
			if (WeHaveTokens)
			{
				if (_dropNetClient == null)
				{
					_dropNetClient = DropNetExtensions.CreateClient(Token, Secret);
				}

				if (dropboxAction != null)
				{
					dropboxAction();
				}
			}
			else
			{
				Messenger.Default.Register<CredentialsUpdatedMessage>(
					this, (message) =>
						{
							Messenger.Default.Unregister<CredentialsUpdatedMessage>(this);
							ExecuteDropboxAction(dropboxAction);
						});

				Messenger.Default.Send(new NeedCredentialsMessage("Not authenticated"));
			}
		}

		private Action<DropboxException> WrapExceptionHandler(Action<DropboxException> handler)
		{
			return (ex) =>
				{
					// Dropnet responds with BadGateway if the network isn't accessible
					switch (ex.Response.StatusCode)
					{
						case HttpStatusCode.BadGateway:
							Messenger.Default.Send(new NetworkUnavailableMessage());
							break;
						case HttpStatusCode.ServiceUnavailable:
							Messenger.Default.Send(new CannotAccessDropboxMessage("Too many requests"));
							break;
						case HttpStatusCode.InternalServerError:
							Messenger.Default.Send(new CannotAccessDropboxMessage("Dropbox Server Error"));
							break;
						case HttpStatusCode.Unauthorized:
							_dropNetClient = null;
							Token = string.Empty;
							Secret = string.Empty;
							Messenger.Default.Send(new NeedCredentialsMessage("Authentication failed"));
							break;
						case HttpStatusCode.BadRequest:
							Messenger.Default.Send(new CannotAccessDropboxMessage("Lacking mobile authentication permission"));
							break;
						default:
							Messenger.Default.Send(new CannotAccessDropboxMessage());
							break;
					}

					if (handler != null)
					{
						handler(ex);
					}
				};
		}

		public void GetMetaData(string path, Action<MetaData> success, Action<DropboxException> failure)
		{
			ExecuteDropboxAction(
				() => _dropNetClient.GetMetaDataAsync(path, success, WrapExceptionHandler(failure)));
		}

		public void Upload(string path, string filename, byte[] bytes, Action<RestResponse> success,
		                   Action<DropboxException> failure)
		{
			ExecuteDropboxAction(
				() => _dropNetClient.UploadFileAsync(path, filename, bytes, success, WrapExceptionHandler(failure)));
		}

		public void GetFile(string path, Action<RestResponse> success, Action<DropboxException> failure)
		{
			ExecuteDropboxAction(
				() => _dropNetClient.GetFileAsync(path, success, WrapExceptionHandler(failure)));
		}

		public void GetToken()
		{
			if (_dropNetClient == null)
			{
				_dropNetClient = DropNetExtensions.CreateClient();
			}

			_dropNetClient.GetTokenAsync(
				success =>
				{
					string tokenUrl = _dropNetClient.BuildAuthorizeUrl("http://todotxt.traceur-llc.com/dblogin.html");

					Messenger.Default.Send(new RetrievedDropboxTokenMessage(new Uri(tokenUrl)));
				},
			    failure => Messenger.Default.Send(new RetrievedDropboxTokenMessage(failure.Message)));
		}

		public void GetAccessToken()
		{
			_dropNetClient.GetAccessTokenAsync(response =>
				{
					Token = response.Token;
					Secret = response.Secret;
					Messenger.Default.Send(new CredentialsUpdatedMessage());<commit_msg>Fixed the url for dropbox login callback<commit_after>﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using DropNet;
using DropNet.Exceptions;
using DropNet.Models;
using GalaSoft.MvvmLight.Messaging;
using RestSharp;
using TodotxtTouch.WindowsPhone.Messages;

namespace TodotxtTouch.WindowsPhone.Service
{
	public class DropboxService
	{
		private DropNetClient _dropNetClient;

		private String _secret = String.Empty;
		private String _token = String.Empty;

		public bool WeHaveTokens
		{
			get { return !String.IsNullOrEmpty(Token) && !String.IsNullOrEmpty(Secret); }
		}

		/// <summary>
		/// Gets the Secret property.
		/// Changes to that property's value raise the PropertyChanged event. 
		/// </summary>
		public string Secret
		{
			get
			{
				if (String.IsNullOrEmpty(_secret))
				{
					String secret;
					if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("dropboxSecret",
					                                                            out secret))
					{
						_secret = secret;
					}
				}

				return _secret;
			}

			set
			{
				if (_secret == value)
				{
					return;
				}

				_secret = value;
				IsolatedStorageSettings.ApplicationSettings["dropboxSecret"] = _secret;
			}
		}

		/// <summary>
		/// Gets the Token property.
		/// Changes to that property's value raise the PropertyChanged event. 
		/// </summary>
		public string Token
		{
			get
			{
				if (String.IsNullOrEmpty(_token))
				{
					String token;
					if (IsolatedStorageSettings.ApplicationSettings.TryGetValue("dropboxToken",
					                                                            out token))
					{
						_token = token;
					}
				}

				return _token;
			}

			set
			{
				if (_token == value)
				{
					return;
				}

				_token = value;
				IsolatedStorageSettings.ApplicationSettings["dropboxToken"] = _token;
			}
		}

		private void ExecuteDropboxAction(Action dropboxAction)
		{
			if (WeHaveTokens)
			{
				if (_dropNetClient == null)
				{
					_dropNetClient = DropNetExtensions.CreateClient(Token, Secret);
				}

				if (dropboxAction != null)
				{
					dropboxAction();
				}
			}
			else
			{
				Messenger.Default.Register<CredentialsUpdatedMessage>(
					this, (message) =>
						{
							Messenger.Default.Unregister<CredentialsUpdatedMessage>(this);
							ExecuteDropboxAction(dropboxAction);
						});

				Messenger.Default.Send(new NeedCredentialsMessage("Not authenticated"));
			}
		}

		private Action<DropboxException> WrapExceptionHandler(Action<DropboxException> handler)
		{
			return (ex) =>
				{
					// Dropnet responds with BadGateway if the network isn't accessible
					switch (ex.Response.StatusCode)
					{
						case HttpStatusCode.BadGateway:
							Messenger.Default.Send(new NetworkUnavailableMessage());
							break;
						case HttpStatusCode.ServiceUnavailable:
							Messenger.Default.Send(new CannotAccessDropboxMessage("Too many requests"));
							break;
						case HttpStatusCode.InternalServerError:
							Messenger.Default.Send(new CannotAccessDropboxMessage("Dropbox Server Error"));
							break;
						case HttpStatusCode.Unauthorized:
							_dropNetClient = null;
							Token = string.Empty;
							Secret = string.Empty;
							Messenger.Default.Send(new NeedCredentialsMessage("Authentication failed"));
							break;
						case HttpStatusCode.BadRequest:
							Messenger.Default.Send(new CannotAccessDropboxMessage("Lacking mobile authentication permission"));
							break;
						default:
							Messenger.Default.Send(new CannotAccessDropboxMessage());
							break;
					}

					if (handler != null)
					{
						handler(ex);
					}
				};
		}

		public void GetMetaData(string path, Action<MetaData> success, Action<DropboxException> failure)
		{
			ExecuteDropboxAction(
				() => _dropNetClient.GetMetaDataAsync(path, success, WrapExceptionHandler(failure)));
		}

		public void Upload(string path, string filename, byte[] bytes, Action<RestResponse> success,
		                   Action<DropboxException> failure)
		{
			ExecuteDropboxAction(
				() => _dropNetClient.UploadFileAsync(path, filename, bytes, success, WrapExceptionHandler(failure)));
		}

		public void GetFile(string path, Action<RestResponse> success, Action<DropboxException> failure)
		{
			ExecuteDropboxAction(
				() => _dropNetClient.GetFileAsync(path, success, WrapExceptionHandler(failure)));
		}

		public void GetToken()
		{
			if (_dropNetClient == null)
			{
				_dropNetClient = DropNetExtensions.CreateClient();
			}

			_dropNetClient.GetTokenAsync(
				success =>
				{
					string tokenUrl = _dropNetClient.BuildAuthorizeUrl("http://todotxt.traceur-llc.com/dblogin.htm");

					Messenger.Default.Send(new RetrievedDropboxTokenMessage(new Uri(tokenUrl)));
				},
			    failure => Messenger.Default.Send(new RetrievedDropboxTokenMessage(failure.Message)));
		}

		public void GetAccessToken()
		{
			_dropNetClient.GetAccessTokenAsync(response =>
				{
					Token = response.Token;
					Secret = response.Secret;
					Messenger.Default.Send(new CredentialsUpdatedMessage());
{'fraction_non_alphanumeric': 0.09946923530568115, 'fraction_numerical': 0.0, 'mean_word_length': 2.9059500959692897, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2974734', 'n_tokens_mistral': 3881, 'n_tokens_neox': 2855, 'n_words': 571}