/** delete multiple comments */
  private static void deleteComments(
      final int accountId,
      final CommentList comments,
      final OnCommentsModeratedListener actionListener) {

    final Blog blog = WordPress.getBlog(accountId);

    if (blog == null || comments == null || comments.size() == 0) {
      if (actionListener != null) actionListener.onCommentsModerated(new CommentList());
      return;
    }

    final CommentList deletedComments = new CommentList();
    final int localBlogId = blog.getLocalTableBlogId();
    final int remoteBlogId = blog.getRemoteBlogId();

    final Handler handler = new Handler();
    new Thread() {
      @Override
      public void run() {
        XMLRPCClientInterface client =
            XMLRPCFactory.instantiate(blog.getUri(), blog.getHttpuser(), blog.getHttppassword());

        for (Comment comment : comments) {
          Object[] params = {
            remoteBlogId, blog.getUsername(), blog.getPassword(), comment.commentID
          };

          Object result;
          try {
            result = client.call("wp.deleteComment", params);
            boolean success = (result != null && Boolean.parseBoolean(result.toString()));
            if (success) deletedComments.add(comment);
          } catch (XMLRPCException e) {
            AppLog.e(T.COMMENTS, "Error while deleting comment", e);
          } catch (IOException e) {
            AppLog.e(T.COMMENTS, "Error while deleting comment", e);
          } catch (XmlPullParserException e) {
            AppLog.e(T.COMMENTS, "Error while deleting comment", e);
          }
        }

        // remove successfully deleted comments from SQLite
        CommentTable.deleteComments(localBlogId, deletedComments);

        if (actionListener != null) {
          handler.post(
              new Runnable() {
                @Override
                public void run() {
                  actionListener.onCommentsModerated(deletedComments);
                }
              });
        }
      }
    }.start();
  }
  /*
   * add a comment for the passed post
   */
  public static void addComment(
      final int accountId,
      final String postID,
      final String commentText,
      final CommentActionListener actionListener) {
    final Blog blog = WordPress.getBlog(accountId);
    if (blog == null || TextUtils.isEmpty(commentText)) {
      if (actionListener != null) actionListener.onActionResult(false);
      return;
    }

    final Handler handler = new Handler();

    new Thread() {
      @Override
      public void run() {
        XMLRPCClientInterface client =
            XMLRPCFactory.instantiate(blog.getUri(), blog.getHttpuser(), blog.getHttppassword());

        Map<String, Object> commentHash = new HashMap<String, Object>();
        commentHash.put("content", commentText);
        commentHash.put("author", "");
        commentHash.put("author_url", "");
        commentHash.put("author_email", "");

        Object[] params = {
          blog.getRemoteBlogId(), blog.getUsername(), blog.getPassword(), postID, commentHash
        };

        int newCommentID;
        try {
          newCommentID = (Integer) client.call("wp.newComment", params);
        } catch (XMLRPCException e) {
          AppLog.e(T.COMMENTS, "Error while sending new comment", e);
          newCommentID = -1;
        } catch (IOException e) {
          AppLog.e(T.COMMENTS, "Error while sending new comment", e);
          newCommentID = -1;
        } catch (XmlPullParserException e) {
          AppLog.e(T.COMMENTS, "Error while sending new comment", e);
          newCommentID = -1;
        }

        final boolean succeeded = (newCommentID >= 0);

        if (actionListener != null) {
          handler.post(
              new Runnable() {
                @Override
                public void run() {
                  actionListener.onActionResult(succeeded);
                }
              });
        }
      }
    }.start();
  }
  /** delete (trash) a single comment */
  private static void deleteComment(
      final int accountId, final Comment comment, final CommentActionListener actionListener) {
    final Blog blog = WordPress.getBlog(accountId);
    if (blog == null || comment == null) {
      if (actionListener != null) actionListener.onActionResult(false);
      return;
    }

    final Handler handler = new Handler();

    new Thread() {
      @Override
      public void run() {
        XMLRPCClientInterface client =
            XMLRPCFactory.instantiate(blog.getUri(), blog.getHttpuser(), blog.getHttppassword());

        Object[] params = {
          blog.getRemoteBlogId(), blog.getUsername(), blog.getPassword(), comment.commentID
        };

        Object result;
        try {
          result = client.call("wp.deleteComment", params);
        } catch (final XMLRPCException e) {
          AppLog.e(T.COMMENTS, "Error while deleting comment", e);
          result = null;
        } catch (IOException e) {
          AppLog.e(T.COMMENTS, "Error while deleting comment", e);
          result = null;
        } catch (XmlPullParserException e) {
          AppLog.e(T.COMMENTS, "Error while deleting comment", e);
          result = null;
        }

        final boolean success = (result != null && Boolean.parseBoolean(result.toString()));
        if (success) CommentTable.deleteComment(accountId, comment.commentID);

        if (actionListener != null) {
          handler.post(
              new Runnable() {
                @Override
                public void run() {
                  actionListener.onActionResult(success);
                }
              });
        }
      }
    }.start();
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blog_preferences);

    Integer id = getIntent().getIntExtra("id", -1);
    blog = WordPress.getBlog(id);

    if (blog == null) {
      Toast.makeText(this, getString(R.string.blog_not_found), Toast.LENGTH_SHORT).show();
      finish();
      return;
    }

    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(StringUtils.unescapeHTML(blog.getBlogName()));
    actionBar.setDisplayHomeAsUpEnabled(true);

    mUsernameET = (EditText) findViewById(R.id.username);
    mPasswordET = (EditText) findViewById(R.id.password);
    mHttpUsernameET = (EditText) findViewById(R.id.httpuser);
    mHttpPasswordET = (EditText) findViewById(R.id.httppassword);
    mScaledImageWidthET = (EditText) findViewById(R.id.scaledImageWidth);
    mFullSizeCB = (CheckBox) findViewById(R.id.fullSizeImage);
    mScaledCB = (CheckBox) findViewById(R.id.scaledImage);
    mLocationCB = (CheckBox) findViewById(R.id.location);
    mImageWidthSpinner = (Spinner) findViewById(R.id.maxImageWidth);

    if (blog.isDotcomFlag()) {
      // Hide credentials section
      RelativeLayout credentialsRL = (RelativeLayout) findViewById(R.id.sectionContent);
      credentialsRL.setVisibility(View.GONE);
    }

    loadSettingsForBlog();
  }
  /**
   * change the status of multiple comments TODO: investigate using system.multiCall to perform a
   * single call to moderate the list
   */
  static void moderateComments(
      final int accountId,
      final CommentList comments,
      final CommentStatus newStatus,
      final OnCommentsModeratedListener actionListener) {

    // deletion is handled separately
    if (newStatus != null && newStatus.equals(CommentStatus.TRASH)) {
      deleteComments(accountId, comments, actionListener);
      return;
    }

    final Blog blog = WordPress.getBlog(accountId);

    if (blog == null
        || comments == null
        || comments.size() == 0
        || newStatus == null
        || newStatus == CommentStatus.UNKNOWN) {
      if (actionListener != null) actionListener.onCommentsModerated(new CommentList());
      return;
    }

    final CommentList moderatedComments = new CommentList();
    final String newStatusStr = CommentStatus.toString(newStatus);
    final int localBlogId = blog.getLocalTableBlogId();
    final int remoteBlogId = blog.getRemoteBlogId();

    final Handler handler = new Handler();
    new Thread() {
      @Override
      public void run() {
        XMLRPCClientInterface client =
            XMLRPCFactory.instantiate(blog.getUri(), blog.getHttpuser(), blog.getHttppassword());
        for (Comment comment : comments) {
          Map<String, String> postHash = new HashMap<String, String>();
          postHash.put("status", newStatusStr);
          postHash.put("content", comment.getCommentText());
          postHash.put("author", comment.getAuthorName());
          postHash.put("author_url", comment.getAuthorUrl());
          postHash.put("author_email", comment.getAuthorEmail());

          Object[] params = {
            remoteBlogId,
            blog.getUsername(),
            blog.getPassword(),
            Long.toString(comment.commentID),
            postHash
          };

          Object result;
          try {
            result = client.call("wp.editComment", params);
            boolean success = (result != null && Boolean.parseBoolean(result.toString()));
            if (success) {
              comment.setStatus(newStatusStr);
              moderatedComments.add(comment);
            }
          } catch (XMLRPCException e) {
            AppLog.e(T.COMMENTS, "Error while editing comment", e);
          } catch (IOException e) {
            AppLog.e(T.COMMENTS, "Error while editing comment", e);
          } catch (XmlPullParserException e) {
            AppLog.e(T.COMMENTS, "Error while editing comment", e);
          }
        }

        // update status in SQLite of successfully moderated comments
        CommentTable.updateCommentsStatus(localBlogId, moderatedComments, newStatusStr);

        if (actionListener != null) {
          handler.post(
              new Runnable() {
                @Override
                public void run() {
                  actionListener.onCommentsModerated(moderatedComments);
                }
              });
        }
      }
    }.start();
  }
  /** change the status of a single comment */
  static void moderateComment(
      final int accountId,
      final Comment comment,
      final CommentStatus newStatus,
      final CommentActionListener actionListener) {

    // deletion is handled separately
    if (newStatus != null && newStatus.equals(CommentStatus.TRASH)) {
      deleteComment(accountId, comment, actionListener);
      return;
    }

    final Blog blog = WordPress.getBlog(accountId);

    if (blog == null
        || comment == null
        || newStatus == null
        || newStatus == CommentStatus.UNKNOWN) {
      if (actionListener != null) actionListener.onActionResult(false);
      return;
    }

    final Handler handler = new Handler();

    new Thread() {
      @Override
      public void run() {
        XMLRPCClientInterface client =
            XMLRPCFactory.instantiate(blog.getUri(), blog.getHttpuser(), blog.getHttppassword());

        Map<String, String> postHash = new HashMap<String, String>();
        postHash.put("status", CommentStatus.toString(newStatus));
        postHash.put("content", comment.getCommentText());
        postHash.put("author", comment.getAuthorName());
        postHash.put("author_url", comment.getAuthorUrl());
        postHash.put("author_email", comment.getAuthorEmail());

        Object[] params = {
          blog.getRemoteBlogId(),
          blog.getUsername(),
          blog.getPassword(),
          Long.toString(comment.commentID),
          postHash
        };

        Object result;
        try {
          result = client.call("wp.editComment", params);
        } catch (XMLRPCException e) {
          AppLog.e(T.COMMENTS, "Error while editing comment", e);
          result = null;
        } catch (IOException e) {
          AppLog.e(T.COMMENTS, "Error while editing comment", e);
          result = null;
        } catch (XmlPullParserException e) {
          AppLog.e(T.COMMENTS, "Error while editing comment", e);
          result = null;
        }

        final boolean success = (result != null && Boolean.parseBoolean(result.toString()));
        if (success)
          CommentTable.updateCommentStatus(
              blog.getLocalTableBlogId(), comment.commentID, CommentStatus.toString(newStatus));

        if (actionListener != null) {
          handler.post(
              new Runnable() {
                @Override
                public void run() {
                  actionListener.onActionResult(success);
                }
              });
        }
      }
    }.start();
  }
  /** reply to an individual comment */
  static void submitReplyToComment(
      final int accountId,
      final Comment comment,
      final String replyText,
      final CommentActionListener actionListener) {

    final Blog blog = WordPress.getBlog(accountId);
    if (blog == null || comment == null || TextUtils.isEmpty(replyText)) {
      if (actionListener != null) actionListener.onActionResult(false);
      return;
    }

    final Handler handler = new Handler();

    new Thread() {
      @Override
      public void run() {
        XMLRPCClientInterface client =
            XMLRPCFactory.instantiate(blog.getUri(), blog.getHttpuser(), blog.getHttppassword());

        Map<String, Object> replyHash = new HashMap<String, Object>();
        replyHash.put("comment_parent", Long.toString(comment.commentID));
        replyHash.put("content", replyText);
        replyHash.put("author", "");
        replyHash.put("author_url", "");
        replyHash.put("author_email", "");

        Object[] params = {
          blog.getRemoteBlogId(),
          blog.getUsername(),
          blog.getPassword(),
          Long.toString(comment.postID),
          replyHash
        };

        long newCommentID;
        try {
          Object newCommentIDObject = client.call("wp.newComment", params);
          if (newCommentIDObject instanceof Integer) {
            newCommentID = ((Integer) newCommentIDObject).longValue();
          } else if (newCommentIDObject instanceof Long) {
            newCommentID = (Long) newCommentIDObject;
          } else {
            AppLog.e(T.COMMENTS, "wp.newComment returned the wrong data type");
            newCommentID = -1;
          }
        } catch (XMLRPCException e) {
          AppLog.e(T.COMMENTS, "Error while sending the new comment", e);
          newCommentID = -1;
        } catch (IOException e) {
          AppLog.e(T.COMMENTS, "Error while sending the new comment", e);
          newCommentID = -1;
        } catch (XmlPullParserException e) {
          AppLog.e(T.COMMENTS, "Error while sending the new comment", e);
          newCommentID = -1;
        }

        final boolean succeeded = (newCommentID >= 0);

        if (actionListener != null) {
          handler.post(
              new Runnable() {
                @Override
                public void run() {
                  actionListener.onActionResult(succeeded);
                }
              });
        }
      }
    }.start();
  }