package com.ym.mec.teacher.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.CourseScheduleEvaluate;
import com.ym.mec.biz.service.CourseScheduleEvaluateService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RequestMapping("studyReport")
@Api(tags = "陪练报告")
@RestController
public class StudyReportController extends BaseController {

    @Autowired
    private CourseScheduleEvaluateService courseScheduleEvaluateService;
    @Autowired
    private SysUserFeignService sysUserFeignService;


    @ApiOperation("获取学生陪练报告信息")
    @GetMapping(value = "info")
    public Object info(Integer classGroupId) {
        return succeed(courseScheduleEvaluateService.getStudyReport(classGroupId));
    }

    @ApiOperation(value = "课程组评论列表")
    @GetMapping("getGroupReviews")
    public Object getGroupReviews(Integer groupId) {
        List<CourseScheduleEvaluate> courseScheduleEvaluates = courseScheduleEvaluateService.findByGroupId(groupId);
        boolean hasReport = false;
        Date nowDate = new Date();
        for (CourseScheduleEvaluate courseScheduleEvaluate : courseScheduleEvaluates) {
            courseScheduleEvaluate.setTimes(2);
            courseScheduleEvaluate.setTotalMinutes(100);
            courseScheduleEvaluate.setReportLink("http://www.baidu.com");
            if (DateUtil.format(courseScheduleEvaluate.getCreateTime(), "yyyy-MM").equals(DateUtil.format(nowDate, "yyyy-MM"))) {
                hasReport = true;
            }
        }
        if (!hasReport) {
            CourseScheduleEvaluate courseScheduleEvaluate = new CourseScheduleEvaluate();
            courseScheduleEvaluate.setReportLink("http://www.baidu.com");
            courseScheduleEvaluate.setTimes(2);
            courseScheduleEvaluate.setTotalMinutes(100);
            courseScheduleEvaluate.setMonth(DateUtil.format(nowDate, "yyyy年MM月"));
        }

        return succeed(courseScheduleEvaluates);
    }

    @ApiOperation(value = "评论详情")
    @GetMapping("getReviewsInfo")
    public Object getReviewsInfo(Integer id) {
        return succeed(courseScheduleEvaluateService.findById(id));
    }

    @ApiOperation(value = "获取需评论的课程列表")
    @GetMapping("getNeedPost")
    public Object getNeedPost() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (Objects.isNull(sysUser)) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return succeed(courseScheduleEvaluateService.getNeedPostList(sysUser.getId()));
    }

    @ApiOperation(value = "获取需评论的课程列表")
    @GetMapping("createEvaluate")
    public Object createEvaluate(Date date) {
        Date nowDate = new Date();
        if(date != null){
            nowDate = date;
        }
        Date afterDate = DateUtil.addDays1(nowDate, 5);
        return succeed(courseScheduleEvaluateService.createEvaluate(nowDate, afterDate));
    }
}
