package com.backendsys.modules.cms.views;

import cn.hutool.core.convert.Convert;
import com.backendsys.modules.cms.article.entity.Article;
import com.backendsys.modules.cms.article.entity.ArticleCategory;
import com.backendsys.modules.cms.article.service.ArticleCategoryService;
import com.backendsys.modules.cms.article.service.ArticleService;
import com.backendsys.modules.cms.siteinfo.entity.SiteInfo;
import com.backendsys.modules.common.aspect.Pages;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.v2.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
public class ArticleViewController {

    @Autowired
    private MessageSource messageSource;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Pages
    @Operation(summary = "资讯列表 (视图)")
    @GetMapping({"/article/list"})
    public String articleList(Model model,
        @RequestParam(value = "page_num", defaultValue = "1") Integer pageNum,
        @RequestParam(value = "page_size", defaultValue = "10") Integer pageSize,
        @RequestParam(value = "title", required = false) String title,
        @RequestParam(value = "category_id", required = false) Long categoryId
    ) {

        String lang = Convert.toStr(model.getAttribute("lang"));

        // [Get] 获取资讯列表 -------------------------------------------------
        // [Condition] 查询条件
        Article article = new Article();
        article.setLang(lang);
        article.setTitle(title);
        article.setCategory_id(categoryId);
        // [Condition] 分页
        PageUtils.startPage(pageNum, pageSize);
        PageEntity articleList = articleService.selectArticleList(article);
        model.addAttribute("articleList", articleList);

        // [Get] 获取资讯分类列表 ---------------------------------------------
        ArticleCategory articleCategory = new ArticleCategory();
        articleCategory.setLang(lang);
        articleCategory.setStatus(1);
        List<ArticleCategory> articleCategoryList = articleCategoryService.selectArticleCategory(articleCategory);
        model.addAttribute("articleCategoryList", articleCategoryList);

        // 参数暴露
        Map<String, Object> parameters = new LinkedHashMap<>();
        parameters.put("title", title);
        parameters.put("category_id", categoryId);
        model.addAttribute("parameters", parameters);

        // -- Layout ---------------------------------------------
        String titleTranslate = messageSource.getMessage("article.title", null, new Locale(lang));
        model.addAttribute("title", titleTranslate);

        model.addAttribute("layout", "article");
        return "layout/layout";
    }


    @Pages
    @Operation(summary = "资讯详情 (视图)")
    @GetMapping({"/article/detail/{uid}"})
    public String articleDetail(Model model, @PathVariable("uid") String uid) {

        String lang = Convert.toStr(model.getAttribute("lang"));

        // [Get] 获取资讯详情 ----------------------------------------------
        Article articleDetail = articleService.selectArticle(lang, uid);
        if (articleDetail == null) return "error";
        model.addAttribute("articleDetail", articleDetail);

        // -- Layout -----------------------------------------------------
        String titleTranslate = messageSource.getMessage("articleDetail.title", null, new Locale(lang));
        model.addAttribute("title", articleDetail.getTitle() + " - " + titleTranslate);

        // 重载页头信息
        SiteInfo siteInfo = (SiteInfo) model.getAttribute("siteInfo");
        siteInfo.setMeta_keyword(articleDetail.getMeta_keyword());
        siteInfo.setMeta_description(articleDetail.getMeta_description());
        model.addAttribute("siteInfo", siteInfo);

        model.addAttribute("layout", "articleDetail");
        return "layout/layout";
    }

}
