private void OnGetWallpapers(TLVector <TLWallPaperBase> results, int skip, int take)
        {
            var currentItem = StateService.CurrentBackground;

            foreach (var result in results.Skip(skip).Take(take))
            {
                var wallpaper = result as TLWallPaper;
                if (wallpaper == null)
                {
                    continue;
                }

                var size = BackgroundImageConverter.GetPhotoSize(wallpaper.Sizes, 480.0);
                if (size != null)
                {
                    var location = size.Location as TLFileLocation;
                    if (location != null)
                    {
                        var fileName = String.Format("{0}_{1}_{2}.jpg",
                                                     location.VolumeId,
                                                     location.LocalId,
                                                     location.Secret);

                        BackgroundItem item;
                        var            isSelected = currentItem != null && "telegram" + wallpaper.Id == currentItem.Name;
                        if (isSelected)
                        {
                            item            = currentItem;
                            item.IsSelected = true;
                        }
                        else
                        {
                            item = new BackgroundItem
                            {
                                Name        = "telegram" + wallpaper.Id,
                                Wallpaper   = wallpaper,
                                IsoFileName = fileName
                            };
                        }
                        Static.Add(item);
                    }
                }
            }
        }
        private void ReplaceBackgroundItem(BackgroundItem item)
        {
            var currentItem = StateService.CurrentBackground;

            if (currentItem != null)
            {
                currentItem.IsSelected = false;
                currentItem.NotifyOfPropertyChange("IsSelected");
            }
            else
            {
                _emptyBackground.IsSelected = false;
                _emptyBackground.NotifyOfPropertyChange("IsSelected");
            }

            item.IsSelected = true;
            item.NotifyOfPropertyChange("IsSelected");
            StateService.CurrentBackground = item;
        }
        /// <summary>
        /// Add a finalizer to check for memory leaks
        /// </summary>
        //~ChooseBackgroundViewModel()
        //{
        //    TLUtils.WritePerformance("++ChooseBackgroundViewModel dstr");
        //}
#endif

        public void Choose(BackgroundItem item)
        {
            if (item == null)
            {
                return;
            }

            if (item == _libraryBackground)
            {
                var task = new PhotoChooserTask
                {
                    PixelHeight = 800,
                    PixelWidth  = 480,
                    ShowCamera  = true
                };
                task.Completed += (sender, result) =>
                {
                    if (result.TaskResult != TaskResult.OK)
                    {
                        return;
                    }

                    byte[] bytes;
                    var    sourceStream = result.ChosenPhoto;
                    var    fileName     = Path.GetFileName(result.OriginalFileName);

                    if (string.IsNullOrEmpty(fileName))
                    {
                        return;
                    }

                    using (var memoryStream = new MemoryStream())
                    {
                        sourceStream.CopyTo(memoryStream);
                        bytes = memoryStream.ToArray();
                    }

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var file = store.OpenFile(fileName, FileMode.CreateNew))
                        {
                            file.Write(bytes, 0, bytes.Length);
                        }
                    }

                    _libraryBackground.IsoFileName = fileName;

                    ReplaceBackgroundItem(_libraryBackground);
                };
                task.Show();

                return;
            }

            if (item == AnimatedBackground1)
            {
                MessageBox.Show(AppResources.PleaseNoteThatAnimatedBackgroundConsumesMoreBatteryResources);
            }

            ReplaceBackgroundItem(item);
        }
        public ChooseBackgroundViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            Static = new ObservableCollection <BackgroundItem>();

            var currentBackground = StateService.CurrentBackground;

            if (currentBackground != null &&
                currentBackground.Name == Constants.EmptyBackgroundString)
            {
                _emptyBackground            = currentBackground;
                _emptyBackground.IsSelected = true;
            }
            else
            {
                _emptyBackground = new BackgroundItem
                {
                    Name         = Constants.EmptyBackgroundString,
                    SourceString = string.Empty,
                    IsSelected   = currentBackground == null
                };
            }

            if (currentBackground != null &&
                currentBackground.Name == Constants.LibraryBackgroundString)
            {
                _libraryBackground            = currentBackground;
                _libraryBackground.IsSelected = true;
            }
            else
            {
                _libraryBackground = new BackgroundItem
                {
                    Name         = Constants.LibraryBackgroundString,
                    SourceString = "/Images/Backgrounds/gallery_WXGA.png"
                };
            }


            _userCachedWallpapers = _cachedWallpapers != null;
            if (!_userCachedWallpapers)
            {
                IsWorking = true;
                MTProtoService.GetWallpapersAsync(
                    results =>
                {
                    IsWorking         = false;
                    _cachedWallpapers = results;
                    Telegram.Api.Helpers.Execute.BeginOnUIThread(() =>
                    {
                        if (_forwardInAnimationComplete)
                        {
                            OnGetWallpapers(results, 0, results.Count);
                        }
                        else
                        {
                            _wallpapers = results;
                            //OnGetWallpapers(results, 0, 4);
                        }
                    });
                },
                    error =>
                {
                    IsWorking = false;
                    Telegram.Api.Helpers.Execute.ShowDebugMessage("account.getWallpapers error " + error);
                });
            }
            else
            {
                OnGetWallpapers(_cachedWallpapers, 0, 4);
            }


            eventAggregator.Subscribe(this);
        }