package api.torrents.torrents.comments;

import api.son.MySon;
import api.soup.MySoup;
import api.util.Tuple;

import java.util.ArrayList;
import java.util.List;

/**
 * The class TorrentComments
 * For getting and using the API related to reading
 * and posting TorrentComments
 *
 * @author Gwindow
 */
public class TorrentComments {
    /** The API response, contains the data we want */
	private Response response;

    /** The status of the API response */
	private String status;

	/**
	 * The torrent group id we're viewing comments for
	 */
	private transient int groupId;

	/**
	 * Get the first page of comments for some torrent group
	 *
	 * @param id id of torrent group to get comments
	 */
	public static TorrentComments fromId(int id){
		return fromId(id, 1);
	}

	/**
	 * Get the TorrentComments for some torrent from the torrent group ID
	 * and page number of the comments
	 *
	 * @param id   The TorrentGroup ID to get comments for
	 * @param page The page of comments to get
	 * @return The TorrentComments
	 */
	public static TorrentComments fromId(int id, int page){
		String url = "ajax.php?action=tcomments&id=" + id + "&page=" + page;
		TorrentComments tc = (TorrentComments)MySon.toObject(url, TorrentComments.class);
		tc.groupId = id;
		return tc;
	}

    /**
     * Post a comment on some torrent
     *
     * @param id The TorrentGroup id to post the comment on
     * @param body The body text of the comment to post
     */
    public static void postComment(int id, String body) {
        if (body.length() > 0) {
            try {
                String url = "torrents.php?action=takeedit_post&groupid=" + id;
                List<Tuple<String, String>> list = new ArrayList<Tuple<String, String>>();
                list.add(new Tuple<String, String>("action", "reply"));
                list.add(new Tuple<String, String>("auth", MySoup.getAuthKey()));
                list.add(new Tuple<String, String>("groupid", String.valueOf(id)));
                list.add(new Tuple<String, String>("body", body));
                MySoup.postMethod(url, list);
                System.out.println("Reply posted");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

	/**
	 * Post a comment on the torrent group
	 *
	 * @param body Text of comment to post
	 */
	public void postComment(String body){
		postComment(groupId, body);
	}

    /**
     * Get the API response
     *
     * @return the API response
     */
	public Response getResponse() {
		return this.response;
	}

    /**
     * Get the status of the API request
     *
     * @return True if the request was successful
     */
    public boolean getStatus(){
	    return status.equalsIgnoreCase("success");
    }

	/**
	 * Check if a next page of comments is available
	 */
	public boolean hasNextPage(){
		return response.getPage() != null && response.getPages().intValue() < response.getPages().intValue();
	}

	/**
	 * Get the next page of comments. Returns null if there is
	 * no next page
	 */
	public TorrentComments nextPage(){
		return hasNextPage() ? fromId(groupId, getPage() + 1) : null;
	}

	/**
	 * Check if a previous page of comments is available
	 */
	public boolean hasPreviousPage(){
		return response.getPage() != null && response.getPage().intValue() > 1;
	}

	/**
	 * Get the previous page of comments. Returns null if there is
	 * no previous page
	 */
	public TorrentComments previousPage(){
		return hasPreviousPage() ? fromId(groupId, getPage() - 1) : null;
	}

	/**
	 * Get the current page number being viewed
	 *
	 * @return the page
	 */
	public int getPage(){
		return response.getPage().intValue();
	}

	/**
	 * Get the total number of pages of comments
	 */
	public int getPages(){
		return response.getPages().intValue();
	}

	@Override
	public String toString() {
		return "TorrentComments [getResponse()=" + getResponse() + ", getStatus()=" + getStatus() + "]";
	}
}

@@ -596,11 +596,10 @@
 the 
-fir
+la
 st p
@@ -628,32 +628,101 @@
 me torrent group
+, this mimics the%0A%09 * site behavior of showing latest comments first.
 %0A%09 *%0A%09 * @param 
@@ -818,28 +818,173 @@
 %7B%0A%09%09
-return fromId(id, 1)
+String url = %22ajax.php?action=tcomments&id=%22 + id;%0A%09%09TorrentComments tc = (TorrentComments)MySon.toObject(url, TorrentComments.class);%0A%09%09tc.groupId = id;%0A%09%09return tc
 ;%0A%09%7D
@@ -3100,33 +3100,32 @@
 response.getPage
-s
 ().intValue() %3C 

Fix torrent comment page handling and put proper next page check in