package com.caimei.www.controller.unlimited;

import com.caimei.www.controller.BaseController;
import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.page.Article;
import com.caimei.www.pojo.page.BaseLink;
import com.caimei.www.pojo.page.ImageLink;
import com.caimei.www.service.page.ArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.util.List;

/**
 * 文章页鉴于seo,保留旧链接
 *
 * @author : Charles
 * @date : 2020/7/31
 */
@Controller
public class ArticleController extends BaseController {

    private static final String ARTICLE_LIST_PATH = "article/list";
    private static final String ARTICLE_DETAIL_PATH = "article/detail";
    private ArticleService articleService;

    @Autowired
    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    /**
     * 文章列表【旧center】
     */
    @GetMapping("/info/center-{id}-1.html")
    public String toArticleList(@PathVariable("id") Integer id, final Model model) {
        List<BaseLink> typeList = articleService.getArticleTypes();
        model.addAttribute("articleType", typeList);
        model.addAttribute("typeId", id);
        model.addAttribute("labelId", 0);
        return ARTICLE_LIST_PATH;
    }

    /**
     * 文章列表【旧label】
     */
    @GetMapping("/info/label-{id}-1.html")
    public String toArticleLabel(@PathVariable("id") Integer id, final Model model) {
        List<BaseLink> typeList = articleService.getArticleTypes();
        model.addAttribute("articleType", typeList);
        model.addAttribute("typeId", 0);
        model.addAttribute("labelId", id);
        return ARTICLE_LIST_PATH;
    }

    /**
     * 文章搜索结果
     */
    @GetMapping("/info/search.html")
    public String toArticleSearch(final Model model) {
        List<BaseLink> typeList = articleService.getArticleTypes();
        model.addAttribute("articleType", typeList);
        model.addAttribute("typeId", 0);
        model.addAttribute("labelId", 0);
        return ARTICLE_LIST_PATH;
    }

    /**
     * 文章详情【旧】
     */
    @GetMapping("/info/detail-{id}-1.html")
    public String toArticleDetail(@PathVariable("id") Integer id, final Model model) {
        List<BaseLink> typeList = articleService.getArticleTypes();
        Article article = articleService.getArticleInfo(id);
        model.addAttribute("article", article);
        model.addAttribute("articleType", typeList);
        model.addAttribute("articleId", id);
        model.addAttribute("typeId", (article!=null ? article.getTypeId() : 0));
        model.addAttribute("labelId", 0);
        return ARTICLE_DETAIL_PATH;
    }

    /**
     * 获取文章热门标签
     */
    @GetMapping("/article/labels")
    @ResponseBody
    public JsonModel<List<BaseLink>> getArticleLabels() {
        return articleService.getArticleLabels();
    }

    /**
     * 获取文章推荐
     */
    @GetMapping("/article/recommend")
    @ResponseBody
    public JsonModel<PageInfo<ImageLink>> getArticleRecommended(Integer typeId,
                                                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                                @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        return articleService.getArticleRecommended(typeId, pageNum, pageSize);
    }

    /**
     * 获取相关文章
     */
    @GetMapping("/article/related")
    @ResponseBody
    public JsonModel<PageInfo<Article>> getArticleRelated(Integer id, String labels,
                                                          @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        return articleService.getArticleRelated(id, labels, pageNum, pageSize);
    }

    /**
     * 获取文章广告
     */
    @GetMapping("/article/ads")
    @ResponseBody
    public JsonModel<List<ImageLink>> getLastestInfoAds() {
        return articleService.getLastestInfoAds();
    }

    /**
     * 点击标签
     */
    @GetMapping("/article/label/click")
    @ResponseBody
    public JsonModel clickArticleLabel(Integer id) {
        return articleService.clickArticleLabel(id);
    }

    /**
     * 点击广告
     */
    @GetMapping("/article/ad/click")
    @ResponseBody
    public JsonModel clickArticleAd(Integer id) {
        return articleService.clickArticleAd(id);
    }

    /**
     * 文章点赞
     */
    @GetMapping("/article/like")
    @ResponseBody
    public JsonModel articleLike(Integer id, ServerWebExchange serverWebExchange) {
        return articleService.articleLike(id, serverWebExchange);
    }

}
