package com.yonge.cooleshow.student.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.CourseHomeworkSubmitDto;
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.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 = "首页-我的课程-课程详情(陪练课)-课后作业信息详情",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.checkStudentCourseSchedule(sysUser.getId(), courseId)) {
            return failed("学生只能看自己购买的课程详情");
        }
        return null;
    }


    @ApiOperation(value = "陪练课-作业提交", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/submit", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Boolean> reviewCourseHomework(@Valid @RequestBody CourseHomeworkSubmitDto submitDto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        HttpResponseResult info = checkCourseSchedule(submitDto.getCourseScheduleId(),sysUser);
        if (info != null) return info;
        submitDto.setStudentId(sysUser.getId());
        return succeed(courseHomeworkService.submitCourseHomework(submitDto));
    }

    @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.getSubmit() == null) {
            return failed("提交状态不能为空");
        }
        query.setDecorate(YesOrNoEnum.YES);
        query.setStudentId(sysUser.getId());
        query.setCourseStatus(CourseScheduleEnum.COMPLETE);
        query.setCourseType(CourseScheduleEnum.PRACTICE);
        return succeed(PageUtil.pageInfo(courseHomeworkService.selectPage(PageUtil.getPage(query),query)));
    }


}
