/*
   * block a blog - result includes the list of posts that were deleted by the block so they
   * can be restored if the user undoes the block
   */
  public static BlockedBlogResult blockBlogFromReader(
      final long blogId, final ActionListener actionListener) {
    final BlockedBlogResult blockResult = new BlockedBlogResult();
    blockResult.blogId = blogId;
    blockResult.deletedPosts = ReaderPostTable.getPostsInBlog(blogId, 0, false);
    blockResult.wasFollowing = ReaderBlogTable.isFollowedBlog(blogId);

    ReaderPostTable.deletePostsInBlog(blogId);
    ReaderBlogTable.setIsFollowedBlogId(blogId, false);

    com.wordpress.rest.RestRequest.Listener listener =
        new RestRequest.Listener() {
          @Override
          public void onResponse(JSONObject jsonObject) {
            if (actionListener != null) {
              actionListener.onActionResult(true);
            }
          }
        };
    RestRequest.ErrorListener errorListener =
        new RestRequest.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError volleyError) {
            AppLog.e(T.READER, volleyError);
            ReaderPostTable.addOrUpdatePosts(null, blockResult.deletedPosts);
            if (blockResult.wasFollowing) {
              ReaderBlogTable.setIsFollowedBlogId(blogId, true);
            }
            if (actionListener != null) {
              actionListener.onActionResult(false);
            }
          }
        };

    AppLog.i(T.READER, "blocking blog " + blogId);
    String path = "me/block/sites/" + Long.toString(blogId) + "/new";
    WordPress.getRestClientUtilsV1_1().post(path, listener, errorListener);

    return blockResult;
  }
  @Override
  public View getView(final int position, View convertView, final ViewGroup parent) {
    final BlogViewHolder holder;
    if (convertView == null || !(convertView.getTag() instanceof BlogViewHolder)) {
      convertView = mInflater.inflate(R.layout.reader_listitem_blog, parent, false);
      holder = new BlogViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (BlogViewHolder) convertView.getTag();
    }

    final long blogId;
    final String blogUrl;
    final boolean isFollowing;
    switch (getBlogType()) {
      case RECOMMENDED:
        final ReaderRecommendedBlog blog = (ReaderRecommendedBlog) getItem(position);
        blogId = blog.blogId;
        blogUrl = blog.getBlogUrl();
        isFollowing = ReaderBlogTable.isFollowedBlog(blogId, blogUrl);
        holder.txtTitle.setText(blog.getTitle());
        holder.txtDescription.setText(blog.getReason());
        holder.txtUrl.setText(UrlUtils.getDomainFromUrl(blogUrl));
        holder.imgBlog.setImageUrl(blog.getImageUrl(), WPNetworkImageView.ImageType.AVATAR);
        break;

      case FOLLOWED:
        final ReaderBlog blogInfo = (ReaderBlog) getItem(position);
        blogId = blogInfo.blogId;
        blogUrl = blogInfo.getUrl();
        isFollowing = blogInfo.isFollowing;
        String domain = UrlUtils.getDomainFromUrl(blogUrl);
        if (blogInfo.hasName()) {
          holder.txtTitle.setText(blogInfo.getName());
        } else {
          holder.txtTitle.setText(domain);
        }
        holder.txtUrl.setText(domain);
        break;

      default:
        blogId = 0;
        blogUrl = null;
        isFollowing = false;
        break;
    }

    // show the correct following status
    ReaderUtils.showFollowStatus(holder.txtFollow, isFollowing);
    holder.txtFollow.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            AniUtils.zoomAction(holder.txtFollow);
            changeFollowStatus(holder.txtFollow, position, !isFollowing);
          }
        });

    // show blog preview when view is clicked
    convertView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            // make sure we have either the blog id or url
            if (blogId != 0 || !TextUtils.isEmpty(blogUrl)) {
              ReaderActivityLauncher.showReaderBlogPreview(getContext(), blogId, blogUrl);
            }
          }
        });

    return convertView;
  }