private void LoadDataCompleted(Exception err, PageModel response, SiteModel site)
 {
     if (err != null)
     {
         System.Diagnostics.Debug.WriteLine(err.ToString());
     }
     else
     {
         this.CurrentPage = response;
     }
 }
 public async Task LoadData(string siteId, string pageToken)
 {
     CurrentSite = ServiceManager.Sites.FirstOrDefault(x => x.Id == siteId);
     if (CurrentSite != null)
     {
         switch (CurrentSite.Title)
         {
             case "Alter Information":
                 await _serviceManager.GetDataAlterInformation(pageToken.ToInt(1), (response, err) => LoadDataCompleted(err, response));
                 break;
             default:
                 await _serviceManager.GetDataBlogZaSeriozniHora(pageToken, (response, err) => LoadDataCompleted(err, response));
                 break;
         }
     }
 }
        public async void GetDataBlogZaSeriozniHora(string pageToken, Action<PageModel, Exception> callback)
        {
            string uri = "http://blogzaserioznihora.blogspot.com/";

            Exception err = null;
            var result = new PageModel();
            try
            {
                BloggerService _service = new BloggerService(new BaseClientService.Initializer()
                {
                    ApiKey = "AIzaSyCkyf5p4x_2tW-Bwmqt7Bbj-HhFIC_kXvw",
                });

                SiteModel site = Sites.FirstOrDefault(x => x.Title == "Блог за сериозни хора");
                if (site == null)
                {
                    var blog = await _service.Blogs.GetByUrl(uri).ExecuteAsync();
                    site = new SiteModel()
                    {
                        Title = "Блог за сериозни хора",
                        Id = blog.Id
                    };
                    
                    //group.ImagePath = blog.
                    Sites.Add(site);
                }

                PageModel page = site.Pages.FirstOrDefault(x => x.CurrentPageToken == pageToken);
                if (page != null)
                {
                    result = page;
                }
                else
                {
                    var getPosts = _service.Posts.List(site.Id);
                    getPosts.MaxResults = 10;
                    if (!string.IsNullOrEmpty(pageToken))
                    {
                        getPosts.PageToken = pageToken;
                        result.CurrentPageToken = pageToken;
                    }

                    var response = await getPosts.ExecuteAsync();
                    result.NextPageToken = response.NextPageToken;
                    result.PrevPageToken = response.PrevPageToken;
                    result.PageIndex = site.Pages.Count;

                    foreach (var item in response.Items)
                    {
                        var itemModel = new ItemModel()
                        {
                            Id = item.Id,
                            Title = item.Title,
                            Content = item.Content,
                            OriginalItem = item,
                            Parent = page
                        };

                        var document = new HtmlDocument();
                        document.LoadHtml(item.Content);
                        var img = document.DocumentNode.Descendants().FirstOrDefault(doc => doc.Name == "img");
                        if (img != null)
                        {
                            itemModel.ImagePath = img.Attributes["src"].Value;
                        }

                        result.Items.Add(itemModel);
                    }
                    site.Pages.Add(result);
                }
            }
            catch (Exception ex)
            {
                err = ex;
            }

            callback(result, err);
        }
        public async void GetDataAlterInformation(string pageToken, Action<PageModel, Exception> callback)
        {
            HttpClient client = new HttpClient();
            string uri = "http://alterinformation.wordpress.com/feed/?paged=" + pageToken;

            Exception err = null;
            var result = new PageModel();
            
            try
            {
                var blog = await client.GetStringAsync("http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&q=" + uri);
                var responseRootObject = JsonConvert.DeserializeObject<RootObject>(blog);

                SiteModel site = Sites.FirstOrDefault(x => x.Title == "Alter Information");
                if (site == null)
                {
                    site = new SiteModel()
                    {
                        Title = "Alter Information",
                        //Id = blog.Id
                    };

                    //group.ImagePath = blog.
                    Sites.Add(site);
                }

                PageModel page = site.Pages.FirstOrDefault(x => x.CurrentPageToken == pageToken.ToString());
                if (page != null)
                {
                    result = page;
                }
                else
                {
                    result.PageIndex = site.Pages.Count;
                    result.CurrentPageToken = Convert.ToInt16(result.PageIndex + 1).ToString();
                    result.NextPageToken = Convert.ToInt16(result.CurrentPageToken + 1).ToString();
                    foreach (var item in responseRootObject.responseData.feed.entries)
                    {
                        var itemModel = new ItemModel()
                        {
                            Title = item.title,
                            Content = item.content,
                            OriginalItem = item,
                            Parent = page
                        };

                        var document = new HtmlDocument();
                        document.LoadHtml(item.content);
                        var img = document.DocumentNode.Descendants().FirstOrDefault(doc => doc.Name == "img");
                        if (img != null)
                        {
                            itemModel.ImagePath = img.Attributes["src"].Value;
                        }

                        result.Items.Add(itemModel);
                    }
                    site.Pages.Add(result);
                }
            }
            catch (Exception ex)
            {
                err = ex;
            }

            callback(result, err);
        }
        public async Task Search(string searchQuery, Action<PageModel, Exception> callback)
        {
            string uri = "http://blogzaserioznihora.blogspot.com/";

            Exception err = null;
            var result = new PageModel();
            try
            {
                BloggerService _service = new BloggerService(new BaseClientService.Initializer()
                {
                    ApiKey = "AIzaSyCkyf5p4x_2tW-Bwmqt7Bbj-HhFIC_kXvw",
                });

                SiteModel site = Sites.FirstOrDefault(x => x.Title == "Блог за сериозни хора");
                if (site == null)
                {
                    var blog = await _service.Blogs.GetByUrl(uri).ExecuteAsync();
                    site = new SiteModel()
                    {
                        Title = "Блог за сериозни хора",
                        Id = blog.Id
                    };

                    //group.ImagePath = blog.
                    Sites.Add(site);
                }
                var response = await _service.Posts.Search(site.Id, searchQuery).ExecuteAsync();
                result.NextPageToken = response.NextPageToken;
                result.PrevPageToken = response.PrevPageToken;
                result.PageIndex = site.Pages.Count;

                foreach (var item in response.Items)
                {
                    var itemModel = new ItemModel()
                    {
                        Id = item.Id,
                        Title = item.Title,
                        OriginalItem = item,
                        //Parent = result,
                        OriginalUrl = item.Url,
                        CommentUrl =
                            "http://www.blogger.com/comment.g?blogID=" + site.Id + "&postID=" + item.Id +
                            "&isPopup=true"
                    };

                    var document = new HtmlDocument();
                    document.LoadHtml(WebContentHelper.WrapHtml(item.Content, 0, 0));

                    itemModel.Content = RefactorContent(document);

                    var img = document.DocumentNode.Descendants().FirstOrDefault(doc => doc.Name == "img");
                    if (img != null)
                    {
                        itemModel.ImagePath = img.Attributes["src"].Value;
                    }

                    AddItemToTiles(itemModel);
                    result.Items.Add(itemModel);
                }
                site.Pages.Add(result);
            }
            catch (Exception ex)
            {
                err = ex;
            }

            callback(result, err);
        }