package cn.reghao.tnb.search.app.comment.service;

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.string.IdGenerator;
import cn.reghao.jutil.jdk.string.SnowFlake;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.search.app.comment.db.mongo.RichTextMongo;
import cn.reghao.tnb.search.app.comment.model.dto.ArticlePublishSbt;
import cn.reghao.tnb.search.app.comment.model.dto.UpdateArticleContent;
import cn.reghao.tnb.search.app.comment.model.dto.UpdateArticleScope;
import cn.reghao.tnb.search.app.comment.model.po.RichText;
import cn.reghao.tnb.search.app.comment.model.vo.AnswerCard;
import cn.reghao.tnb.search.app.comment.db.mongo.ArticlePostMongo;
import cn.reghao.tnb.search.app.comment.model.po.ArticlePost;
import cn.reghao.tnb.search.app.comment.model.vo.ArticleHeader;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2023-02-02 11:35:25
 */
@Service
public class ArticlePostService {
    private final ArticlePostMongo articlePostMongo;
    private final IdGenerator idGenerator = new IdGenerator("article-id");
    private final SnowFlake idGenerator1 = new SnowFlake(1L, 1L);
    private RichTextMongo richTextMongo;
    private int pageSize = 12;

    public ArticlePostService(ArticlePostMongo articlePostMongo, RichTextMongo richTextMongo) {
        this.articlePostMongo = articlePostMongo;
        this.richTextMongo = richTextMongo;
    }

    public void submitArticle(ArticlePublishSbt articlePublishSbt) {
        long loginUser = UserContext.getUserId();
        String title = articlePublishSbt.getTitle();
        String content = articlePublishSbt.getContent();

        RichText richText = new RichText();
        richText.setContentId(idGenerator1.nextId());
        richText.setOriginalUrl("");
        richText.setTitle(title);
        richText.setCoverUrl("");
        richText.setContent(content);
        richText.setPublishAt(System.currentTimeMillis());
        richText.setPublishBy(loginUser);
        richTextMongo.save(richText);
    }

    public void submitArticle1(ArticlePublishSbt articlePublishSbt) {
        //long userId = articlePublishSbt.getUserId();
        String title = articlePublishSbt.getTitle();
        String content = articlePublishSbt.getContent();
        //String excerpt = articlePublishSbt.getExcerpt();

        /*String articleId = idGenerator.stringId();
        ArticlePost articlePost = new ArticlePost(articleId, title, excerpt, content, userId);
        articlePostMongo.save(articlePost);

        StringBuilder sb = new StringBuilder();
        Document document = Jsoup.parse(content);
        Elements elements = document.select("p");
        elements.forEach(element -> {
            String text = element.text();
            sb.append(text).append(System.lineSeparator());
        });*/
    }

    public void updateArticleScope(UpdateArticleScope updateArticleScope) {
        String articleId = updateArticleScope.getArticleId();
        ArticlePost articlePost = articlePostMongo.findByArticleId(articleId);
        if (articlePost == null) {
            return;
        }

        int scope = updateArticleScope.getScope();
        articlePost.setScope(scope);
        articlePostMongo.update(articlePost);
    }

    public void updateArticleContent(UpdateArticleContent updateArticleContent) {
        String articleId = updateArticleContent.getArticleId();
        ArticlePost articlePost = articlePostMongo.findByArticleId(articleId);
        if (articlePost == null) {
            return;
        }

        articlePost.setContent(updateArticleContent.getContent());
        articlePostMongo.update(articlePost);
    }

    public PageList<ArticlePost> getPageByUserId(long userId, int page) {
        Long total = articlePostMongo.countByUserId(userId);
        List<ArticlePost> list = articlePostMongo.findByUserId(userId, page);
        return PageList.pageList(page, pageSize, total.intValue(), list);
    }

    public ArticlePost getArticleInfo(String articleId) {
        return articlePostMongo.findByArticleId(articleId);
    }

    public PageList<ArticleHeader> getArticleHeaders(long userId, int page) {
        List<ArticleHeader> list = articlePostMongo.findByUserId(userId, page).stream()
                .map(articlePost -> {
                    String articleId = articlePost.getArticleId();
                    String title = articlePost.getTitle();
                    String excerpt = articlePost.getExcerpt();
                    String publishAt = DateTimeConverter.format(articlePost.getCreateAt()*1000);
                    return new ArticleHeader(articleId, title, excerpt, publishAt);
                })
                .collect(Collectors.toList());

        int total = list.size();
        return PageList.pageList(page, pageSize, total, list);
    }

    public PageList<AnswerCard> getAnswerCards(long userId, int page) {
        List<AnswerCard> list = articlePostMongo.findByUserId(userId, page).stream()
                .map(articlePost -> {
                    String articleId = articlePost.getArticleId();
                    String title = articlePost.getTitle();
                    String content = articlePost.getContent();
                    long userId1 = articlePost.getUserId();

                    AnswerCard answerCard = new AnswerCard();
                    answerCard.setArticleId(articleId);
                    //answerCard.setQuestionId(537796453L);
                    //answerCard.setAnswerId(2529906921L);
                    answerCard.setTitle(title);
                    answerCard.setExcerpt(content.substring(0, 10));
                    answerCard.setContent(content);
                    //answerCard.setAnswerUrl("https://www.zhihu.com/question/268395554/answer/954662932");

                    answerCard.setUserId(userId1);
                    answerCard.setUsername("宁静致远");
                    answerCard.setAvatarUrl("https://pic1.zhimg.com/v2-77ecb55c95c30dba63280263244f1bfc_xll.jpg");
                    answerCard.setSignature("化工狗");
                    return answerCard;})
                .collect(Collectors.toList());

        int total = list.size();
        return PageList.pageList(page, pageSize, total, list);
    }
}
