public static List <CollectionListItem> GetSubCategoryList(int id, bool forceRefresh = false)
        {
            if (SubCategories.ContainsKey(id) && !forceRefresh)
            {
                return(SubCategories[id]);
            }

            WallpaperResponse response = Get(buildUrl(LookupMethods.sub_category_list, InfoLevels.Basic, 1, 0, 0, SizeOperators.Equal, id));

            if (response == null)
            {
                LastResult = "An unknown error has occured!";
            }
            else if (response.SubCategories == null)
            {
                LastResult = response.ErrorMessage;
            }
            else
            {
                if (SubCategories.ContainsKey(id))
                {
                    SubCategories[id] = new List <CollectionListItem>(response.SubCategories);
                }
                else
                {
                    SubCategories.Add(id, new List <CollectionListItem>(response.SubCategories));
                }

                return(SubCategories[id]);
            }
            return(new List <CollectionListItem>());
        }
        private static RequestErrors Get(String requestUrl, out List <Wallpaper> returnedwallpapers, out bool isLastPage)
        {
            TimeSpan timeout = new TimeSpan(0, 0, 10);
            Stream   stream;

            Task <Stream> task = new Task <Stream>(() => { return(new System.Net.WebClient().OpenRead(requestUrl)); });

            task.Start();

            bool     timedout = false;
            DateTime start    = DateTime.Now;

            while (!task.IsCompleted)
            {
                Task.Delay(100);

                if (DateTime.Now - start > timeout)
                {
                    timedout = true;
                    break;
                }
            }

            if (!timedout)
            {
                stream = task.Result;
                // task completed within timeout
            }
            else
            {
                isLastPage         = true;
                returnedwallpapers = null;
                return(RequestErrors.request_timeout);
                // timeout logic
            }

            //Stream stream = new System.Net.WebClient().OpenRead(requestUrl);

            var serializer = new DataContractJsonSerializer(typeof(WallpaperResponse));
            WallpaperResponse deserializedResult = (WallpaperResponse)serializer.ReadObject(stream);


            if (deserializedResult.Success == true)
            {
                LastResult = "OK";

                returnedwallpapers = new List <Wallpaper>(deserializedResult.Wallpapers);
                isLastPage         = deserializedResult.IsLast;
                return(RequestErrors.NoError);
            }

            LastResult = deserializedResult.ErrorMessage;

            isLastPage         = true;
            returnedwallpapers = null;
            return(ErrorDescriptions.ToEnum(deserializedResult.ErrorMessage));
        }
        private static WallpaperResponse Get(String requestUrl)
        {
            Stream stream = new System.Net.WebClient().OpenRead(requestUrl);

            var serializer = new DataContractJsonSerializer(typeof(WallpaperResponse));
            WallpaperResponse deserializedResult = (WallpaperResponse)serializer.ReadObject(stream);

            if (deserializedResult.Success == true)
            {
                LastResult = "OK";
            }
            else
            {
                LastResult = deserializedResult.ErrorMessage;
            }

            return(deserializedResult);
        }
        //public static List<Wallpaper> Newest(InfoLevels level = InfoLevels.Basic, SizeOperators op = SizeOperators.Equal, Size? size = null, int maxResults = 30)
        //{
        //    int width = 0;
        //    int height = 0;

        //    if (size != null && size.HasValue)
        //    {
        //        width = size.Value.Width;
        //        height = size.Value.Height;
        //    }

        //    List<string> urls = BuildUrls(LookupMethods.newest, level, maxResults, width, height, op, 0, SortMethods.Newest);

        //    return Get(urls);
        //}

        //public static List<Wallpaper> Newest(InfoLevels level = InfoLevels.Basic, SizeOperators op = SizeOperators.Equal, int width = 0, int height = 0, int maxResults = 30)
        //{
        //    List<string> urls = BuildUrls(LookupMethods.newest, level, maxResults, width, height, op, 0, SortMethods.Newest);

        //    return Get(urls);
        //}

        #region Counts

        public static ApiCounts GetCounts()
        {
            WallpaperResponse response = Get(buildUrl(LookupMethods.query_count));

            if (response == null)
            {
                LastResult = "An unknown error has occured!";
            }
            else if (response.Counts == null)
            {
                LastResult = response.ErrorMessage;
            }
            else
            {
                return(response.Counts);
            }

            return(null);
        }
        public static int GetUserCount(int id, int width, int height, SizeOperators op)
        {
            WallpaperResponse response = Get(buildUrl(LookupMethods.user_count, InfoLevels.Basic, 1, width, height, op, id));

            if (response == null)
            {
                LastResult = "An unknown error has occured!";
            }
            else if (response.Counts == null)
            {
                LastResult = response.ErrorMessage;
            }
            else
            {
                return(response.Count);
            }

            return(-1);
        }
        public static List <CollectionListItem> GetCollectionList(bool forceRefresh = false)
        {
            if (Collections.Count > 0 && !forceRefresh)
            {
                return(Collections);
            }

            WallpaperResponse response = Get(buildUrl(LookupMethods.collection_list));

            if (response == null)
            {
                LastResult = "An unknown error has occured!";
            }
            else if (response.Collections == null)
            {
                LastResult = response.ErrorMessage;
            }
            else
            {
                return(Collections = new List <CollectionListItem>(response.Collections));
            }

            return(new List <CollectionListItem>());
        }