package me.qwq.doghouse;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import lombok.extern.slf4j.Slf4j;
import me.qwq.doghouse.config.DoghouseProperties;
import me.qwq.doghouse.dao.PostMapper;
import me.qwq.doghouse.entity.Comment;
import me.qwq.doghouse.entity.Meta;
import me.qwq.doghouse.entity.Post;
import me.qwq.doghouse.enums.MetaTypeEnum;
import me.qwq.doghouse.enums.PostTypeEnum;
import me.qwq.doghouse.service.AssetService;
import me.qwq.doghouse.service.CommentService;
import me.qwq.doghouse.service.MetaService;
import me.qwq.doghouse.service.post.PostService;

@SpringBootTest
@Slf4j
public class MyBlogApplicationTests {
    
    @Autowired
    CommentService commentService;

    @Autowired
    PostService postService;
    
    @Autowired
    PostMapper postMapper;
    
    @Autowired
    MetaService metaService;
    
    @Autowired
    AssetService assetService;
    
    @Autowired
    DoghouseProperties doghouseProperties;
    
    //@Test
    public void testDoghouseProperties() {
        log.info("{}", doghouseProperties);
    }
    
    //@Test
    public void testGetPopularPosts() {
        List<Post> post =
        postService.getPopularPosts(null, PostTypeEnum.POST);
        post.forEach(System.out::println);
    }

    //@Test
    public void testPostSaveOrUpdateBatch() {
        Post post = postMapper.selectById(19L);
        log.info("{}", post.getPostTitle());
        boolean success = postService.saveOrUpdateBatch(new ArrayList<Post>() {
            private static final long serialVersionUID = 1L;
            {
                add(post);
            }
        });
        log.info("{}", success);
    }
    
    //@Test
    public void testMetaSaveOrUpdateBatch() {
        Meta meta = metaService.getById(1L);
        log.info("{}", meta.getName());
        boolean success = metaService.saveOrUpdateBatch(new ArrayList<Meta>() {
            private static final long serialVersionUID = 1L;
            {
                add(meta);
            }
        });
        log.info("{}", success);
    }
    
    //@Test
    public void testListMetaCount() {
        List<Meta> metas =
        metaService.listMetaCount(MetaTypeEnum.CATEGORY , null);
        metas.forEach(System.out::println);
    }

	//@Test
	public void contextLoads() {
	    List<Comment> comments = commentService.list();
	    for (Comment comment : comments) {
	        updateCommentLevel (comment);
	    }
	}
	
	public Integer updateCommentLevel(Comment comment) {
	    Long replyTo = comment.getReplyTo();
        if (replyTo != -1L) {
            Comment parent = commentService.getById(replyTo);
            Integer parentLevel = updateCommentLevel(parent);
            comment.setLevel(parentLevel + 1);
            commentService.saveOrUpdate(comment);
            return parentLevel + 1;
        }
        else {
            comment.setLevel(0);
            commentService.saveOrUpdate(comment);
            return 0;
        }
	}

}
