private static async Task <string> GetJsonDataAsync(string url, MainThreadMarshaller uiMarshaller)
        {
            try
            {
                Logger.Instance.LogMessageToFile("Searching Reddit for a wallpaper.", LogLevel.Information);

                using (var wc = HelperMethods.CreateWebClient())
                {
                    return(await wc.DownloadStringTaskAsync(url).ConfigureAwait(false));
                }
            }
            catch (WebException ex)
            {
                uiMarshaller.LogFailure(ex.Message, $"Reddit server error: {ex.Message}",
                                        LogLevel.Error);

                throw;
            }
            catch (Exception ex)
            {
                uiMarshaller.LogFailure("Error downloading search results.",
                                        $"Error downloading search results: {ex.Message}", LogLevel.Error);

                throw;
            }
        }
        // TODO refactor
        private static string GetRedditSearchUrl(Random random, MainThreadMarshaller uiMarshaller)
        {
            var query = WebUtility.UrlEncode(Settings.Default.searchQuery) +
                        "+self%3Ano+((url%3A.png+OR+url%3A.jpg+OR+url%3A.jpeg)+OR+(url%3Aimgur.png+OR+url%3Aimgur.jpg+OR+url%3Aimgur.jpeg)+OR+(url%3Adeviantart))";

            var subreddits = new StringBuilder(Settings.Default.subredditsUsed).Replace(" ", "")
                             .Replace("www.reddit.com/", "")
                             .Replace("reddit.com/", "")
                             .Replace("http://", "")
                             .Replace("/r/", "")
                             .ToString();
            var subs = subreddits.Split('+');
            var sub  = subs[random.Next(0, subs.Length)];

            uiMarshaller.UpdateStatus("Searching /r/" + sub + " for a wallpaper...");
            Logger.Instance.LogMessageToFile("Selected sub to search: " + sub, LogLevel.Information);

            var formURL = new StringBuilder("http://www.reddit.com/");

            if (!sub.Any())
            {
                formURL.Append("r/all");
            }
            else if (sub.Contains("/m/"))
            {
                formURL.Append(subreddits)
                .Replace("http://", "")
                .Replace("https://", "")
                .Replace("user/", "u/");
            }
            else
            {
                formURL.Append($"r/").Append(sub);
            }

            switch ((WallpaperGrabType)Settings.Default.wallpaperGrabType)
            {
            case WallpaperGrabType.Random:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append(SortValues[random.Next(0, 4)])
                .Append(TopValues[random.Next(0, 5)])
                .Append("&restrict_sr=on");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.Newest:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=new&restrict_sr=on");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.HotToday:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=hot&restrict_sr=on&t=day");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TopLastHour:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=top&restrict_sr=on&t=hour");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TopToday:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=top&restrict_sr=on&t=day");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TopWeek:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=top&restrict_sr=on&t=week");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TopMonth:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=top&restrict_sr=on&t=month");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TopYear:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=top&restrict_sr=on&t=year");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TopAllTime:
                formURL.Append("/search.json?q=")
                .Append(query)
                .Append("&sort=top&restrict_sr=on&t=all");
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;

            case WallpaperGrabType.TrulyRandom:
                formURL.Append("/random.json?p=").Append(Guid.NewGuid());
                Logger.Instance.LogMessageToFile("Full URL Search String: " + formURL, LogLevel.Information);
                break;
            }

            return(formURL.ToString());
        }
 public WallpaperChanger(MainThreadMarshaller uiMarshaller, Database database)
 {
     _uiMarshaller = uiMarshaller;
     _database     = database;
 }