package com.yonge.cooleshow.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.CourseScheduleEnum;
import com.yonge.cooleshow.biz.dal.service.CourseHomeworkService;
import com.yonge.cooleshow.biz.dal.service.CourseScheduleService;
import com.yonge.cooleshow.biz.dal.service.ImGroupService;
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.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Description 学生课后作业相关接口
 *
 * @author liujunchi
 * @date 2022-04-13
 */
@Api(tags = "学生课后作业相关接口")
@RestController
@RequestMapping("${app-config.url.student:}/homework")
public class CourseHomeworkController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private CourseScheduleService courseScheduleService;

    @Autowired
    private CourseHomeworkService courseHomeworkService;

    @Autowired
    private ImGroupService imGroupService;


    @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;
        CourseHomeworkDetailVo detailVo = courseHomeworkService.getCourseHomeworkDetailByCourseId(courseId, sysUser.getId());
        if (detailVo != null) {
            detailVo.setImUserId(imGroupService.getImUserId(String.valueOf(detailVo.getTeacherId()),ClientEnum.TEACHER.name()));
        }
        return succeed(detailVo);
    }

    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);
        List<CourseScheduleEnum> list = new ArrayList<>();
        list.add(CourseScheduleEnum.PIANO_ROOM_CLASS);
        list.add(CourseScheduleEnum.PRACTICE);
        query.setCourseType(list);

        IPage<CourseHomeworkVo> page = courseHomeworkService.selectPage(PageUtil.getPage(query), query);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {

            for (CourseHomeworkVo item : page.getRecords()) {
                item.setImUserId(imGroupService.getImUserId(String.valueOf(item.getTeacherId()),ClientEnum.TEACHER.name()));
            }
        }

        return succeed(PageUtil.pageInfo(page));
    }


}
