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

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.search.app.blog.model.CategoryType;
import cn.reghao.tnb.search.app.blog.model.po.AboutView;
import cn.reghao.tnb.search.app.blog.model.vo.*;
import cn.reghao.tnb.search.app.blog.service.ArticleQuery;
import cn.reghao.tnb.search.app.blog.service.ArticleViewService;
import cn.reghao.tnb.search.app.blog.service.CategoryService;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.search.app.hibernate.HibernateQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author reghao
 * @date 2023-04-16 14:27:07
 */
@Tag(name = "博客前台页面")
@RestController
@RequestMapping("/api/blog")
public class BlogController /*extends BaseController */{
    private final CategoryService categoryService;
    private final ArticleQuery articleQuery;
    private final ArticleViewService articleViewService;
    private final HibernateQuery hibernateQuery;
    private final int pageSize = 10;

    public BlogController(CategoryService categoryService, ArticleQuery articleQuery,
                          ArticleViewService articleViewService, HibernateQuery hibernateQuery) {
        this.categoryService = categoryService;
        this.articleQuery = articleQuery;
        this.articleViewService = articleViewService;
        this.hibernateQuery = hibernateQuery;
    }

    @Operation(summary = "前台首页", description = "N")
    @GetMapping(value = "/post/list")
    public String index(@RequestParam("pn") int pageNumber) {
        Page<UserArticle> page = articleQuery.findUserArticleByPage(pageSize, pageNumber);
        PageList<UserArticle> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    @Operation(summary = "博客文章内容页面", description = "N")
    @GetMapping("/post/detail")
    public String article(@RequestParam("articleId") String articleId) {
        ArticleVO articleVO = articleQuery.getArticle(articleId);
        if (articleVO == null) {
            return WebResult.failWithMsg("not found");
        }

        articleViewService.incr(articleId);
        return WebResult.success(articleVO);
    }

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

    @Operation(summary = "文章分类/标签列表页面", description = "N")
    @GetMapping("/category")
    public String tag(@RequestParam("type") String type) {
        int typeInt = 0;
        if (type.equals("tag")) {
            typeInt = CategoryType.Tag.getValue();
        } else if (type.equals("category")) {
            typeInt = CategoryType.Category.getValue();
        } else {
            WebResult.failWithMsg("type invalid");
        }

        List<CategoryCount> list = categoryService.findCategoryCountByPage(typeInt, false);
        return WebResult.success(list);
    }

    @Operation(summary = "某个分类/标签下的文章列表页面", description = "N")
    @GetMapping("/category/post")
    public String tagPost(/*@RequestParam("type") String type1,
                          @RequestParam("name") String name1,
                          @RequestParam("pn") int pageNumber1,*/
                          HttpServletRequest request) {
        Map<String, String> map = getQuery(request);
        String type = map.get("type");
        if (type == null) {
            return WebResult.failWithMsg("parameter type not exist");
        }

        String name = map.get("name");
        if (name == null) {
            return WebResult.failWithMsg("parameter name not exist");
        }

        String pn = map.get("pn");
        if (pn == null) {
            return WebResult.failWithMsg("parameter pn not exist");
        }
        int pageNumber = Integer.parseInt(pn);

        int typeInt = 0;
        if (type.equals("tag")) {
            typeInt = CategoryType.Tag.getValue();
        } else if (type.equals("category")) {
            typeInt = CategoryType.Category.getValue();
        } else {
            WebResult.failWithMsg("type invalid");
        }

        Page<UserArticle> page = articleQuery.findByCategory(pageSize, pageNumber, name, typeInt);
        PageList<UserArticle> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    private Map<String, String> getQuery(HttpServletRequest request) {
        String queryString = request.getQueryString();
        Map<String, String> map = new HashMap<>();
        for (String param : queryString.split("&")) {
            String[] paramArr = param.split("=");
            map.put(paramArr[0], paramArr[1]);
        }
        return map;
    }

    @Operation(summary = "文章归档列表页面", description = "N")
    @GetMapping("/archive")
    public String archive() {
        List<ArchiveArticle> results = articleQuery.getArchiveArticles();
        List<ArticleLink> articleLinkList = articleQuery.getArticleLinks();
        return WebResult.success(articleLinkList);
    }

    @Operation(summary = "文章搜索结果列表页面", description = "N")
    @GetMapping("/search")
    public String search(@RequestParam("kw") String kw, @RequestParam("pn") int pageNumber) {
        PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.ASC, "createTime"));
        Page<UserArticle> page = hibernateQuery.searchArticle(kw, pageRequest);
        PageList<UserArticle> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    @Operation(summary = "关于页面", description = "N")
    @GetMapping("/about")
    public String about() {
        AboutView aboutView = articleQuery.getAboutView();
        return WebResult.success(aboutView);
    }
}
