package com.yonge.cooleshow.teacher.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.CourseHomeworkReviewDto;
import com.yonge.cooleshow.biz.dal.dto.CourseHomeworkSaveDto;
import com.yonge.cooleshow.biz.dal.dto.search.HomeworkSearch;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import com.yonge.cooleshow.biz.dal.enums.YesOrNoEnum;
import com.yonge.cooleshow.biz.dal.service.CourseHomeworkService;
import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.CountVo;
import com.yonge.cooleshow.biz.dal.vo.CourseHomeworkDetailVo;
import com.yonge.cooleshow.biz.dal.vo.CourseHomeworkVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Description 老师课后作业相关接口
 *
 * @author liujunchi
 * @date 2022-04-13
 */
@Api(tags = "老师课后作业相关接口")
@RestController
@RequestMapping("/homework")
public class CourseHomeworkController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private CourseScheduleService courseScheduleService;

    @Autowired
    private CourseHomeworkService courseHomeworkService;

    @ApiOperation(value = "未布置的课后作业数量")
    @GetMapping(value="/count")
    public HttpResponseResult<CountVo> countTeacherNoDecorateHomework() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        HomeworkSearch homeworkSearch = new HomeworkSearch();
        // 默认查当前老师，陪练课 课程状态为完成，有学生考勤记录 没有学生课程记录
        homeworkSearch.setTeacherId(sysUser.getId());
        homeworkSearch.setCourseStatus(CourseScheduleEnum.COMPLETE);
        homeworkSearch.setCourseType(CourseScheduleEnum.PRACTICE);
        homeworkSearch.setDecorate(YesOrNoEnum.NO);
        Integer integer = courseScheduleService.countTeacherNoDecorateHomework(homeworkSearch);
        CountVo countVo = new CountVo();
        countVo.setCount(integer);
        return succeed(countVo);
    }

    @ApiOperation(value = "首页-我的课程-课程详情(陪练课)-课后作业信息详情",notes = "传入课程编号ID")
    @GetMapping(value = "/detail/{courseId}")
    public HttpResponseResult<CourseHomeworkDetailVo> detail(@ApiParam(value = "课程编号ID", required = true)
                                                                 @PathVariable("courseId") Long courseId) {

        SysUser sysUser = sysUserFeignService.queryUserInfo();
        HttpResponseResult info = checkCourseSchedule(courseId,sysUser);
        if (info != null) return info;
        return succeed(courseHomeworkService.getCourseHomeworkDetailByCourseId(courseId));
    }

    private HttpResponseResult checkCourseSchedule(Long courseId,SysUser sysUser) {
        if (sysUser == null  || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (!courseScheduleService.checkTeacherCourseSchedule(sysUser.getId(), courseId)) {
            return failed("老师只能看自己的课程详情");
        }
        return null;
    }

    @ApiOperation(value = "陪练课-布置作业", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/decorate", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Boolean> decorateCourseHomework(@Valid @RequestBody CourseHomeworkSaveDto saveDto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        HttpResponseResult info = checkCourseSchedule(saveDto.getCourseScheduleId(),sysUser);
        if (info != null) return info;
        saveDto.setTeacherId(sysUser.getId());
        return succeed(courseHomeworkService.decorateCourseHomework(saveDto));
    }

    @ApiOperation(value = "陪练课-作业点评", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/review", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Boolean> reviewCourseHomework(@Valid @RequestBody CourseHomeworkReviewDto reviewDto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        HttpResponseResult info = checkCourseSchedule(reviewDto.getCourseScheduleId(),sysUser);
        if (info != null) return info;
        reviewDto.setTeacherId(sysUser.getId());
        return succeed(courseHomeworkService.reviewCourseHomework(reviewDto));
    }

    @ApiOperation(value = "课后作业-列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/list", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<CourseHomeworkVo>> list(@Valid @RequestBody HomeworkSearch query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (query.getDecorate() == null) {
            return failed("布置状态不能为空");
        }
        query.setTeacherId(sysUser.getId());
        query.setCourseStatus(CourseScheduleEnum.COMPLETE);
        query.setCourseType(CourseScheduleEnum.PRACTICE);
        return succeed(PageUtil.pageInfo(courseHomeworkService.selectPage(PageUtil.getPage(query),query)));
    }


}
