package com.czbix.v2ex.parser;

import com.czbix.v2ex.model.Avatar;
import com.czbix.v2ex.model.Comment;
import com.czbix.v2ex.model.Member;
import com.czbix.v2ex.model.Topic;
import com.czbix.v2ex.model.TopicWithComments;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TopicParser extends Parser {
    private static final Pattern PATTERN_THANKS = Pattern.compile("\\d+");

    public static TopicWithComments parseDoc(Document doc, Topic topic) {
        final Topic.Builder topicBuilder = topic.toBuilder();

        parseContent(topicBuilder, doc.select(".topic_content"));
        List<Comment> comments = parseComments(doc.select("#Main > div:nth-child(4) tr"));

        return new TopicWithComments(topicBuilder.createTopic(), comments);
    }

    private static List<Comment> parseComments(Elements elements) {
        List<Comment> list = Lists.newArrayListWithCapacity(elements.size());

        for (Element ele : elements) {
            list.add(parseComment(ele));
        }

        return list;
    }

    private static Comment parseComment(Element ele) {

        final Avatar.Builder avatarBuilder = new Avatar.Builder();
        parseAvatar(avatarBuilder, ele);

        final Member.Builder memberBuilder = new Member.Builder();
        memberBuilder.setAvatar(avatarBuilder.createAvatar());
        parseMember(memberBuilder, ele);

        final Comment.Builder commentBuilder = new Comment.Builder();
        commentBuilder.setMember(memberBuilder.createMember());
        parseInfo(commentBuilder, ele);
        parseContent(commentBuilder, ele);

        return commentBuilder.createComment();
    }

    private static void parseContent(Comment.Builder builder, Element ele) {
        ele = ele.select(".reply_content").get(0);
        builder.setContent(ele.html());
    }

    private static void parseInfo(Comment.Builder builder, Element ele) {
        final Element tableEle = ele.parent().parent().parent();
        // example data: r_123456
        final int id = Integer.parseInt(tableEle.id().substring(2));
        builder.setId(id);

        final Elements elements = ele.select(".small");

        final Element timeEle = elements.get(0);
        builder.setReplyTime(timeEle.text());

        if (elements.size() == 2) {
            final Matcher matcher = PATTERN_THANKS.matcher(elements.get(1).text());
            Preconditions.checkState(matcher.find());

            final int thanks = Integer.parseInt(matcher.group());
            builder.setThanks(thanks);
        }
    }

    private static void parseMember(Member.Builder builder, Element ele) {
        ele = ele.select(".dark").get(0);

        builder.setUsername(ele.text());
    }

    private static void parseAvatar(Avatar.Builder builder, Element ele) {
        ele = ele.select(".avatar").get(0);

        builder.setUrl(ele.attr("src"));
    }

    private static void parseContent(Topic.Builder builder, Elements elements) {
        if (elements.size() == 0) return;
        final Element ele = elements.get(0);
        builder.setContent(ele.html());
    }
}

@@ -573,21 +573,22 @@
 PATTERN_
-THANK
+NUMBER
 S = Patt
@@ -808,24 +808,67 @@
 content%22));%0A
+        parseTopicInfo(topicBuilder, doc);%0A
         List
@@ -943,24 +943,24 @@
 (4) tr%22));%0A%0A
-
         retu
@@ -1022,32 +1022,520 @@
 mments);%0A    %7D%0A%0A
+    private static void parseTopicInfo(Topic.Builder topicBuilder, Document doc) %7B%0A        final Elements elements = doc.select(%22#Main %3E div:nth-child(4) %3E .cell:nth-child(1) %3E span%22);%0A        Preconditions.checkState(elements.size() == 1);%0A        final String text = elements.get(0).ownText();%0A        final Matcher matcher = PATTERN_NUMBERS.matcher(text);%0A        Preconditions.checkState(matcher.find());%0A%0A        topicBuilder.setReplyCount(Integer.parseInt(matcher.group()));%0A    %7D%0A%0A
     private stat
@@ -3075,13 +3075,14 @@
 ERN_
-THANK
+NUMBER
 S.ma

update reply count when view topic