package cn.reghao.tnb.search.esapp.blog.controller;

import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.search.esapp.blog.model.dto.ArticleDto;
import cn.reghao.tnb.search.esapp.blog.model.vo.AdminArticle;
import cn.reghao.tnb.search.esapp.blog.model.vo.EditArticle;
import cn.reghao.tnb.search.esapp.blog.service.ArticleQuery;
import cn.reghao.tnb.search.esapp.blog.service.ArticleService;
import cn.reghao.tnb.search.esapp.hibernate.HibernateLucene;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2023-04-17 10:15:39
 */
@Tag(name = "博客文章接口")
@RestController
@RequestMapping("/api/blog/v2/post")
public class AdminArticleController {
    private final ArticleService articleService;
    private final ArticleQuery articleQuery;
    private final HibernateLucene hibernateLucene;
    private int pageSize = 10;

    public AdminArticleController(ArticleService articleService, ArticleQuery articleQuery,
                                  HibernateLucene hibernateLucene) {
        this.articleService = articleService;
        this.articleQuery = articleQuery;
        this.hibernateLucene = hibernateLucene;
    }

    @Operation(summary = "博客文章列表页面", description = "N")
    @GetMapping("/list")
    public String list(@RequestParam("pn") int pageNumber,
                       @RequestParam(value = "categoryId", required = false) Integer categoryId,
                       @RequestParam(value = "published", required = false) Integer published,
                       @RequestParam(value = "title", required = false) String title) {
        if (categoryId == null) {
            categoryId = 0;
        }

        if (published == null) {
            published = 0;
        }

        if (title == null) {
            title = "";
        }

        int postType = 1; // 文章
        Page<AdminArticle> page1 = articleQuery.findAdminArticleByPage(pageSize, pageNumber, postType, categoryId, published, title);
        PageList<AdminArticle> pageList = getPageList(page1);
        return WebResult.success(pageList);
    }

    private PageList<AdminArticle> getPageList(Page<AdminArticle> page) {
        int pageNumber = page.getNumber() + 1;
        int pageSize = page.getSize();
        long total = page.getTotalElements();
        List<AdminArticle> list = page.getContent();
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    @Operation(summary = "编辑文章", description = "N")
    @GetMapping("/edit")
    public String edit(@RequestParam("articleId") String articleId) {
        EditArticle editArticle = articleQuery.getEditArticle(articleId);
        return WebResult.success(editArticle);
    }

    @Operation(summary = "新增/更新文章接口", description = "N")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String editArticle(@Validated ArticleDto articleDto) {
        String articleId = articleDto.getArticleId();
        Result result;
        if (articleId == null) {
            result = articleService.addArticle(articleDto);
        } else {
            result = articleService.updateArticle(articleDto);
        }

        return WebResult.result(result);
    }

    @Operation(summary = "删除文章", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String delete(@RequestParam("articleId") String articleId) {
        articleService.deleteArticle(articleId);
        return WebResult.success();
    }

    @Operation(summary = "重置文章索引", description = "N")
    @PostMapping(value = "/reset_index", produces = MediaType.APPLICATION_JSON_VALUE)
    public String resetIndex() {
        hibernateLucene.resetIndex();
        return WebResult.success();
    }
}
