package com.backendsys.modules.cms.template;

import com.backendsys.modules.cms.article.dao.ArticleDao;
import com.backendsys.modules.cms.article.entity.Article;
import com.backendsys.modules.cms.article.service.ArticleService;
import com.backendsys.modules.common.aspect.Pages;
import com.backendsys.modules.common.utils.CookieUtil;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.v2.PageUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ViewArticleController {

    @Autowired
    private ArticleService articleService;

    @Pages
    @GetMapping({"/article"})
    public String article(Model model, HttpServletRequest request,
        @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
    ) {

        model.addAttribute("title","资讯中心");

//        // -- 初始化公共事件 ---------------------------------------------
//        RequestUtil.initResponse(model, request, cmsSiteInfoService, cmsNavigationService);
        String lang = CookieUtil.getCookie("lang", request);
        System.out.println("lang = " + lang);
//        // -- Header ---------------------------------------------
//        model.addAttribute("title", "zh".equals(lang) ? "资讯中心": "Aricle");
//        // -------------------------------------------------------
//
//        // [Get] 资讯列表
//        CmsArticleDTO cmsArticleDTO = new CmsArticleDTO();
//        cmsArticleDTO.setTitle(title);
//        cmsArticleDTO.setCategory_id(categoryId);
//        cmsArticleDTO.setLang(lang);

        Article article = new Article();
        article.setLang(lang);
        article.setTitle(title);
        article.setCategory_id(categoryId);

        PageUtils.startPage();  // 分页
        PageEntity articleList = articleService.selectArticleList(article);

        System.out.println(articleList);
        model.addAttribute("article", articleList);
//
//        // [Get] 资讯分类
//        List<Map<String, Object>> categoryList = articleCategoryService.queryArticleCategoryActive();
//        String categoryName = null;
//        if ("zh".equals(lang)) {
//            categoryName = (String) categoryList.stream()
//                    .filter(cate -> cate.get("id").equals(categoryId))
//                    .map(cate -> cate.get("category_name"))
//                    .findFirst().orElse(null);
//        }
//        if ("en".equals(lang)) {
//            categoryName = (String) categoryList.stream()
//                    .filter(cate -> cate.get("id").equals(categoryId))
//                    .map(cate -> cate.get("category_name_en"))
//                    .findFirst().orElse(null);
//        }
//
//
//        model.addAttribute("isBrand", true);
//        model.addAttribute("articleCategory", categoryList);
//
//        Map<String, Object> parameters = new LinkedHashMap<>();
//        parameters.put("title", title);
//        parameters.put("category_id", categoryId);
//        parameters.put("category_name", categoryName);
//        model.addAttribute("parameters", parameters);

        System.out.println("article layout!");

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


//    @GetMapping({"/articleDetail/{id}"})
//    public String articleDetail(Model model, HttpServletRequest request, @PathVariable("id") Long id) {
//
//        // -- 初始化公共事件 ---------------------------------------------
//        RequestUtil.initResponse(model, request, cmsSiteInfoService, cmsNavigationService);
//        // -- Header ---------------------------------------------
//        model.addAttribute("title","资讯中心");
//        // -------------------------------------------------------
//
//        // [Get] 资讯详情
//        CmsArticleDTO cmsArticleDTO = new CmsArticleDTO();
//        cmsArticleDTO.setArticle_id(id);
//        cmsArticleDTO.setLang((String) model.getAttribute("lang"));
//
//        Map<String, Object> detail = articleService.queryArticleDetailPublic(cmsArticleDTO);
//        model.addAttribute("articleDetail", detail);
//
//        // [Get] 资讯分类
//        model.addAttribute("articleCategory", articleCategoryService.queryArticleCategoryActive());
//
//        // -- Layout ---------------------------------------------
//        model.addAttribute("layout", "articleDetail");
//        return "layout/layout";
//    }

}
