package com.cirle.scientific.controller.view;

import com.cirle.scientific.dao.QkjArticleDao;
import com.cirle.scientific.dto.QkjRecommendDTO;
import com.cirle.scientific.po.QkjArticle;
import com.cirle.scientific.pojo.Result;
import com.cirle.scientific.service.ArticleService;
import com.cirle.scientific.service.RecommendService;
import com.cirle.scientific.utils.DeviceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/view")
@Api("资讯相关接口")
public class ViewController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private QkjArticleDao articleDao;


    @GetMapping("/index")
    @ApiOperation(value = "去往资讯首页")
    public String index(
            HttpServletRequest request,
            @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = true) String from
    ){
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/view/view-index";
        } else {
            return "web/view/view-index";
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "去往文章详情页面")
    public String detail(
            HttpServletRequest request,
            @ApiParam(name = "from",value = "请求类型 web：网站 H5：手机端") @RequestParam(name = "from",required = false) String from,
            @ApiParam(name = "artId",value = "文章ID") @RequestParam(name = "artId",required = true) Long artId
    ){
        //获取详情
        request.setAttribute("artId", artId);
        QkjArticle art = articleDao.findById(artId).get();
        if(art.getType() == 2){
            String redirectUrl = "redirect:"+art.getContent().trim();
            return redirectUrl;
        }
        if (StringUtils.isNotBlank(from) && from.equals("H5")) {
            return "html5/view/view-detail";
        } else {
            return "web/view/view-detail";
        }
    }

    @PostMapping("/getArtListData")
    @ApiOperation(value = "获取文章列表")
    @ResponseBody
    public Result getArtListData(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "cataId",value = "分类ID") @RequestParam(name = "cataId",required = true) Integer cataId,
            @ApiParam(name = "sortBy",value = "排序字段") @RequestParam(name = "sortBy",required = false) String sortBy,
            @ApiParam(name = "page",value = "分页") @RequestParam(name = "page",required = true,defaultValue="1") Integer page,
            @ApiParam(name = "size",value = "每页数量") @RequestParam(name = "size",required = true,defaultValue="10") Integer size,
            HttpServletRequest request
    ){
        boolean isPC= DeviceUtil.isMobileDevice(request);
        return Result.success(articleService.getArtListData(memCard, cataId, page, size,sortBy,isPC));
    }

    @PostMapping("/getAdInfo")
    @ResponseBody
    @ApiOperation(value = "课程首页获取广告位")
    public Result<List<QkjRecommendDTO>> getAdInfo(HttpServletRequest request) {
        // 默认展示pc端内容  0全部不显示 、1全部显示 、2m端显示、3pc端显示
        Integer status = 3;
        if(DeviceUtil.isMobileDevice(request)) {
            status = 2;
        }
        List<QkjRecommendDTO> recmdTop = recommendService.findByPosIdLimit(18L, status, 1, 10);
        return Result.success(recmdTop);
    }

    @PostMapping("/updataClick")
    @ResponseBody
    @ApiOperation(value = "课程首页获取广告位")
    public Result updataClick(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "artId",value = "咨询id") @RequestParam(name = "artId",required = true) Long artId
    ){
        return  articleService.updataClick(artId);
    }

    @PostMapping("/getDetailData")
    @ApiOperation(value = "获取资讯详情")
    @ResponseBody
    public Result getDetailData(
            @RequestAttribute(value = "memCard",required = false) String memCard,
            @ApiParam(name = "artId",value = "直播ID") @RequestParam(name = "artId",required = true) Long artId
    ){
        return articleService.getDetailData(artId, memCard);
    }
}
