package com.ynfy.app.api.v1.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ynfy.app.api.v1.annoation.IgnoreAuth;
import com.ynfy.buss.article.service.IArticleService;
import com.ynfy.buss.banner.service.IBannerService;
import com.ynfy.buss.course.course.service.ICourseService;
import com.ynfy.buss.exam.exam.entity.Exam;
import com.ynfy.buss.exam.exam.service.IExamService;
import com.ynfy.buss.practice.userpractice.service.IUserPracticeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.TokenUtil;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/v1/index")
public class ApiIndexController extends ApiBaseController {

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private ISysAnnouncementService announcementService;

    @Autowired
    private ISysAnnouncementSendService announcementSendService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IExamService examService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private IUserPracticeService userPracticeService;

    @IgnoreAuth
    @PostMapping("/banner/list")
    public Result<?> list() {
        ;
        return Result.OK(bannerService.list());
    }

    /**
     * 查询通知公告
     *
     * @return
     */
    @IgnoreAuth
    @PostMapping("/announcement/list")
    public Result<?> announcementList() {
        String userId = TokenUtil.getUserId(TokenUtil.getToken(request));
        SysAnnouncement announcement = new SysAnnouncement();
        announcement.setUserId(userId);
        List<SysAnnouncement> pageList = announcementService.listNoRead(announcement);
        return Result.OK(pageList);
    }

    /**
     * 阅读通知公告
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @GetMapping(value = "/viewMessage")
    public Result<?> viewMessage(@RequestParam String id) {
        SysAnnouncement announcement = announcementService.getById(id);

        String userId = TokenUtil.getUserId(TokenUtil.getToken(request));
        //阅读通知公告
        if (StringUtils.isNotBlank(userId)) {
            announcementService.readMessage(id, userId);
        }
        return Result.OK(announcement);
    }


    /**
     * 热门课程
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listHotCourse")
    public Result<?> listHotCourse(Integer limit) {
        return Result.OK(courseService.listHotCourse(limit));
    }

    /**
     * 最新考试
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listLatestExam")
    public Result<?> listLatestExam(Integer limit) {
        List<Exam> examList = examService.listLatestExam(limit, TokenUtil.getUserId(TokenUtil.getToken(request)));
        if (!CollectionUtils.isEmpty(examList)) {
            examList.forEach(item -> item.setAppShowTime(DateUtil.parseDate(DateUtil.formatDate(!Objects.isNull(item.getUpdateTime())
                    ? item.getUpdateTime() : item.getCreateTime()))));
        }
        return Result.OK(examList);
    }

    /**
     * 推荐资讯
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listRecommendArticle")
    public Result<?> listRecommendArticle(Integer limit) {
        return Result.OK(articleService.listRecommendArticle(limit));
    }

    /**
     * 查看资讯
     *
     * @param id
     * @return
     */
    @IgnoreAuth
    @GetMapping(value = "/viewArticle")
    public Result<?> viewArticle(@RequestParam String id) {
        return Result.OK(articleService.getById(id));
    }

    /**
     * 最近练习
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listLatestPractice")
    public Result<?> listLatestPractice(Integer limit) {
        return Result.OK(userPracticeService.listLatestPractice(limit, TokenUtil.getUserId(TokenUtil.getToken(request))));
    }

}
